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

Float nan values are generated in JSON #93

Open
PMunch opened this issue Dec 6, 2024 · 5 comments
Open

Float nan values are generated in JSON #93

PMunch opened this issue Dec 6, 2024 · 5 comments

Comments

@PMunch
Copy link

PMunch commented Dec 6, 2024

Had an issue recently where NaN values from Nim got written to JSON as nan. According to the official JSON spec nan is not a valid value in JSON. Not quite sure what the actual behaviour here should be. I see that jq parses such JSON fine, but Nims parseJson fails.

@treeform
Copy link
Owner

treeform commented Dec 6, 2024

NaN is not a valid number in json, so I will not parse it. If you are expecting to get NaNs some times you could make a parseHook and parse them yourself.

@PMunch
Copy link
Author

PMunch commented Dec 7, 2024

As it should be, but this issue is about jsony creating nans when converting an object to a JSON string

@PMunch
Copy link
Author

PMunch commented Dec 10, 2024

Here's a small reproduction:

import jsony

type m = object
  test: string
  field: float

echo m(test: "Hello world", field: Nan).toJson

which produces:

{"test":"Hello world","field":nan}

Notice how field in the JSON output has the illegal value of nan.

@pietroppeter
Copy link
Contributor

For reference in Python there is an allow_nan parameters that allows to output nans (and infinity values). If set to False it would raise an error when serializing an non number value. https://docs.python.org/3/library/json.html#basic-usage

@treeform
Copy link
Owner

Sorry this is indeed a real issue.

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