-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
some std.json improvements #561
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,15 +53,33 @@ 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; | ||
| } | ||
| /// 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]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -233,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"); | ||
|
|
@@ -249,6 +268,7 @@ JSONValue parseJSON(T)(T json, int maxDepth = -1) if(isInputRange!T) { | |
| if(c == '-') { | ||
| number.put('-'); | ||
| c = getChar(); | ||
| isNegative = true; | ||
| } | ||
|
|
||
| readInteger(); | ||
|
|
@@ -274,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; | ||
|
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. The order should be reversed here. If the leftmost bit is set, it's a signed integer. 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. order is correct, but line should be changed to:
it supposed to detect big unsigned numbers |
||
| } | ||
| break; | ||
|
|
||
|
|
@@ -376,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; | ||
|
|
||
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.
unusual indentation, I think "in" and "body" should be aligned