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

UnsupportedOperationException: Not a string: null #103

Open
CLOVIS-AI opened this issue Jul 20, 2018 · 3 comments
Open

UnsupportedOperationException: Not a string: null #103

CLOVIS-AI opened this issue Jul 20, 2018 · 3 comments

Comments

@CLOVIS-AI
Copy link

Here's a small example of the problem:

JsonObject json = new JsonObject();
json.add("test", (String) null);
String content = json.getString("test", "default");

When running this, an UnsupportedOperationException is thrown. According to the doc of getString, however:

If this object does not contain a member with this name, the given default value is returned.

So I would expect that it would return the default value (since the value null is used to notify that there is no value).

The only way currently to bypass the exception is to set the code to:

String content = json.get("test").isNull() ? "default" : json.getString("test", "default");

which is a lot for not much (and it's badly-optimized because there are two get calls).

It would be nice if the methods would just return the default value if the content is null...

@CLOVIS-AI
Copy link
Author

Here is an example of a use-case, from reading the contents of a REST response:

JsonObject values = new Request(GET, "/users/" + ID + "/")
  .get()
  .asObject();

// Use the old value as default value: if nothing is specified, keep the old value
name =      values.getString("name", name);
avatar =    values.getString("avatar", avatar);
bio =       values.getString("bio", bio);
isBanned =  values.getBoolean("banned", isBanned);

This throws an exception because the server always sends empty fields when there is no value (eg. no bio -> there is a 'bio' field but its value is 'null').

Since "no value" and "value is null" is essentially the same (especially for Strings, in which you can specify "" to say 'empty'), it would be nice that this simple code would work instead of throwing exceptions.

@CLOVIS-AI
Copy link
Author

And in the case where you actually want to know if the object contains the literal null, you could still use json.get("test").isNull() so no feature is lost.

@CLOVIS-AI
Copy link
Author

This would make working with this API a lot easier, please at least consider it.

kgeilmann pushed a commit to kgeilmann/cucumber that referenced this issue Oct 26, 2020
This fix works around the limitation of minimal-json, that null as default is not handled (see ralfstx/minimal-json#103 (comment))
aurelien-reeves pushed a commit to cucumber/ci-environment that referenced this issue Nov 5, 2021
This fix works around the limitation of minimal-json, that null as default is not handled (see ralfstx/minimal-json#103 (comment))
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

No branches or pull requests

1 participant