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
Enable std.json.parseJSON to parse nan/inf. #3141
Conversation
JSONValue.toString will output 'nan', 'inf', or '-inf', when it has type JSON_TYPE.FLOAT and value double.nan/double.infinity/-double.infinity. However, parseJSON would fail on encountering these values in a string. This patch enables parseJSON to read special float values.
|
Fair enough, they aren't part of the official standard. However, I could at least see a use case for serializing |
I think We could let I'd lean towards letting them throw, though. I think it's less surprising than getting strings back. |
Someone on the bugzilla issue linked this go discussion that mentions a part of the json spec where it specifies that NaN and Inf should be represented as |
If we decide to throw, I have implemented that at (see rcorre@d2c1665). |
Here's the (dead-)linked specification: http://www.ecma-international.org/ecma-262/5.1/ The quotes are from section 15.12.3 which describes a So we'd be in the good company of ECMAScript itself if we'd map
I'm against adding new keywords to the JSON output (like Generating JSON strings (like I'd still prefer to reject the special values, though. When I have to be aware of |
Yeah, I agree here. |
It's a separate option to enable this for the |
It think the best option here would be to make it configurable. |
Adds the SpecialFloats flag to JSONValue.toString, JSONValue.toPrettyString, and JSONValue.toJSON. Given SpecialFloats.yes, the special float values NaN, Infinity, and -Infinity will be encoded as the strings "NaN", "Infinite", "-Infinite". Given SpecialFloats.no, attempting to stringify a JSONValue containing a special float value will throw. This overrides the previous behavior of encoding special float values as non-string literals, which resulted in non-standard JSON strings.
Passing JsonSpecialFloats.yes to parseJSON prompts the parser to interpret "NaN", "Infinite", and "-Infinite" as the special floating-point values they represent. Adds the overload parseJSON(JSONValue, SpecialFloats) that takes a JSONValue and the JsonSpecialFloats flag so users do not have to pass maxDepth in order to specify special float handling behavior.
I've updated the PR with optional encoding/decoding of special float values (defaults to 'no'). |
We hardly found a review manager yet. Better to fix things now than prolonging it in favor of something else to come. |
/** | ||
Flag that enables encoding special float values (NaN/Inf) as strings. | ||
*/ | ||
alias JsonSpecialFloats = Flag!"JsonSpecialFloats"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention is alias JsonSpecialFloats = Flag!"jsonSpecialFloats";
, not the small j
, so that you can write JsonSpecialFloats.yes
or Yes.jsonSpecialFloats
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better if we start with an orable enum here, because it easily allowed to add more options without changing the API.
enum JsonOptions
{
none,
specialFloatLiterals = 0x1,
}
Looking good, thanks. |
Replace Flag!"JsonSpecialFloats" with a flagset enum to allow future extensibility. Add more documentation to modified functions.
Updated docs and replaced |
Auto-merge toggled on |
Enable std.json.parseJSON to parse nan/inf.
Thx |
JSONValue.toString
outputs 'nan', 'inf', or '-inf', when it has typeJSON_TYPE.FLOAT
and value double.nan/double.infinity/-double.infinity.However,
parseJSON
would fail on encountering these values in a string.This patch enables
parseJSON
to read special float values.I submitted this as issue #14399 a bit ago and then decided to take a shot at fixing it.
To be clear: this does not change the output of
std.json.toString
, which was already outputting special values for nan/inf. It simply allowsstd.json
to read the special values that it outputs.