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

some std.json improvements #561

Merged
merged 2 commits into from
Jul 9, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 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,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"); }
Copy link
Member

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

body
{
return object[k];
}
}

/**
Expand Down Expand Up @@ -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");
Expand All @@ -249,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 @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

order is correct, but line should be changed to:

value.type = !isNegative && value.uinteger & (1UL << 63) ? JSON_TYPE.UINTEGER : JSON_TYPE.INTEGER;

it supposed to detect big unsigned numbers

}
break;

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