AiorosXu / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

GSON does not handle Object[] well #114

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a 1-element Object[] array
2. Insert a Double into said array
3. Convert said array to JSON using GSON

What is the expected output? What do you see instead?
I expected to get "[1235.12411]" but got "[{}]" instead.  This happens for
other element types such as java.util.Date and java.lang.Integer.  I see
the expected output if I use a Double[] or List instead of Object[].

Using GSON 1.3 release on Ubuntu Intrepid Ibex.
$ java -version
java version "1.6.0_10"
Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
Java HotSpot(TM) 64-Bit Server VM (build 11.0-b15, mixed mode)

Original issue reported on code.google.com by ket...@gmail.com on 2 Apr 2009 at 9:09

Attachments:

GoogleCodeExporter commented 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:

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by inder123 on 27 Jul 2009 at 11:26

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
Fixed in r442.

Original comment by joel.leitch@gmail.com on 22 Sep 2009 at 7:13