-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gson serializes anonymous inner class instance as "null" #2059
Comments
Gson intentionally does not support serialization of anonymous classes, see #298. However, I (personally) agree that the current behavior is pretty error-prone. Additionally there has been some discussion from the community that this restriction could be lifted. For example if an instance of an anonymous class is serialized and a custom type adapter exists for it (that is, reflection is not used), then serialization could work without any issues, same for serialization and deserialization of local classes. The whole class exclusion logic, which just reads or writes @eamonnmcmanus what do you think? Note that I am not a member of this project. |
it comes to an error when switch jackson to GSON.so why GSON does not support serializes anonymous inner class |
I haven't looked into this in detail, but it does seem as if the current situation is poor. For example, if I register a I see that |
To answer my earlier question, |
@eamonnmcmanus, maybe we should close this specific issue here as "not planned" because the example provided by the author uses an anonymous subclass of their custom Solving #1510 seems more reasonable, so that issue should not be closed, because it is about using custom adapters for anonymous classes. |
Gson version
2.8.9
Java
11
Description
Give a class definition:
The result of running this is "null":
This result of running this is as expected (
{ "discovery": { "foo":"bar" } }
):Expected behavior
Both outputs should be the same
This is very problematic, it means the serialization will fail unexpectedly and silently when somebody uses an inner class possibly in completely unrelated part of the code.
The text was updated successfully, but these errors were encountered: