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

javax.json.JsonObject.isNull(String) contract broken #110

Open
eleumik opened this issue Mar 23, 2017 · 2 comments
Open

javax.json.JsonObject.isNull(String) contract broken #110

eleumik opened this issue Mar 23, 2017 · 2 comments

Comments

@eleumik
Copy link

eleumik commented Mar 23, 2017

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 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);
  }
@EugenCepoi
Copy link
Contributor

EugenCepoi commented Mar 23, 2017 via email

@eleumik
Copy link
Author

eleumik commented Mar 23, 2017

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 isNullOrAbsent is convenient and isNull is almost useless. For a low level client like a generic binding app it doesnt' make any difference as it will normally use only has()/containsKey() and get().

So yes maybe having isNull behave as isNullOrAbsent would be more convenient, however this behavior is specified and all javax.json implementations I tested behave as from the spec.

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

No branches or pull requests

2 participants