Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Adjust Record adapter and extend test coverage #2224
Adjust Record adapter and extend test coverage #2224
Changes from 1 commit
0e407fb
7267193
e1f696c
55bbdca
f499528
e28ad83
2f444b2
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify: by default, static fields in records will be neither serialized nor deserialized, right? Only if that weird
excludeFieldsWithModifiers
method is called? Because I definitely don't think static fields should be serialized.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that is correct, see default excluded modifiers here:
gson/gson/src/main/java/com/google/gson/internal/Excluder.java
Line 55 in c2458bf
I have mainly added this to handle it in a reasonable way instead of failing with some obscure exception.
For static fields maybe there are legit (opt-in) use cases, e.g. to automatically encode a version number:
Though I am not sure if someone is actually using this, and it is certainly not obvious (and probably not even officially supported) to use
excludeFieldsWithModifiers
for this.If you want I can adjust it to also always exclude static fields on serialization, and we can then wait for someone to request this feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think by default static fields should not be serialized. I suppose they should be if someone calls
excludeFieldsWithModifiers(0)
, though even then I don't really understand why people would want that. I'm not really convinced by theVERSION
use case. We would serialize that field, and then what would we do on deserialization? Do we check that the version matches? Do we try to overwrite theVERSION
field? If we don't, isn't that inconsistent? I think if you really want a version then it should either be an instance field or you should use a customTypeAdapter
.(I looked at the history, and apparently
excludeFieldsWithModifiers
was already present in the first version on GitHub, dated 2008. So who knows why it was added. For what it's worth, Google's code has only a handful of calls to this method, and exactly one that is includingSTATIC
, perhaps accidentally.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be most useful for services which only serialize data, e.g. a REST API which generates the response. At least that is the most reasonable situation I can come up with at the moment.
It appears the
excludeFieldsWithModifiers
trick is known, see this StackOverflow answer.