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 classes

See original GitHub issue

Describe the feature Gson should allow serialization of anonymous classes, the reason is that the user shouldn’t care about the implementation of the code that generates the objects they are using.

For example, this code that only uses Guava and Gson looks fine and users may expect it to print ["a", "b"]:

        System.out.println(gson.toJsonTree(Sets.union(
                ImmutableSet.of("a"),
                ImmutableSet.of("b"))));

But actually, that code prints null, totally unexpected to a user that is not familiar with the implementation of Sets.union (that function returns an instance of an anonymous class).

Additional context

I think this feature is well deserved because of the amount of confusion that has been around the lack of it. If we do a Google search we find several people who were caught by this issue:

And the list goes on and on.

I think what aggravates the lack of this feature it he way Gson silently serializes those instances to null, which is a source of silent bugs.

NOTE: Deserialization of anonymous inner classes is problematic, I’m not asking for that to be supported. This feature request deals only with serialization.

Possible workarounds

I’ve seen suggested workarounds like:

gson.toJsonTree(union, TypeToken<Set<String>>(){}.getType());.

But notice that only works in the most simple of cases, but it doesn’t work in cases where we have a Map with values of different anonymous classes:

ImmutableMap.of(
    "key1", Sets.union(...),
    "key2", new HashSet(){},
    "key3", new MyRecord(){}
);

As there is not a single TokenType I can accommodate for that disparity of values and that will behave as expected. Moreover, sometimes the values are not known at compile time (in designing APIs, the values can be anything the user passes to us, and its out of our control).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:10
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
piegamesdecommented, Mar 11, 2020

If you don’t want to implement this, fine, but at least give an exception instead of silently serializing to null. I just lost way too much time debugging some weird NullPointerExceptions until I realized it was due to the use of anonymous classes.

I think deserialization should be possible as an opt-in through the use of custom type adapters.

2reactions
al6xcommented, Jul 22, 2020

There should be some option to tell Gson to include the fields of anonymous classes, something like toJson(obj, withAnonymousStuff = true).

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
NotSerializableException on anonymous class - java
You can declare anonymous class serializable, but the class is only then really serializable, if all its fields are serializable ...
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 >
Anonymous classes | Mastering PHP 7 - Packt Subscription
However, anonymous classes cannot be serialized. Trying to serialize an instance of an anonymous class, as shown in the following code snippet, ...
Read more >
Serialized anonymous classes using JsonUtility - Unity Forum
Most serializers require the class that is serialized to be marked with the System.Serializable attribute. Anonymous classes do not have this ...
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