discomarathon / google-gson

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

fromJson fails with SubClasses #51

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When I use a code like:

public class GsonTester
{
...
  class BagOfPrimitives {
    private int value1 = 1;
    private String value2 = "abc";
    private transient int value3 = 3;
  }
...
  public String getAObject()
  {
    BagOfPrimitives obj = new BagOfPrimitives();
    Gson gson = new Gson();
    String json = gson.toJson(obj); 
    BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);  
    return json;
  }
...
}

I can not use the standard "fromJson" method. Because the no-arg
constructor is not recognized.

Original issue reported on code.google.com by mmuelle...@googlemail.com on 30 Sep 2008 at 3:59

GoogleCodeExporter commented 9 years ago
Since BagOfPrimitives class in your case is an inner class (not a static inner 
class) 
it needs a reference to the parent class GsonTester to work. 

If your BagOfPrimitives was a static inner class, this will work without any 
issues. 
However, for the other case, you need to provide an instance creator since 
there is 
no way to constructor an inner class without a reference to the outer class to 
which 
it is bound.

Original comment by inder123 on 13 Oct 2008 at 7:27

GoogleCodeExporter commented 9 years ago
See r262 for an example of how non-static inner classes can be serialized and 
deserialized with Gson.

Original comment by inder123 on 13 Oct 2008 at 7:49