AiorosXu / google-gson

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

Gson does not handle null values in list properly #83

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
my code:

Gson g=new Gson();      
List<String> t6=new ArrayList<String>();
t6.add("list 1");
t6.add(null);
t6.add("list 2");
String s6=g.toJson(t6);

trace:

java.lang.NullPointerException
    at 
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize
(DefaultTypeAdapters.java:414)
    at 
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize
(DefaultTypeAdapters.java:1)
    at com.google.gson.JsonSerializationVisitor.visitUsingCustomHandler
(JsonSerializationVisitor.java:132)
    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:96)
    at com.google.gson.JsonSerializationContextDefault.serialize
(JsonSerializationContextDefault.java:47)
    at com.google.gson.Gson.toJson(Gson.java:279)
    at com.google.gson.Gson.toJson(Gson.java:230)
    at com.google.gson.Gson.toJson(Gson.java:210)
    at com.aisino.platform.view.json.TestGson.testToJson
(TestGson.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody
(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected
(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected
(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod
(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run
(TestMethodRunner.java:45)
    at 
org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod
(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run
(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected
(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected
(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run
(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)

and  when I made some change on 
com.google.gson.DefaultTypeAdapters,then ,it is ok.

public JsonElement serialize(Collection src, Type typeOfSrc, 
JsonSerializationContext context) {
      if (src == null) {
        return JsonNull.createJsonNull();
      }
      JsonArray array = new JsonArray();
      Type childGenericType = null;
      if (typeOfSrc instanceof ParameterizedType) {
        childGenericType = new TypeInfoCollection(typeOfSrc).getElementType
();        
      }
      for (Object child : src) {
          if(child!=null){   // here, if null? 
        Type childType = (childGenericType == null || childGenericType == 
Object.class)
            ? child.getClass() : childGenericType;
        JsonElement element = context.serialize(child, childType);
        array.add(element);
          }else{   //  here, do when it is null
              array.add(null);
          }

      }
      return array;
    }

Original issue reported on code.google.com by iamj...@gmail.com on 25 Dec 2008 at 1:18

GoogleCodeExporter commented 9 years ago
What version of Gson are you using?

It appears that this bug was fixed in the 1.2 release.  This bug should not 
resurface
since we added a test to ensure "null"s in lists are properly serialized
(http://www.google.com/codesearch/p?hl=en#1W0hTXRl0vg/trunk/gson/src/test/java/c
om/google/gson/functional/CollectionTest.java&q=testNullsInListSerialization%20p
ackage:http://google-gson\.googlecode\.com).

A side note:
Inderjeet and I hope to release a new version of Gson, 1.3, before the new year 
(but
that is an aggressive goal).

Original comment by joel.leitch@gmail.com on 28 Dec 2008 at 2:21

GoogleCodeExporter commented 9 years ago
I test the Gson 1.3 release. But still has the problem,

java.lang.NullPointerException
    at 
com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.serialize
(DefaultTypeAdapters.java:414)

I also checked the URL and ran the CollectionTest.java ,but I found a different 
between your class and mine.
I use
               String s6=g.toJson(t6);
and the test class user 
              Type typeOfList = new TypeToken<List<String>>() {}.getType();
          String json = g.toJson(list, typeOfList);

That why mine failed.

Original comment by iamj...@gmail.com on 24 Jun 2009 at 5:12