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

The validator thinks -0 is a string. not a number #25

Closed
coderextreme opened this issue Jan 27, 2016 · 11 comments
Closed

The validator thinks -0 is a string. not a number #25

coderextreme opened this issue Jan 27, 2016 · 11 comments

Comments

@coderextreme
Copy link

See attached CC.txt JSON document and Schema.txt JSON schema

CC.txt
Schema.txt

erosb added a commit that referenced this issue Jan 27, 2016
@erosb
Copy link
Contributor

erosb commented Jan 27, 2016

@coderextreme thanks for reporting it. I'm afraid I can't fix it, because this problem is caused by a bug of the underlying org.json API (more specifically the JSONTokener#nextValue() and/or the JSONObject#stringToValue()` methods).

I've tried to find a workaround for it, but it seems the parser notoriously parses the -0 literal as a string. Trying to convert the String instance as a Number would not make sense, because if I'd do it that way, then a schema with {"type" : "number"} would also accept the "-0" JSON string literal as a number (which is against the JSON Schema spec). That would mean fixing a bug by creating a new bug, obviously doesn't make sense.

edit: minimal snippet about my findings:

JSONObject input = new JSONObject("{\"prop\" : \"-0\"}");
System.out.println(input.get("prop") + " " + input.get("prop").getClass());
input = new JSONObject("{\"prop\" : -0}");
System.out.println(input.get("prop") + " " + input.get("prop").getClass());
input = new JSONObject("{\"prop\" : -0.0}");
System.out.println(input.get("prop") + " " + input.get("prop").getClass());

It prints:

-0 class java.lang.String
-0 class java.lang.String
-0.0 class java.lang.Double

So both -0 and "-0" are parsed into a String instance (while -0.0 is properly parsed as Double), therefore the validator cannot decide after parsing if the original literal was a string or a number.

@coderextreme
Copy link
Author

Can we use something besides JSONTokener?
On Jan 27, 2016 8:10 AM, "Bence Eros" notifications@github.com wrote:

@coderextreme https://github.com/coderextreme thanks for reporting it.
I'm afraid I can't fix it, because this problem is caused by a bug of the
underlying org.json API (more specifically the JSONTokener#nextValue()
and/or the JSONObject#stringToValue()` methods).

I've tried to find a workaround for it, but it seems the parser
notoriously parses the -0 literal as a string. Trying to convert the
String instance as a Number would not make sense, because if I'd do it
that way, then a schema with {"type" : "number"} would also accept the
"-0" JSON string literal as a number (which is against the JSON Schema
spec). That would mean fixing a bug by creating a new bug, obviously
doesn't make sense.


Reply to this email directly or view it on GitHub
#25 (comment)
.

@erosb
Copy link
Contributor

erosb commented Jan 27, 2016

Well I'm not going to reimplement the tokener from scratch. I'll look after how much time does it take to fix the org.json implementation.

@coderextreme
Copy link
Author

I was thinking of doing a JSON.parse() and working with that result, but
I'm not familiar with parsers in Java, just JavaScript.
On Jan 27, 2016 8:26 AM, "Bence Eros" notifications@github.com wrote:

Well I'm not going to reimplement the tokener from scratch. I'll look
after how much time does it take to fix the org.json implementation.


Reply to this email directly or view it on GitHub
#25 (comment)
.

@coderextreme
Copy link
Author

Or maybe we could switch to gson if it's better.
On Jan 27, 2016 8:29 AM, "John Carlson" yottzumm@gmail.com wrote:

I was thinking of doing a JSON.parse() and working with that result, but
I'm not familiar with parsers in Java, just JavaScript.
On Jan 27, 2016 8:26 AM, "Bence Eros" notifications@github.com wrote:

Well I'm not going to reimplement the tokener from scratch. I'll look
after how much time does it take to fix the org.json implementation.


Reply to this email directly or view it on GitHub
#25 (comment)
.

@coderextreme
Copy link
Author

I filed an issue here against org.json: stleary/JSON-java#187

@erosb
Copy link
Contributor

erosb commented Jan 27, 2016

As the most simple temporary solution I recommend using -0.0 in your JSON instead of -0

@coderextreme
Copy link
Author

I don’t control the JSON but I can make a recommendation.

John

On Jan 27, 2016, at 8:51 AM, Bence Eros notifications@github.com wrote:

As the most simple temporary solution I recommend using -0.0 in your JSON instead of -0


Reply to this email directly or view it on GitHub #25 (comment).

@stleary
Copy link

stleary commented Jan 28, 2016

A pull request containing the fix can be found here: stleary/JSON-java#188. Feel free to try it out and let us know whether it addresses the issue.

@coderextreme
Copy link
Author

Thanks. Our JSON passes through now (at least that area). We had over 6300 instances of -0 so this was pretty critical.

John

On Jan 27, 2016, at 10:42 PM, Sean Leary notifications@github.com wrote:

A pull request containing the fix can be found here: stleary/JSON-java#188 stleary/JSON-java#188. Feel free to try it out and let us know whether it addresses the issue.


Reply to this email directly or view it on GitHub #25 (comment).

@erosb
Copy link
Contributor

erosb commented Jan 28, 2016

@stleary how are the artifacts deployed to maven central? From my side, fixing this issue will only mean upgrading (in my pom.xml) to the version containing the bugfix.

@erosb erosb closed this as completed Mar 15, 2016
erosb added a commit that referenced this issue Mar 24, 2017
…C Licensed existing subject-valid.json file - see #80 for details
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

3 participants