-
Notifications
You must be signed in to change notification settings - Fork 10
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
Proposal to remove Infinity
and NaN
#2
Comments
I disagree with JSON5 as syntactic sugar for JSON. It means forever keeping compatibility with with whatever design decision were made when JSON was standardised. I for one strongly disagreed with Crockford's decision to leave out NaN and comments, which are the main reasons I chose JSON5. NaN and slightly less so Infinity are incredibly useful for scientific computing. As a use case for Infinity I can think of serialising boundary conditions. As you said IEEE-754 is basically ubiquitous and so is the choice to represent an invalid number as NaN. Python's numpy and matplotlib nicely handle NaN but fail on I"m not the only one to disagree with the elision of NaN & Infinity, the python standard implementation of JSON serialises and interprets NaN by default. See https://docs.python.org/3/library/json.html#basic-usage. Python has a huge user base so decisions in it's standard don't come lightly.
|
@jordanbtucker: I'm sorry for missing your original post! But thanks for fantastic and thoughtful discussion to both you guys! I think your point about this being the sole exception to "everything in JSON5 can be represented in JSON" / "JSON5 is syntactic sugar" is a great argument. But @csiz's pointer to Python supporting I've been hesitant to add 🤔 |
Related: json5/json5#136 |
If you are looking for a real-world use-case: I use JSON for logging data. Basically my log-file is one JSON object per line, each object containing structured data. When the data for a log message contains variables from my code, I need to be able to write out any value a variable might hold. For double values, this currently means that I turn non-finite values into strings, but it would be more helpful if I could declare my log file structures as JSON5 and simply keep the type the same (number) and only writing out the correct value instead of switching the type for some values. |
@d-frey |
Leaving |
Note, number values out of the scope will be parse to ±Infinity as original JSON spec. That means JSON5 do not need support |
Support for Infinity and NaN is absolutely critical. Suppose you are using the newest and finest:
The data series contains |
I was referred to this issue and I would like to point out a discrepancy in the following text, which apparently is from the official JSON spec:
The interchange of information between programs (and thus machines) has become a major use-case of JSON. It's nice that it's human readable. But when I'm finished writing my programs, I rarely read or write JSON. As such, it is mind boggling why numbers are converted to base 10, if an exchange of the numbers in base 16 would be much more convenient for machines (notions for hexadecimal floats exist in C99 and Java, and it will take a microcontroller some serious effort to convert a float to base 10). When humans must read JSON, then it can be made pretty by some editor/viewer. Your favorite IDE could show JSON in a form that humans appreciate (this may not only include conversion to base 10 but also pretty printing and such). Furthermore, human beings are perfectly capable of using infinity. So why does the author of the above text dare to claim that we are not? Also, what is "agnostic" supposed to mean in this context anyway? As far as I know, agnostic means that, regarding a certain question, somebody does not commit to a point of view. However, JSON clearly forces us to use base 10 and it forbids certain values. It is certainly not number agnostic in the sense that it would allow me to provide my own number parser to deal with numbers in base 13, for example. Actually, the author did the exact opposite of what he claims: he limited JSON to some subset of number formats - maybe in some attempt to keep things "simple". If JSON seriously was not intended to exchange data between programs/machines, then we shouldn't use it as such. However, if that is a valid use-case, then let me tell your that my programs indeed need Also, if JSON5 is only intended to offer "syntax sugar" on top of JSON, then I agree that JSON5 shouldn't include |
@skoehler Thanks for your comments, but this issue was settled in September 2017. I pointed you to this issue to explain why |
The issue of what the spec says may be settled, but what toolmakers do with it isn't. It sounds like there would be a demand for JSON5 decoding that does not permit NaN or Inf±. Certainly if you advertise a drop-in API replacement for JSON parsing, it would be disturbing if the range of output from the replacement function was different from the range of the original. And you do advertise a drop-in API replacement: "The JSON5 API is compatible with the JSON API." Except that JSON5 can produce values with behavior completely unexpected by client code. As far as I can tell, this means JSON5 as documented is internally contradictory (that is, you can't be API compatible and support returning NaNs), and as a result, there are no conforming implementations. |
Yes, I think the output values changing is the big problem, also see my reasoning here. While I think having explicit literals for |
@nopdotcom This is getting away from the topic of this thread, but I would like to respond. JSON5 is not advertised as a drop-in replacement for JSON. The intention of keeping the API compatible with JSON is to make it easier for developers to transition to using JSON5. They don't need to learn a brand new API because the method signature is the same. Maybe the wording should be changed to "The JSON5 API is backward compatible with the JSON API" since JSON5 documents are backward compatible with JSON documents. Perhaps more clarifications are needed if developers are interpreting the sentence to mean that the two API's are interchangeable. Secondly, what determines whether a JSON5 library is a conforming implementation is whether it parses and generates documents that conform to the spec, not whether it conforms to any API. The On topic, there is clear support for NaN and Infinity, as indicated in this thread, despite the concerns some of us have, including myself. NaN and Infinity are artifacts that made it into spec. Implementers are welcome to create libraries that do not produce documents containing NaN and Infinity, however I advise them to still parse documents containing those values in order to fully conform to the spec. See #24 for my views on that. I'm not opposed to adding prose to the spec that discourages the use of NaN and Infinity due to interoperability concerns. In fact, I have several modifications planned for v1.1.0 of the spec. |
That is however, IMHO, a notable downside. I ended up skipping on it entirely, and going with "JSON with comments" instead even though it is so badly underspecced, pretty much only due to this. I just felt like it would be hard for me to do a good enough implementation in a lang with no NaN, and I couldn't enable it by default either for the JSON module to make it immediately available to a wider range of users due to that output difference, and then I decided on not bothering using it at all. Just something to think about, maybe. |
I might get a lot of flack for this, but I don't think
Infinity
andNaN
belong in JSON5. Let me address my reasons and try to preemptively address some concerns.Motivation
Number Implementation Agnosticism
JSON had a good reason for not including
Infinity
andNaN
. It took an implementation agnostic approach to numbers as stated in the official spec (PDF) on page ii:I agree with JSON on this matter to some extent, and by adding
Infinity
andNaN
, JSON5 becomes more dogmatic and expects all implementations to comply with IEEE-754 floating point values.Granted, IEEE-754 is almost ubiquitous, and so IEEE-754 is usually the best representation of JSON numbers, as mentioned in RFC 7159.
Minimal benefit
If you can build and demonstrate a use case for serializing
Infinity
, then you've probably invalidated my proposal. It seems likeInfinity
was added just because it's IEEE-754.It may be easier to build a use case for
NaN
since it represents an invalid numeric value and so brings with it type information. But JSON is typically produced and consumed with some form of schema in place, and if the schema expects a number and gets anull
, wouldNaN
really add any benefit?Compatibility with JSON
Every feature of JSON5 is compatible with JSON (in the sense that all values represented by JSON5 can be serialized as JSON without losing significant meaning) with the exception of
Infinity
andNaN
.The reason I feel that JSON5 needs to maintain backward compatible serialization with JSON is because I think JSON5 should be to JSON what Babel and CoffeeScript are to JavaScript—syntactic sugar. In other words, JSON5 transpiles to JSON.
JSON is a data interchange format, but many products are using it like a configuration file format, and those files often need to be modified by hand. JSON5 alleviates the pain points and fills in the gaps while maintaining compatibility with ES5.
If everyone (or just @aseemk) disagrees with me about whether JSON5 should just be a syntactically sweet version of JSON, then I will concede and continue to maintain this project with support for
Infinity
andNaN
and maybe include native support forDate
.Concerns
What about existing JSON5 files?
v1.0 of the spec would still include support for
Infinity
andNaN
, however they would be deprecated and removed from a future version.What would this mean for
Date
andRegExp
?Date
andRegExp
would never be natively supported by JSON5, however they would still get serialized as strings instringify
.If JSON5 is just syntactically sweetened JSON, then why not just use CSON?
I really like CSON, and it's especially great for projects written in CoffeeScript, but it's not so great for projects written in JavaScript.
To demonstrate, say you're writing an application that uses Highcharts, a JavaScript based charting library, and you have a chart like this one. Here's what the configuration looks like:
That looks like great candidate for storing your configuration in a
.json5
file. Better yet, if you want to make changes to your configuration and quickly test them out in JSFiddle, you can just copy and paste the JSON5—something you wouldn't be able to do with a CSON file unless you transpiled it to JSON or wrote your project in CoffeeScript.Discussion
Let me know what you think. All questions, comments, concerns, and kudos are welcome.
The text was updated successfully, but these errors were encountered: