narayana1208 / google-gson

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

Confusing behavior when Object is the field type #205

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Object[] array = new Object[] { new Object[] { 1, 2 } };
2. Gson gson = new Gson();
3. System.out.println(gson.toJson(array));

What is the expected output? 
[[1,2]]

What do you see instead?
[{}]

What version of the product are you using? On what operating system?
1.4 on Windows XP.

Please provide any additional information below.
I may be doing this wrong...  Is that behavior intentional?

Original issue reported on code.google.com by michael.hixson@gmail.com on 23 Apr 2010 at 10:51

GoogleCodeExporter commented 9 years ago
I add something similar to your problem, issue 209.
I "fix" your problem as following on version 1;3 (1.4 is beta did not look at 
it):
Class: com.google.gson.JsonSerializationVisitor
Method: visitArray

Old content
  public void visitArray(Object array, Type arrayType) {
    assignToRoot(new JsonArray());
    int length = Array.getLength(array);
    TypeInfoArray fieldTypeInfo = TypeInfoFactory.getTypeInfoForArray(arrayType);
    Type componentType = fieldTypeInfo.getSecondLevelType();
    for (int i = 0; i < length; ++i) {
      Object child = Array.get(array, i);
      Type childType = componentType;
      // we should not get more specific component type yet since it is possible
      // that a custom
      // serializer is registered for the componentType
      addAsArrayElement(new ObjectTypePair(child, childType, false));
    }
  }

New content
  public void visitArray(Object array, Type arrayType) {
    assignToRoot(new JsonArray());
    int length = Array.getLength(array);
    for (int i = 0; i < length; ++i) {
      Object child = Array.get(array, i);
      // we should not get more specific component type yet since it is possible
      // that a custom
      // serializer is registered for the componentType
      addAsArrayElement(new ObjectTypePair(child, child.getClass(), false));
    }
  }

Etienne

Original comment by lapinouj...@gmail.com on 15 May 2010 at 1:02

GoogleCodeExporter commented 9 years ago
Sorry my thread was for the current version in trunk.
For version 1.3 I did the folowwing:
Class: com.google.gson.JsonSerializationVisitor
Method: visitArray
Old content
  public void visitArray(Object array, Type arrayType) {
    assignToRoot(new JsonArray());
    int length = Array.getLength(array);
    TypeInfoArray fieldTypeInfo = TypeInfoFactory.getTypeInfoForArray(arrayType);
    Type componentType = fieldTypeInfo.getSecondLevelType();
    for (int i = 0; i < length; ++i) {
      Object child = Array.get(array, i);
      addAsArrayElement(componentType, child);
    }
  }

New content
  public void visitArray(Object array, Type arrayType) {
    assignToRoot(new JsonArray());
    int length = Array.getLength(array);
    for (int i = 0; i < length; ++i) {
      Object child = Array.get(array, i);
      addAsArrayElement(child.getClass(), child);
    }
  }

Etienne

Original comment by lapinouj...@gmail.com on 15 May 2010 at 1:10

GoogleCodeExporter commented 9 years ago

Original comment by inder123 on 3 Nov 2010 at 1:48

GoogleCodeExporter commented 9 years ago
FYI - you can work around this by providing static type information when 
calling toJson().
  toJson(array, Integer[][].class);

In general GSON works better when it knows the types of everything!

Original comment by limpbizkit on 3 Nov 2010 at 4:43

GoogleCodeExporter commented 9 years ago
@limpbizkit: Understood.  

In the situation that led to this report, those arrays were buried deep within 
a Map<String, Object>, which contained many nested maps, collections, and 
arrays of various types.  I was building this data structure up in Java and 
wanted to convert it to JSON in one pass, so that solution would not have been 
practical for me.  

Original comment by michael.hixson@gmail.com on 3 Nov 2010 at 5:44

GoogleCodeExporter commented 9 years ago
Yup, good to know. One thing we should consider doing is using runtime types 
whenever "Object" is the static type.

Original comment by limpbizkit on 3 Nov 2010 at 5:54

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 4 Nov 2010 at 10:52

GoogleCodeExporter commented 9 years ago
Issue 245 has been merged into this issue.

Original comment by limpbizkit on 4 Nov 2010 at 10:53

GoogleCodeExporter commented 9 years ago
I have an inkling that this works in Gson 1.7.

We have a test for a Collection<Object>, Object[] and a Map<String,Object>.

Original comment by joel.leitch@gmail.com on 13 Apr 2011 at 9:16