discomarathon / google-gson

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

Nested classes cannot be deserialized #84

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a nested class (inside a test case for instance)
2. Create Gson object
3. Try to deserlialize an instance of the nested class

What is the expected output? What do you see instead?
Expected output is successfull deserailization. Insted an exception is
thrown (No no-args constructor found for class blaablaa please use...)

What version of the product are you using? On what operating system?
1.2.3

Please provide any additional information below.

Original issue reported on code.google.com by miku.kop...@gmail.com on 27 Dec 2008 at 11:07

GoogleCodeExporter commented 9 years ago
We have several tests that thoroughly tests the expected behavior around inner 
classes. Is this a static inner class or a regular inner class? For regular 
inner 
classes, Gson would have no way of constructing the instance because of the 
needed 
reference to the outer class. You will need to register an InstanceCreator.

Please provide a code sample so that we can reproduce the bug. 

Original comment by inder123 on 27 Dec 2008 at 1:35

GoogleCodeExporter commented 9 years ago
On a somewhat related note, I have added the ability to exclude all "inner 
classes"
from serialization/deserialization in r350.

This is useful if you have many classes that contain an inner class, but you do 
not
want to write custom serializers for each of these classes.  This feature would 
help
the user above assuming that they do not want this inner class as part of the
serialization/deserializaiton and they have a default constructor (or Instance
Creator) for the outer class.  The latter assumption must be correct since it is
attempt to deserialize the field.

FYI, a "static nested class" is not an "inner class".

Original comment by joel.leitch@gmail.com on 28 Dec 2008 at 3:54

GoogleCodeExporter commented 9 years ago
This got me too - But Joel's comment about a "static nested class" vs "inner 
class"
gave me the clue I needed to solve the problem.

Even if you create a no-args constructor in your inner class it still throws an
exception saying it can't find one.

It might be worth mentioning in the User Guide that to use an inner class it 
needs to
be static or GSON won't be able to find the constructor. Alternatively can the 
code
that looks for the constructor in MappedObjectConstructor check for this 
somehow and
throw a more informative exception message?

Original comment by dirk.mcc...@gmail.com on 24 Jun 2009 at 3:22

GoogleCodeExporter commented 9 years ago
Changed the User Guide to highlight this fact a little more.  See the "Nested
Classes" section.

Original comment by joel.leitch@gmail.com on 29 Sep 2009 at 8:45

GoogleCodeExporter commented 9 years ago
Changed the User Guide to highlight this fact a little more.  See the "Nested
Classes" section.

Original comment by joel.leitch@gmail.com on 29 Sep 2009 at 8:45