-
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
Add TypeAdapter methods for custom property name conversion #1961
base: main
Are you sure you want to change the base?
Add TypeAdapter methods for custom property name conversion #1961
Conversation
Additionally some of the adapters implemented in TypeAdapters.java have been adjusted to override readFromPropertyName and createPropertyName to avoid the indirection over JsonTreeReader for better performance. Their implementation should be (nearly) the same as the previous implementation, even if that was not ideal.
// TODO: Throwing UnsupportedOperationException is not ideal because it 'bubbles up' | ||
// too far the call stack, but Gson has no better fitting exception |
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.
Not sure about this one
// TODO: Throwing UnsupportedOperationException is not ideal because it 'bubbles up' | ||
// too far the call stack, but Gson has no better fitting exception which also | ||
// fits for createPropertyName |
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.
Not sure about this one
@Override public String getPath() { | ||
// Does not have access to actual path, so at least make it obvious that | ||
// path is unknown | ||
return "#fake-property-name-path"; |
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.
This path and especially the "fake" might be a bit irritating for users; maybe just use <property-name>
or similar as path? That hopefully should make it obvious enough that this is not the actual name.
Resolves #127
Relates to / resolves #1722
Supersedes #1035
Currently Gson uses
String.valueOf
for Map key serialization and the regularTypeAdapter.read
method for deserialization. This is often not symmetric, preventing serialized data from being deserialized again, and is also not easily customizable by the user.This pull request adds the
TypeAdapter
methodscreatePropertyName(T)
andreadFromPropertyName(String)
. These allow the user to define how values should be converted to a property name and how it should be converted back. The default implementation of usingString.valueOf
was kept for backward compatibility.These methods have the following advantages:
Map
-like types can uses these methods as well, having a well defined way to convert Map keysMapTest.testCustomEnumMapKeyConversion()
.It might have been ideal if Gson added such methods in the past already so only types for which conversion to property names makes sense would have opted-in into the conversion. The current opt-out situation (especially with
String.valueOf
usage) is error-prone because users convert values to property names for which this was never intended or considered.The implementation of this pull request allowed removing the error-prone
promoteNameToValue
method (see also #1768). This comes at the cost that the default implementation ofreadFromPropertyName
creates a new JsonTreeReader wrapping the name as JsonPrimitive. This is not as performant, loses information about whether the original JsonReader was lenient and about the JSON path. The lenient setting is probably not that important because it would have only been used when the name was read as non-finite double. The missing JSON path can be worked around by having the caller catch the exceptions and add additional context, such as the JSON path. In general it is recommended that adapters which do want to support property name conversion should overridereadFromPropertyName
for better performance.An alternative would have been to use the signature
readFromPropertyName(JsonReader)
; this would solve these issues and would also allow streaming reading of the name (as proposed by #1604; though most likely rarely / never needed), but would require keepingpromoteNameToValue
and I think it would reduce usability becausereadFromPropertyName
will only be called for names, so onlynextName()
(and a few other methods) can be called; all other ones would always throw an IllegalStateException due to token mismatch. The approach of only passing aString
to the method seems less error-prone and easier to use to me.