question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

User guide is wrong: Inner classes can now be serialized/deserialized?

See original GitHub issue
The user guide states:

https://sites.google.com/site/gson/gson-user-guide#TOC-Nested-Classes-including-
Inner-Classes-

"Gson can not automatically deserialize the pure inner classes since their 
no-args constructor also need a reference to the containing Object which is not 
available at the time of deserialization."

This is no longer true (I think because of UnsafeAllocator?), and should be 
documented. The example, which previously didn't work, now does. See the 
following:


public class A {
  public String a; 

  public class B {
    public String b; 

    public B() {
    }
  }

  public static void main(String[] arguments) {
    Gson gson = new Gson();
    B b = new A().new B();

    String json = gson.toJson(b);
    System.out.println("gson.toJson(" + b + ") = " + json);
    B b2 = gson.fromJson(json, B.class);
    System.out.println("gson.fromJson(..., B.class) = " + b2);
  }
}

The output of this is:

gson.toJson(co.mitro.core.server.data.A$B@7c97cb70) = {}
gson.fromJson(..., B.class) = co.mitro.core.server.data.A$B@b45ad3d

HOWEVER: If you try to reference the parent class from b2, you get a 
NullPointerException, which is fairly surprising and "dangerous." This needs to 
at least be documented! The debate about if this is the right default is a 
different discussion (I know that GsonBuilder.disableInnerClassSerialization() 
can turn this off)


Original issue reported on code.google.com by e...@evanjones.ca on 26 Sep 2013 at 2:27

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
remyvrscommented, Feb 27, 2018

My previous hack does not work for a tree where more than one instance of A is created. In case anyone needs it the solution applied by is : instead of
gsonBuilder.registerTypeAdapter(A.class, (InstanceCreator) type -> a); gsonBuilder.registerTypeAdapter(A.B.class, (InstanceCreator<A.B>) type -> a.new B());

I used :

	InnerClassInstanceCreator innerClassInstanceCreator = new InnerClassInstanceCreator();
	gsonBuilder.registerTypeAdapter(A.class, innerClassInstanceCreator);
	gsonBuilder.registerTypeAdapter(A.B.class, innerClassInstanceCreator);


private static class InnerClassInstanceCreator implements InstanceCreator<Object> {

	private A a;

	@Override
	public Object createInstance(Type type) {
		if (type.equals(A.class))
			return a = new A();
		else if (type.equals(A.B.class))
			return a.new B();
		else
			return null;
	}
}
1reaction
remyvrscommented, Feb 26, 2018

my hack was this :

GsonBuilder gsonBuilder = new GsonBuilder(); final A a = new A(); gsonBuilder.registerTypeAdapter(A.class, (InstanceCreator) type -> a); gsonBuilder.registerTypeAdapter(A.B.class, (InstanceCreator<A.B>) type -> a.new B()); gson = gsonBuilder.create();

clearly it works only when having in mind only one A instance …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support inner classes / non static member classes
A special serializer could be written for inner classes that would first deserialize the enclosing class reference, then construct the inner class with...
Read more >
SER05-J. Do not serialize instances of inner classes
Serialization of inner classes can introduce platform dependencies and can cause serialization of instances of the outer class. Rule. Severity. Likelihood.
Read more >
Serialization of inner class (Java) - Stack Overflow
Inner classes have an implicit constructor argument, the outer class instance. This doesn't work with Serialization the way you tried. Since you ...
Read more >
Java Serialization - Dos and Don'ts for Correct Serialization
1. Java serialization incompatible changes · Deleting fields – If a field is deleted in a class, the stream written will not contain...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Most of this article is about how to use the JsonSerializer API, ... Json can serialize or deserialize numbers represented by JSON strings ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found