Allow serialization of anonymous and local classes
See original GitHub issueWhat steps will reproduce the problem?
Gson gson = new Gson();
String json = gson.toJson(new Object() {
String url = "http://localhost:10080/live/list";
String[] names = new String[] {"dsc0001","dsc0002","dsc0003"};
});
System.out.println(json);
What is the expected output?
{"url":"http://localhost:10080/live/list","name":[{"dsc0001","dsc0002","dsc0003"
]}
What do you see instead?
blank line
What version of the product are you using? On what operating system?
1.6
Please provide any additional information below.
I understand that there are default exclusion strategies that prevent the
serialization of objects belonging to an anonymous or local class, but it would
be great if these could be bypassed to allow the above notation.
Original issue reported on code.google.com by robby...@gmail.com
on 17 Mar 2011 at 2:14
Issue Analytics
- State:
- Created 9 years ago
- Reactions:3
- Comments:17 (1 by maintainers)
Top Results From Across the Web
Is it possible to serialize anonymous class without outer class?
The answer is no. You cannot do that since Inner class will need outer class to be serialized. Also you ...
Read more >How to serialize anonymous classes - Jenkins
How to serialize anonymous classes ... will give you an overview of the class, and LineNumberTable entries will show you which lines in...
Read more >On Lambdas, Anonymous Classes and Serialization in Java
Serialization in Java is a mechanism by which objects can be marshaled to and from streams of bytes, allowing them to be sent...
Read more >SER05-J. Do not serialize instances of inner classes
Serializing an inner class declared in a non-static context that contains implicit non-transient references to enclosing class instances results in ...
Read more >Java Object Serialization Specification: 1 - System Architecture
Note - Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged ......
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
I see this closed, but I don’t see a reason. Can we reopen or provide clarification? It sounds like this thread seemed in overwhelming support for anonymous class serialization.
@mintern
It should work if you provide a type to the
toJson()
method:I think that this issue should be reopened, because it’s possible to serialize and deserialize an anonymous/local class object, if the class does not have its outer class reference
this$0
, for example, if declared in a static method (don’t think it has much practical use, though).