Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jainsahab
Copy link

@jainsahab jainsahab commented Jan 31, 2017

resolve #1005.
this change will make Gson capable of populating default values in Java models if the corresponding nodes are missing in Json.

@jainsahab jainsahab changed the title #1005 Gson default value handling resolve #1005 Gson default value handling Jan 31, 2017
@jainsahab jainsahab changed the title resolve #1005 Gson default value handling Gson default value handling Jan 31, 2017
@jainsahab
Copy link
Author

@inder123 @JakeWharton any feedback on this PR?

@drekbour

This comment has been minimized.

@FireController1847
Copy link

Wondering the status of this PR because I could really use this in my project right now

Copy link
Collaborator

@Marcono1234 Marcono1234 left a 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;
Copy link
Collaborator

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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
Copy link
Collaborator

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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return defaultValues.get(typeToken.getRawType());
return defaultValues.get(typeToken.getRawType());

Comment on lines +380 to +381
* 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.

Comment on lines +24 to +27
* 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
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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);
Copy link
Collaborator

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.

Comment on lines +236 to +238
Set<String> fieldNames = boundFields.keySet();
for (String fieldName : fieldNames) {
BoundField boundField = boundFields.get(fieldName);
Copy link
Collaborator

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;
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default Value for Missing fields in Json
4 participants