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.

Allow serialization of anonymous and local classes

See original GitHub issue
What 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:closed
  • Created 9 years ago
  • Reactions:3
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
codebloodedcommented, Jul 21, 2016

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.

4reactions
lyubomyr-shaydarivcommented, Oct 1, 2018

@mintern

This is especially problematic with Java 8. I’ve tried to create a TypeAdapterFactory for serializing Streams, but my create method never even gets called because ReferencePipeline$2 is anonymous, so the Excluder just returns null.

It should work if you provide a type to the toJson() method:

... = gson.toJson(stringStream, new TypeToken<Stream<String>>() {}.getType());

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).

Read more comments on GitHub >

github_iconTop 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 >

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