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

Nullable Strings? #2

Closed
nikeee opened this issue Jun 10, 2016 · 0 comments
Closed

Nullable Strings? #2

nikeee opened this issue Jun 10, 2016 · 0 comments

Comments

@nikeee
Copy link
Contributor

nikeee commented Jun 10, 2016

Consider this JSON document:

{"foo": null }

The foo property is either a string or null.
When I try the following:

String json = "{\"foo\": null }";
JsonObject obj = new JsonObject().withValue(json);
String foo = obj.getString("foo");

I expected foo to be null, not "".

How to distinguish "foo": null from "foo": ""?
I looked up how org.json handles this case. As stated here, it behaves similar:
http://stackoverflow.com/questions/13118146

However, org.json provides an isNull method. Since there are no docs for the NetworkParser, I ended up looking in the source, ending up with nothing.

That leaves me with the question:
How to distinguish between "" and null using the NetworkParser library when using getString?

Edit:
I am currently using this workaround:

String nullable = (String)obj.get("foo");

However, considering this JSON document: {"foo": 5 }, the cast to String will fail because get() returns the int 5 (which seems good to me), whereas getString() returns "5" in this case.

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

2 participants