-
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 default value handling #1006
base: main
Are you sure you want to change the base?
Conversation
@inder123 @JakeWharton any feedback on this PR? |
This comment has been minimized.
This comment has been minimized.
Wondering the status of this PR because I could really use this in my project right now |
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 a maintainer of this project, but maybe this review is helpful nonetheless.
@@ -16,24 +16,6 @@ | |||
|
|||
package com.google.gson; | |||
|
|||
import java.io.EOFException; |
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 be good, if possible, to not reorder imports since it makes spotting imports added by accident and similar rather difficult.
@@ -250,7 +252,8 @@ public Gson() { | |||
factories.add(jsonAdapterFactory); | |||
factories.add(TypeAdapters.ENUM_FACTORY); | |||
factories.add(new ReflectiveTypeAdapterFactory( | |||
constructorConstructor, fieldNamingStrategy, excluder, jsonAdapterFactory)); | |||
constructorConstructor, fieldNamingStrategy, excluder, jsonAdapterFactory, | |||
this.missingFieldHandlingStrategy)); |
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.missingFieldHandlingStrategy)); | |
missingFieldHandlingStrategy)); |
To be consistent maybe don't qualify using this
here
@@ -16,23 +16,24 @@ | |||
|
|||
package com.google.gson; | |||
|
|||
import com.google.gson.internal.$Gson$Preconditions; |
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.
Same as above, if possible don't reorder imports
private MissingFieldHandlingStrategy missingFieldHandlingStrategy = new MissingFieldHandlingStrategy() { | ||
@Override | ||
public Object handle(TypeToken typeToken, String fieldName) { | ||
return defaultValues.get(typeToken.getRawType()); |
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.
return defaultValues.get(typeToken.getRawType()); | |
return defaultValues.get(typeToken.getRawType()); |
* Configure Gson to register a default value for a type given which will be used when Gson didn't found any field's | ||
* value in JSON. |
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.
* Configure Gson to register a default value for a type given which will be used when Gson didn't found any field's | |
* value in JSON. | |
* Configure Gson to register a default value for a type given which will be used when Gson didn't find a value for a | |
* field in JSON. |
* This strategy is implemented by the user and should be passed to | ||
* {@link GsonBuilder#useMissingFieldHandlingStrategy(MissingFieldHandlingStrategy)} method | ||
* so that Gson can use it. | ||
* The {@link #handle(TypeToken, String)} is used by the Gson to determine the default value |
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.
Might have to format this using <p>
or <br>
* This strategy is implemented by the user and should be passed to | ||
* {@link GsonBuilder#useMissingFieldHandlingStrategy(MissingFieldHandlingStrategy)} method | ||
* so that Gson can use it. | ||
* The {@link #handle(TypeToken, String)} is used by the Gson to determine the default value |
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.
* The {@link #handle(TypeToken, String)} is used by the Gson to determine the default value | |
* The {@link #handle(TypeToken, String)} is used by Gson to determine the default value |
* @author Prateek Jain | ||
*/ | ||
public interface MissingFieldHandlingStrategy { | ||
Object handle(TypeToken type, String fieldName); |
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.
Would be good to document this method.
Set<String> fieldNames = boundFields.keySet(); | ||
for (String fieldName : fieldNames) { | ||
BoundField boundField = boundFields.get(fieldName); |
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.
Why not iterate over boundFields.values()
? Getting all keys and then getting their values seems rather complicated.
@@ -178,27 +185,76 @@ static boolean excludeField(Field f, boolean serialize, Excluder excluder) { | |||
} | |||
|
|||
static abstract class BoundField { | |||
boolean initialized; |
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.
Making BoundField mutable likely won't work since com.google.gson.Gson.typeTokenCache
is caching adapters. So if someone tries to deserialize the same class twice (e.g. nested or in an array).
You might have to move that logic to ReflectiveTypeAdapterFactory.Adapter.read(JsonReader)
instead.
resolve #1005.
this change will make Gson capable of populating default values in Java models if the corresponding nodes are missing in Json.