-
-
Notifications
You must be signed in to change notification settings - Fork 694
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f423ca
Enable std.json.parseJSON to parse nan/inf.
rcorre ef0b393
JSON: option to encode special floats as strings.
rcorre c33dca9
JSON: option to decode special strings as floats.
rcorre fecf9b3
JSON: Nan/Inf code review tweaks.
rcorre 51674c8
JSON: remove unused import std.typecons : Flag.
rcorre File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,22 @@ import std.conv; | |
import std.range.primitives; | ||
import std.array; | ||
import std.traits; | ||
import std.typecons : Flag; | ||
|
||
/** | ||
String literals used to represent special float values within JSON strings. | ||
*/ | ||
enum JSONFloatLiteral : string | ||
{ | ||
nan = "NaN", /// string representation of floating-point NaN | ||
inf = "Infinite", /// string representation of floating-point Infinity | ||
negativeInf = "-Infinite", /// string representation of floating-point negative Infinity | ||
} | ||
|
||
/** | ||
Flag that enables encoding special float values (NaN/Inf) as strings. | ||
*/ | ||
alias JsonSpecialFloats = Flag!"JsonSpecialFloats"; | ||
|
||
/** | ||
JSON type enumeration | ||
|
@@ -595,23 +611,26 @@ struct JSONValue | |
} | ||
|
||
/// Implicitly calls $(D toJSON) on this JSONValue. | ||
string toString() const | ||
/// $(I specialFloats) will enable encoding NaN/Inf as strings. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a param section.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also misses Params section.
|
||
string toString(in JsonSpecialFloats specialFloats = JsonSpecialFloats.no) const | ||
{ | ||
return toJSON(&this); | ||
return toJSON(&this, false, specialFloats); | ||
} | ||
|
||
/// Implicitly calls $(D toJSON) on this JSONValue, like $(D toString), but | ||
/// also passes $(I true) as $(I pretty) argument. | ||
string toPrettyString() const | ||
/// $(I specialFloats) will enable encoding NaN/Inf as strings. | ||
string toPrettyString(in JsonSpecialFloats specialFloats = JsonSpecialFloats.no) const | ||
{ | ||
return toJSON(&this, true); | ||
return toJSON(&this, true, specialFloats); | ||
} | ||
} | ||
|
||
/** | ||
Parses a serialized string and returns a tree of JSON values. | ||
*/ | ||
JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) | ||
JSONValue parseJSON(T)(T json, int maxDepth = -1, JsonSpecialFloats specialFloats = JsonSpecialFloats.no) | ||
if(isInputRange!T) | ||
{ | ||
import std.ascii : isWhite, isDigit, isHexDigit, toUpper, toLower; | ||
import std.utf : toUTF8; | ||
|
@@ -751,6 +770,22 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) | |
return str.data.length ? str.data : ""; | ||
} | ||
|
||
bool tryGetSpecialFloat(string str, out double val) { | ||
switch(str) { | ||
case JSONFloatLiteral.nan: | ||
val = double.nan; | ||
return true; | ||
case JSONFloatLiteral.inf: | ||
val = double.infinity; | ||
return true; | ||
case JSONFloatLiteral.negativeInf: | ||
val = -double.infinity; | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
void parseValue(JSONValue* value) | ||
{ | ||
depth++; | ||
|
@@ -804,8 +839,17 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) | |
break; | ||
|
||
case '"': | ||
auto str = parseString(); | ||
|
||
// if special float parsing is enabled, check if string represents NaN/Inf | ||
if (specialFloats && tryGetSpecialFloat(str, value.store.floating)) { | ||
// found a special float, its value was placed in value.store.floating | ||
value.type_tag = JSON_TYPE.FLOAT; | ||
break; | ||
} | ||
|
||
value.type_tag = JSON_TYPE.STRING; | ||
value.store.str = parseString(); | ||
value.store.str = str; | ||
break; | ||
|
||
case '0': .. case '9': | ||
|
@@ -905,15 +949,25 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) | |
return root; | ||
} | ||
|
||
/** | ||
Parses a serialized string and returns a tree of JSON values. | ||
*/ | ||
JSONValue parseJSON(T)(T json, JsonSpecialFloats specialFloats) | ||
if(isInputRange!T) | ||
{ | ||
return parseJSON!T(json, -1, specialFloats); | ||
} | ||
|
||
/** | ||
Takes a tree of JSON values and returns the serialized string. | ||
|
||
Any Object types will be serialized in a key-sorted order. | ||
|
||
If $(D pretty) is false no whitespaces are generated. | ||
If $(D pretty) is true serialized string is formatted to be human-readable. | ||
If $(D specialFloats) is JsonSpecialFloats.yes, encode special floats (NaN/Infinity) as strings. | ||
*/ | ||
string toJSON(in JSONValue* root, in bool pretty = false) | ||
string toJSON(in JSONValue* root, in bool pretty = false, in JsonSpecialFloats specialFloats = JsonSpecialFloats.no) | ||
{ | ||
auto json = appender!string(); | ||
|
||
|
@@ -1035,7 +1089,31 @@ string toJSON(in JSONValue* root, in bool pretty = false) | |
break; | ||
|
||
case JSON_TYPE.FLOAT: | ||
json.put(to!string(value.store.floating)); | ||
import std.math : isNaN, isInfinity; | ||
|
||
auto val = value.store.floating; | ||
|
||
if (val.isNaN) { | ||
if (specialFloats) { | ||
toString(JSONFloatLiteral.nan); | ||
} | ||
else { | ||
throw new JSONException( | ||
"Cannot encode NaN. Consider passing the specialFloats flag."); | ||
} | ||
} | ||
else if (val.isInfinity) { | ||
if (specialFloats) { | ||
toString((val > 0) ? JSONFloatLiteral.inf : JSONFloatLiteral.negativeInf); | ||
} | ||
else { | ||
throw new JSONException( | ||
"Cannot encode Infinity. Consider passing the specialFloats flag."); | ||
} | ||
} | ||
else { | ||
json.put(to!string(val)); | ||
} | ||
break; | ||
|
||
case JSON_TYPE.TRUE: | ||
|
@@ -1397,3 +1475,45 @@ EOF"; | |
auto e = collectException!JSONException(parseJSON(s)); | ||
assert(e.msg == "Unexpected character 'p'. (Line 5:3)", e.msg); | ||
} | ||
|
||
// handling of special float values (NaN, Inf, -Inf) | ||
unittest | ||
{ | ||
import std.math : isNaN, isInfinity; | ||
import std.exception : assertThrown; | ||
|
||
// expected representations of NaN and Inf | ||
enum { | ||
nanString = '"' ~ JSONFloatLiteral.nan ~ '"', | ||
infString = '"' ~ JSONFloatLiteral.inf ~ '"', | ||
negativeInfString = '"' ~ JSONFloatLiteral.negativeInf ~ '"', | ||
} | ||
|
||
// when passing JsonSpecialFloats.yes, encode NaN/Inf as strings | ||
assert(JSONValue(float.nan).toString(JsonSpecialFloats.yes) == nanString); | ||
assert(JSONValue(double.infinity).toString(JsonSpecialFloats.yes) == infString); | ||
assert(JSONValue(-real.infinity).toString(JsonSpecialFloats.yes) == negativeInfString); | ||
|
||
// when passing JsonSpecialFloats.no, thow on converting NaN/Inf | ||
assertThrown!JSONException(JSONValue(float.nan).toString); | ||
assertThrown!JSONException(JSONValue(double.infinity).toString); | ||
assertThrown!JSONException(JSONValue(-real.infinity).toString); | ||
|
||
// when parsing json with JsonSpecialFloats.yes, decode special strings as floats | ||
JSONValue jvNan = parseJSON(nanString, JsonSpecialFloats.yes); | ||
JSONValue jvInf = parseJSON(infString, JsonSpecialFloats.yes); | ||
JSONValue jvNegInf = parseJSON(negativeInfString, JsonSpecialFloats.yes); | ||
|
||
assert(jvNan.floating.isNaN); | ||
assert(jvInf.floating.isInfinity && jvInf.floating > 0); | ||
assert(jvNegInf.floating.isInfinity && jvNegInf.floating < 0); | ||
|
||
// when parsing json with JsonSpecialFloats.no, decode special strings as strings | ||
jvNan = parseJSON(nanString); | ||
jvInf = parseJSON(infString); | ||
jvNegInf = parseJSON(negativeInfString); | ||
|
||
assert(jvNan.str == JSONFloatLiteral.nan); | ||
assert(jvInf.str == JSONFloatLiteral.inf); | ||
assert(jvNegInf.str == JSONFloatLiteral.negativeInf); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 smallj
, so that you can writeJsonSpecialFloats.yes
orYes.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.