Closed GoogleCodeExporter closed 9 years ago
I also get the expected result when I wrap the Object[] with
Arrays.asList(...). See
attachment for a simple example of this behavior.
Original comment by ket...@gmail.com
on 2 Apr 2009 at 9:12
Attachments:
I also encounter this problem with following code:
String[] str = new String[3];
str[0] = "str";
Object[] obj = new Object[3];
obj[0] = str;
ArrayList lst = new ArrayList();
lst.add(str);
Gson gson = new Gson();
System.out.println(gson.toJson(obj));
System.out.println(gson.toJson(lst));
System.out.println(gson.toJson(lst.toArray()));
Output:
[{},null,null]
[["str",null,null]]
[{}]
It seems that Gson can not handle array properly.
Original comment by wanpee...@gmail.com
on 7 May 2009 at 10:41
Expecting output for code above.
[["str",null,null],null,null]
[["str",null,null]]
[["str",null,null]]
Original comment by wanpee...@gmail.com
on 7 May 2009 at 10:44
This works acceptably well for me at the moment:
static class ArraySerializer implements JsonSerializer<Object[]>,
JsonDeserializer<Object[]> {
public JsonElement serialize(final Object[] src, final Type typeOfSrc, final
JsonSerializationContext context) {
if (src == null) {
return new JsonNull();
}
JsonArray result = new JsonArray();
for (Object el : src) {
if (el == null) {
result.add(new JsonNull());
} else {
result.add(context.serialize(el, el.getClass()));
}
}
return result;
}
@Override
public Object[] deserialize(final JsonElement json, final Type typeOfT, final
JsonDeserializationContext context)
throws JsonParseException {
JsonArray array = json.getAsJsonArray();
Object[] result = new Object[array.size()];
int tally = 0;
for (JsonElement childElement : array) {
Object value;
if (childElement == null || childElement.isJsonNull()) {
value = null;
} else {
Type type = null;
if (childElement.isJsonArray()) {
type = Object[].class;
} else if (childElement.isJsonObject()) {
type = Object.class;
} else if (childElement.isJsonPrimitive()) {
final JsonPrimitive primitive = childElement.getAsJsonPrimitive();
if (primitive.isBoolean()) {
type = Boolean.class;
} else if (primitive.isNumber()) {
type = Number.class;
} else if (primitive.isString()) {
type = String.class;
}
}
value = context.deserialize(childElement, type);
if (value instanceof Object[]) {
value = new ArrayList(Arrays.asList((Object[]) value));
}
}
result[tally] = value;
++tally;
}
return result;
}
}
Though I'm pretty sure it doesn't cover all the corner cases or generics.
Original comment by anton.sa...@gmail.com
on 7 May 2009 at 12:11
r432 adds ability to serialize and deserialize array/collections of
java.lang.Object
when the actual object is a primitive.
Original comment by inder123
on 27 Jul 2009 at 11:26
Original comment by inder123
on 27 Jul 2009 at 11:26
Hi! if someone could help me, I need to serialize and deserialize Date format
for
Http calls to a Web Service, I ´m using gson 1.3
Thanks
Flor
Original comment by florpere...@gmail.com
on 29 Jul 2009 at 9:59
Fixed in r442.
Original comment by joel.leitch@gmail.com
on 22 Sep 2009 at 7:13
Original issue reported on code.google.com by
ket...@gmail.com
on 2 Apr 2009 at 9:09Attachments: