From 2d625ddc6f110f02e381298270c5df7ae6cc74e0 Mon Sep 17 00:00:00 2001 From: darkstalker Date: Tue, 1 May 2012 19:40:47 -0400 Subject: [PATCH 1/2] std.json: implement opIndex in struct JSONValue for accessing elements with array/hash syntax --- std/json.d | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/std/json.d b/std/json.d index edd586629d6..d42fe8fbf00 100644 --- a/std/json.d +++ b/std/json.d @@ -61,6 +61,22 @@ struct JSONValue { } /// Specifies the _type of the value stored in this structure. JSON_TYPE type; + + /// array syntax for json arrays + ref JSONValue opIndex(size_t i) + in { assert(type == JSON_TYPE.ARRAY, "json type is not array"); } + body + { + return array[i]; + } + + /// hash syntax for json objects + ref JSONValue opIndex(string k) + in { assert(type == JSON_TYPE.OBJECT, "json type is not object"); } + body + { + return object[k]; + } } /** From f8173c042547ca0b3c989d38ef63ae745869327f Mon Sep 17 00:00:00 2001 From: darkstalker Date: Tue, 1 May 2012 19:44:51 -0400 Subject: [PATCH 2/2] std.json: properly handle large ulong values (bigger than 2^63-1) store large ulong values in JSONValue.uinteger instead of throwing an overflow exception --- std/json.d | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/std/json.d b/std/json.d index d42fe8fbf00..6e49c515028 100644 --- a/std/json.d +++ b/std/json.d @@ -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 @@ -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; } @@ -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"); @@ -265,6 +268,7 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) { if(c == '-') { number.put('-'); c = getChar(); + isNegative = true; } readInteger(); @@ -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; @@ -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;