Skip to content

JSONPointer should not always throw JSONPointerException  #233

Closed

Description

@stleary

A user may want to evaluate a JSONPointer which is not expected to match the document. For example, when writing a test case to confirm that a specified item is not present. Having to catch exceptions makes this more difficult. It might be better if the JSONPointer query were to return null instead. I don't think there is a conflict with actual null values, since I believe they are stored as JSONObject.NULL.

Certain other cases may justify throwing an exception. For example, using a syntactically incorrect JSONPointer string.

I don't think the spec requires exceptions to be thrown, nor does it seem to require the same behavior for all types of errors.

All of the cases where JSONPointer throws an exception should be evaluated to determine if returning null might be better. Compatibility of JSONPointer returning null and JSONObject.NULL content should be confirmed.

Activity

  1. changed the title [-]JSONPointer should not always throw JSONPointerException on failure to resolve[/-] [+]JSONPointer should not always throw JSONPointerException [/+] on May 14, 2016
  2. erosb commented on May 14, 2016

    @erosb
    Contributor

    The RFC says the followings:

    In the event of an error condition, evaluation of the JSON Pointer
    fails to complete.

    Error conditions include, but are not limited to:

    o Invalid pointer syntax

    o A pointer that references a nonexistent value

    In my understanding the current behaviour is correct.

  3. stleary commented on May 14, 2016

    @stleary
    OwnerAuthor

    I am working from Effective Java, item 57 (Use exceptions only for exceptional conditions) and item 58 (Use checked exceptions for recoverable conditions and runtime exceptions for programming errors). A pointer not maching the document does not seem to rise to the level of exceptional condition or programming error. Besides, it seems burdensome to require the developer to wrap each query in a try/catch block in case the data does not match. No problem with the invalid syntax part.

  4. johnjaylward commented on May 15, 2016

    @johnjaylward
    Contributor

    The spec doesn't say how the error should be generated, just that it should error. I'm not sure if I'd consider retuning null constitutes an error, but it's how many other libraries indicate a "not found" status.

    Maybe a better solution would be to offer an "opt" getter like JSONObject and JSONArray currently offer. The normal getters throw an exception if not found, and opt returns either null or a default value.

  5. stleary commented on May 15, 2016

    @stleary
    OwnerAuthor

    Good idea. I think adding optQuery() methods to JSONObject and JSONArray would be a reasonable compromise. No change would be needed to JSONPointer.

  6. added a commit that references this issue on May 16, 2016
  7. stleary commented on May 16, 2016

    @stleary
    OwnerAuthor

    Will be fixed in #234

  8. added 2 commits that reference this issue on May 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions