User guide is wrong: Inner classes can now be serialized/deserialized?
See original GitHub issueThe 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:
- Created 9 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 :
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 …