-
Notifications
You must be signed in to change notification settings - Fork 65
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
javax.json.JsonObject.isNull(String) contract broken #110
Comments
The annoying part with failing if the property is missing is that many JSON
ser/de libs allow to exclude null values at serialization time. The result
is that during deser all those keys will have no value in the json
document. Throwing every time an NPE and catching it on the other side
wouldn't be efficient at all from a performance point of view. Ideally we
probably would want to have to public methods (the second one would be like
the isNullOrAbsent that you propose). Also it feels surprising to have a
method called isNull that does throw NPE...
What is your opinion on this? Do you believe that it is a good call to
throw an NPE from isNull?
2017-03-23 10:14 GMT-07:00 Michele Vivoda <notifications@github.com>:
… Hi,
while testing some json parsing libraries I noticed that genson returns
true when passing an unexisting field name to
javax.json.JsonObject.isNull(String) while javadoc
<https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html#isNull-java.lang.String->
says that
throws NullPointerException if the specified name doesn't have any mapping
Digging in code I found
@OverRide
public boolean isNull(String name) {
JsonValue value = values.get(name);
return (JsonValue.NULL.equals(value) || value == null);
}
|| value == null should be removed however isNull is used also by impl of
other methods of same class: getString, getBoolean, getInt, for them the
current implementation is ok.
My proposed change is the following, with getString, getBoolean, getInt
calling isNullOrAbsent
@OverRide
public boolean isNull(String name) {
JsonValue value = values.get(name);
if (value==null) throw new NullPointerException("no mapping for " + name);
return (JsonValue.NULL.equals(value));
}
private boolean isNullOrAbsent(String name) {
JsonValue value = values.get(name);
return (JsonValue.NULL.equals(value) || value == null);
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#110>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADS_0Xq2VcrTa1xAvXLSs6fG7irG4Tw3ks5roqiTgaJpZM4MnFAx>
.
|
I think that if a json api is meant for high level clients that often don't need to distinguish between the two cases then a So yes maybe having |
Hi,
while testing some json parsing libraries I noticed that genson returns
true
when passing an unexisting field name tojavax.json.JsonObject.isNull(String)
while javadoc says thatDigging in code I found
|| value == null
should be removed howeverisNull
is used also by impl of other methods of same class:getString, getBoolean, getInt
, for them the current implementation is ok.My proposed change is the following, with
getString, getBoolean, getInt
callingisNullOrAbsent
The text was updated successfully, but these errors were encountered: