Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJson fails with SubClasses #51

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 2 comments
Closed

fromJson fails with SubClasses #51

GoogleCodeExporter opened this issue Mar 19, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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

  • Changed state: Accepted

@GoogleCodeExporter
Copy link
Author

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

  • Changed state: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant