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

Enable std.json.parseJSON to parse nan/inf. #3141

Merged
merged 5 commits into from
Apr 14, 2015
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
136 changes: 128 additions & 8 deletions std/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Member

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.

Copy link
Member

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,
}


/**
JSON type enumeration
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a param section.

Params:
    specialFloats = enable encoding of NaN/Inf as strings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also misses Params section.

/**
Parses a serialized string and returns a tree of JSON values.
A JSONException is thrown when the depth exceeds maxDepth.

Params:
    maxDepth = maximum depth of nesting allowed, -1 disables depth checking
    specialFloats = enable encoding of NaN/Inf as strings
*/

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;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}