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

Allow writing and reading null values #78

Merged
merged 1 commit into from
Oct 25, 2016
Merged

Conversation

ansman
Copy link
Contributor

@ansman ansman commented Oct 24, 2016

This closes #23, #50 and #67

writeMethod.addStatement("$N.beginObject()", jsonWriter);
for (Map.Entry<Property, FieldSpec> entry : adapters.entrySet()) {
Property prop = entry.getKey();
FieldSpec field = entry.getValue();

if (prop.nullable()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered using nullValue() instead of letting the adapter handle it but adapters should handle null values according to the documentation:

Type adapters should be prepared to read null from the stream and write it to the stream. Alternatively, they should use nullSafe() method while registering the type adapter with Gson.

@@ -392,12 +394,6 @@ public MethodSpec createReadMethod(ClassName className,
FieldSpec name = FieldSpec.builder(String.class, "_name").build();
readMethod.addStatement("$T $N = $N.nextName()", name.type, name, jsonReader);

// check if JSON field value is NULL
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing this has two resulting effects:

  1. If the resulting JSON contains a null value for a field that isn't nullable the serialization will fail.
  2. If the resulting JSON contains a null value for a primitive the serialization will fail.
  3. The adapters now get a chance to handle null values if they want to.

All of these seems like the behaviour you'd want.

@rharter
Copy link
Owner

rharter commented Oct 25, 2016

LGTM 👍

@rharter rharter merged commit 1c0995d into rharter:master Oct 25, 2016
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.

2 participants