You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JSONPointer should not always throw JSONPointerException #233
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.
changed the title [-]JSONPointer should not always throw JSONPointerException on failure to resolve[/-][+]JSONPointer should not always throw JSONPointerException [/+]on May 14, 2016
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.
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.
Activity
[-]JSONPointer should not always throw JSONPointerException on failure to resolve[/-][+]JSONPointer should not always throw JSONPointerException [/+]erosb commentedon May 14, 2016
The RFC says the followings:
In my understanding the current behaviour is correct.
stleary commentedon May 14, 2016
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.
johnjaylward commentedon May 15, 2016
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.
stleary commentedon May 15, 2016
Good idea. I think adding optQuery() methods to JSONObject and JSONArray would be a reasonable compromise. No change would be needed to JSONPointer.
fixing stleary#233
stleary commentedon May 16, 2016
Will be fixed in #234
Merge pull request #234 from erosb/master
Merge pull request #48 from erosb/master