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

optJSONObject() add default value #610

Merged
merged 1 commit into from
Jun 29, 2021

Conversation

tilds
Copy link
Contributor

@tilds tilds commented Jun 23, 2021

I found myself often needing a version of optJSONObject where I could define my own default value as the 2nd argument (as you can on most of the other 'opt' methods).

The use case is when I need to access something that is nested deep in a json object, I currently have to check for null for every level of nesting, but if I could just set the default value to be and empty jsonobject instead of null, I could do something like this:
String nestedvalue = jsonObject.optJSONObject("key", new JSONObject()).optJSONObject("key", new JSONObject()).optJSONObject("key", new JSONObject()).optString("nestedvalue")

Let me know what you think :)

Copy link
Contributor

@johnjaylward johnjaylward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for the contribution.

@stleary
Copy link
Owner

stleary commented Jun 23, 2021

What problem does this code solve?
One of the opt() functions does not have a default return value param

Risks
Low

Changes to the API?
Adding a new API method

Will this require a new release?
No

Should the documentation be updated?
No, but the JavaDocs will be slightly out of date

Does it break the unit tests?
No, but some unit tests were repurposed. The originals will be restored later.

Was any code refactored in this commit?
No

Review status
APPROVED
Starting 3 day comment window

@stleary stleary merged commit 449ec87 into stleary:master Jun 29, 2021
@stleary stleary changed the title New JSONObject.optJSONObject method with defaultValue parameter optJSONObject() add default value Dec 5, 2021
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

Successfully merging this pull request may close these issues.

3 participants