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

Negative numbers in JSON turn to zero in from_json() #334

Closed
totalgee opened this issue Mar 13, 2017 · 5 comments
Closed

Negative numbers in JSON turn to zero in from_json() #334

totalgee opened this issue Mar 13, 2017 · 5 comments

Comments

@totalgee
Copy link
Contributor

  • Compiler Used: Xcode 8.2.1 (clang-800.0.42.1)
  • Operating System: OS X 10.11.6
  • Architecture (ARM/x86/32bit/64bit/etc): 64bit
  • ChaiScript commit: b7e8897 (Dec 29, 2016)

When parsing a JSON file with negative numbers in it (floating point or integer), the negative numbers are converted to zeros.

Minimal Example to Reproduce Behavior

    auto negativeJson = "{ \"x\" : -8, \"y\" : -15, \"z\" : 2 }";
    auto negativeJsonFloat = to_json(["x": -0.08, "y": -15.1, "z": 1.73]);
    print("from_json(${negativeJson}) gives ${from_json(negativeJson)}");
    print("from_json(${negativeJsonFloat}) gives ${from_json(negativeJsonFloat)}");

The above example results in the following output:

from_json({ "x" : -8, "y" : -15, "z" : 2 }) gives [<x, 0>, <y, 0>, <z, 2>]
from_json({
  "x" : -0.080000,
  "y" : -15.100000,
  "z" : 1.730000
}) gives [<x, 0>, <y, 0>, <z, 1.73>]
@totalgee
Copy link
Contributor Author

Okay, I've got a fix for this, I'll submit a PR soon...

totalgee added a commit to totalgee/ChaiScript that referenced this issue Mar 14, 2017
- fix issue ChaiScript#334, where negative numbers loaded from JSON were being
  parsed as 0.
- add unit tests to cover these cases.
@totalgee
Copy link
Contributor Author

I'm going to submit another PR, you can choose which of the two solutions you prefer! (-; Probably you'll go for the smaller change, isolated just to JSON code, I'd guess -- unless you think the regular parse_num should handle negatives (obviously, except for this JSON case, it doesn't need to right now)...

totalgee added a commit to totalgee/ChaiScript that referenced this issue Mar 14, 2017
- fix issue ChaiScript#334, where negative numbers loaded from JSON were being
  parsed as 0.
- add unit tests to cover these cases.
@totalgee
Copy link
Contributor Author

Okay, I've closed #335, as it's probably best to keep this fix local to JSON parsing code. Better to use the fix in #336.

@lefticus
Copy link
Member

You are correct that chaiscript's number parser has to be different because of that unary - issue we discussed before. Or at least, it can be different, the parser should consume the - first. I agree at some point we need to merge these fixes, when I last looked at it there was subtle differences with C++ style numbers and JSON style.

@lefticus
Copy link
Member

I merged #336, closing this

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