-
Notifications
You must be signed in to change notification settings - Fork 103
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
Conversation
writeMethod.addStatement("$N.beginObject()", jsonWriter); | ||
for (Map.Entry<Property, FieldSpec> entry : adapters.entrySet()) { | ||
Property prop = entry.getKey(); | ||
FieldSpec field = entry.getValue(); | ||
|
||
if (prop.nullable()) { |
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.
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 |
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.
Removing this has two resulting effects:
- If the resulting JSON contains a
null
value for a field that isn't nullable the serialization will fail. - If the resulting JSON contains a
null
value for a primitive the serialization will fail. - The adapters now get a chance to handle
null
values if they want to.
All of these seems like the behaviour you'd want.
This closes rharter#23, rharter#50 and rharter#67
LGTM 👍 |
This closes #23, #50 and #67