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

JsonObject.getXXX methods should have variant which does not require default value #43

Open
mpost opened this issue Feb 14, 2015 · 4 comments

Comments

@mpost
Copy link

mpost commented Feb 14, 2015

Having to supply a default value on the JsonObject.getXXX methods is sometimes a bit tedious. Often times you just want the string to be null or the boolean to be false. So there should be a variant where it is not required to provide a default value.

@ralfstx
Copy link
Owner

ralfstx commented Mar 1, 2015

If I understand correctly, your suggestion is to add methods like getInt(String), getBoolean(String), etc. to JsonObject. These methods would return a "default fallback value" if the object does not contain a member with the given name. The obvious default value would be the initial value of the respective type (0 for int, false for boolean, etc.).

Those methods would be ambiguous, as in case of returning the "default fallback value" you could not tell whether the member is present or not. For example, jsonObject.getInt("size") returning 0 could mean that the size was specified as 0, but also that the "size" attribute is missing in the JSON object.

Of course, the same is true for jsonObject.getInt("size", 0), but here it's explicit in the code that if the size is not present it is assumed to be 0. From my point of view, the savings are little compared to the risk of programming errors that would be introduced by the suggested API.

@mpost
Copy link
Author

mpost commented Mar 2, 2015

I follow your reasoning to limit the scope of this library. My use case is to serialize a json object to a regular java object where leaving values in their default state would be the expected deserialization outcome. Therefore i have a lot of redundant method parameters that pass in the default java value. If you think the feature is out of scope, feel free to close the issue.

@marceloverdijk
Copy link

@ralfstx Is there a reason JsonObject has not explicit has(String name) method to check if the member is present or not?

To check this now I have to do jsonObject.get("the_name") != null, while something like jsonObject.has("the_name) would be preferable.

@thekeenant
Copy link

@marceloverdijk I was surprised to find that there wasn't a has or contains. Seems to be in every other json library. You can also use names().contains(...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants