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

Clarify implementation limits on NaN and Infinity #24

Open
ell1e opened this issue May 12, 2020 · 4 comments
Open

Clarify implementation limits on NaN and Infinity #24

ell1e opened this issue May 12, 2020 · 4 comments
Milestone

Comments

@ell1e
Copy link

ell1e commented May 12, 2020

I am thinking about adding support for JSON5 in a programming language that doesn't have NaN as a numeric value (operations that would produce it cause an exception instead). Is there any suggestion or experience of others in regards to what should I do in such a case with JSON5 that contains a NaN? Is there a name of a subset that is "JSON5 but not a NaN" that I could refer to so people don't get confused when I reject these values? I'm not sure what's the best course of action here

Sorry if this is not the right place to ask this, I'd be happy to close this and ask somewhere else!

@jordanbtucker
Copy link
Member

jordanbtucker commented May 12, 2020

Thanks for the question. If you have questions about how to interpret or implement the JSON5 specification, then you've come to the right place.

NaN and Infinity have always been a point of contention for me since I joined this project. I figured an issue like this would come up eventually.

It's okay to reject a JSON5 document that contains NaN if your platform doesn't support it. In the same way, it's okay to reject a JSON5 document that contains a number with more digits than your platform can represent, or a string so long, or a set of objects so deep, that it would overload the memory.

Alternatively, it would also be okay to just convert the offending JSON5 values to data that your platform can represent, possibly with a warning attached to it. For example, you could convert a large number to infinity, or truncate a large string, or stop nesting objects beyond a certain depth. See the Python and PHP implementations of JSON for examples.

While many JSON libraries have extended JSON to support NaN, there isn't really a precedent for platforms that don't support the features of JSON or JSON5. In the case of NaN, I'd probably just return whatever null is in your platform and produce a warning along with it (since JSON implementations typically convert NaN to null when encoding). Or perhaps you'll want a flag that users can give to the parse function that determines whether to throw, convert, or ignore NaN values.

In the end, it's really up to you. I hope that helps.

@jordanbtucker
Copy link
Member

Additionally, I think it's best to give developers the option to parse JSON5 documents even if they contain NaN. Often times, developers do not have control over what a JSON5 document contains, so flat out rejecting an entire document because it contains NaN, without offering an alternative, will probably not be a pleasant experience for developers.

@ell1e
Copy link
Author

ell1e commented May 13, 2020

Ok. In that case I would suggest to expand this:

An implementation may set limits on the size of texts that it accepts, the maximum depth of nesting, the range and precision of numbers, and the length and character contents of strings.

into something like:

An implementation may set limits on the size of texts that it accepts, the maximum depth of nesting, the range and precision of numbers as well as support of +inf/-inf/NaN, and the length and character contents of strings.

@jordanbtucker jordanbtucker added this to the v1.1.0 milestone May 13, 2020
@jordanbtucker jordanbtucker changed the title Question: what to do in a language that doesn't support NaN? Clarify implementation limits on NaN and Infinity May 13, 2020
@aseemk
Copy link
Member

aseemk commented May 15, 2020

(@jordanbtucker: just want to say you continue to kill it as a maintainer and steward of this project, and I appreciate that very much. Thank you! ❤️)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants