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

Added object methods for methods returning primitives #744

Closed
wants to merge 1 commit into from

Conversation

davejbur
Copy link

@davejbur davejbur commented Jun 8, 2023

Added object methods for:

  • optBooleanObj (returns Boolean vice boolean)
  • optFloatObj (returns Float vice float)
  • optIntegerObj (returns Integer vice integer)
  • optLongObj (returns Long vice long)

The reason for doing so is that, sometimes, I want (e.g.) optInt to return null if a value isn't found (i.e. I want to do optInt(theKey,null)). However, that gives a compiler error <null> cannot be converted to int, since optInt needs to return an int. Hence adding an almost identical function that returns an Integer instead.

(I've finally worked out why, in a previous attempt at doing this, Github was trying to allege I'd changed the whole file...! GH was changing all the line endings for me instead of just leaving well alone.)

- optBooleanObj (returns Boolean vice boolean)
- optFloatObj (returns Float vice float)
- optIntegerObj (returns Integer vice integer)
- optLongObj (returns Long vice long)
@stleary
Copy link
Owner

stleary commented Jun 9, 2023

@davejbur Thanks for the pull request, this seems like a reasonable change.

  • Would you mind changing 'Obj' to 'Object', e.g. optFloatObject()?
  • When adding an API method to JSONObject, please add the corresponding method in JSONArray.
  • Please add unit tests for new methods.

@javadev
Copy link
Contributor

javadev commented Jun 9, 2023

I suggest these method names

getOptionalBooleanObject()
getOptionalFloatObject()
getOptionalIntegerObject()
getOptionalLongObject()

@spaffrath
Copy link
Contributor

@davejbur Can't this be done using the current API with optInt("x", (Integer) null) and so on?

@stleary
Copy link
Owner

stleary commented Jun 10, 2023

I believe optInt("x", (Integer) null) will throw a NullPointerException because the 2nd param is a primitive int.
But this code could work:

Integer i = null;
if (j.has("a")) {
    i = j.getInt("a");
}

@spaffrath
Copy link
Contributor

@stleary Yes, of course 👍

@davejbur
Copy link
Author

I suggest these method names

getOptionalBooleanObject()
getOptionalFloatObject()
getOptionalIntegerObject()
getOptionalLongObject()

Thanks - I was trying to keep in line with the optBoolean etc naming convention already in use.

@davejbur
Copy link
Author

Closing this as now dealt with by #753.

@davejbur davejbur closed this Jun 29, 2023
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.

None yet

4 participants