Skip to content

Commit

Permalink
std.json: properly handle large ulong values (bigger than 2^63-1)
Browse files Browse the repository at this point in the history
store large ulong values in JSONValue.uinteger instead of throwing an overflow exception
  • Loading branch information
wolfiestyle committed May 1, 2012
1 parent 2d625dd commit f8173c0
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions std/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum JSON_TYPE : byte {
/// Indicates the type of a $(D JSONValue).
STRING,
INTEGER, /// ditto
UINTEGER,/// integers > 2^63-1
FLOAT, /// ditto
OBJECT, /// ditto
ARRAY, /// ditto
Expand All @@ -52,10 +53,12 @@ struct JSONValue {
string str;
/// Value when $(D type) is $(D JSON_TYPE.INTEGER)
long integer;
/// Value when $(D type) is $(D JSON_TYPE.UINTEGER)
ulong uinteger;
/// Value when $(D type) is $(D JSON_TYPE.FLOAT)
real floating;
/// Value when $(D type) is $(D JSON_TYPE.OBJECT)
JSONValue[string] object;
JSONValue[string] object;
/// Value when $(D type) is $(D JSON_TYPE.ARRAY)
JSONValue[] array;
}
Expand Down Expand Up @@ -249,7 +252,7 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) {
case '0': .. case '9':
case '-':
auto number = appender!string();
bool isFloat;
bool isFloat, isNegative;

void readInteger() {
if(!isDigit(c)) error("Digit expected");
Expand All @@ -265,6 +268,7 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) {
if(c == '-') {
number.put('-');
c = getChar();
isNegative = true;
}

readInteger();
Expand All @@ -290,8 +294,11 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) {
value.floating = parse!real(data);
}
else {
value.type = JSON_TYPE.INTEGER;
value.integer = parse!long(data);
if (isNegative)
value.integer = parse!long(data);
else
value.uinteger = parse!ulong(data);
value.type = value.uinteger & (1UL << 63) ? JSON_TYPE.UINTEGER : JSON_TYPE.INTEGER;
}
break;

Expand Down Expand Up @@ -392,6 +399,10 @@ string toJSON(in JSONValue* root) {
json.put(to!string(value.integer));
break;

case JSON_TYPE.UINTEGER:
json.put(to!string(value.uinteger));
break;

case JSON_TYPE.FLOAT:
json.put(to!string(value.floating));
break;
Expand Down

0 comments on commit f8173c0

Please sign in to comment.