-
Notifications
You must be signed in to change notification settings - Fork 21
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
JSON: handle required and missing fields #41
Conversation
…quired = true), otherwise handle missing properties like explicit "null" fields.
static native boolean isNull(Node node); | ||
|
||
@JSBody(params = { "node" }, script = "return node === undefined;") |
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.
IMO, it's better to use void 0
instead of undefined
.
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.
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;") |
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.
Please, remove node === undefined
check from this method, that's incorrect
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.
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())); |
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 guess you should check if property presents in JSON and only if it does, call field.set
.
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.
done!
…on a missing value, only set the field if the value isn't missing (review)
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.