Skip to content

JSON: handle required and missing fields #41

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

Merged

Conversation

kris-scheibe
Copy link
Contributor

This PR adds functionality to handle missing and required properties when deserializing JSON.

This adds an explicit exception when a required property is missing and otherwise treats all missing fields like "null" fields.

…quired = true), otherwise handle missing properties like explicit "null" fields.
static native boolean isNull(Node node);

@JSBody(params = { "node" }, script = "return node === undefined;")
Copy link
Owner

Choose a reason for hiding this comment

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

IMO, it's better to use void 0 instead of undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

@@ -32,9 +32,12 @@
@JSBody(params = { "node" }, script = "return typeof node == 'string';")
static native boolean isString(Node node);

@JSBody(params = { "node" }, script = "return node === null;")
@JSBody(params = { "node" }, script = "return node === null || node === undefined;")
Copy link
Owner

Choose a reason for hiding this comment

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

Please, remove node === undefined check from this method, that's incorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, but now I had to add a check to not run the serializer for nullable values on the missing value

@@ -498,11 +498,27 @@ private void emitField(PropertyInformation property, Value<Object> target,
Type type = javaField.getGenericType();

String propertyName = property.outputName;
Value<Node> jsonValue = emit(() -> node.get().get(propertyName));
Value<Node> jsonValue = readProperty(property, node, propertyName);
Value<Object> value = convert(jsonValue, context, type, field);
emit(() -> field.set(target.get(), value.get()));
Copy link
Owner

Choose a reason for hiding this comment

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

I guess you should check if property presents in JSON and only if it does, call field.set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

…on a missing value, only set the field if the value isn't missing (review)
@konsoletyper konsoletyper merged commit fe31a19 into konsoletyper:master Jan 27, 2020
@kris-scheibe kris-scheibe deleted the json-required-and-missing branch January 27, 2020 20:18
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