Skip to content

Commit

Permalink
Merge pull request #561 from darkstalker/master
Browse files Browse the repository at this point in the history
some std.json improvements
  • Loading branch information
andralex committed Jul 9, 2012
2 parents 99671b3 + f8173c0 commit 865c7a6
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions std/json.d
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"); }
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;

This comment has been minimized.

Copy link
@wolfiestyle

wolfiestyle Jul 9, 2012

Contributor

there is a little mistake, line should be changed to:
value.type = !isNegative && value.uinteger & (1UL << 63) ? JSON_TYPE.UINTEGER : JSON_TYPE.INTEGER;

}
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

2 comments on commit 865c7a6

@wolfiestyle
Copy link
Contributor

Choose a reason for hiding this comment

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

there is a little mistake, line 301 needs to be changed to:

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

@andralex
Copy link
Member Author

Choose a reason for hiding this comment

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

@Darkstalker care to issue a pull request real quick? Thanks!

Please sign in to comment.