mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 14:10:30 +03:00
std.json: Fixed issue 12969. opIndexAssign operator for JSONValue added. Changed order of elements in enum JSON_TYPE so NULL is a default type when JSONValue is not initialized
Signed-off-by: Uranuz <neuranuz@gmail.com>
This commit is contained in:
parent
b956f687bf
commit
f2bc851d7c
1 changed files with 49 additions and 3 deletions
52
std/json.d
52
std/json.d
|
@ -37,15 +37,15 @@ JSON type enumeration
|
||||||
enum JSON_TYPE : byte
|
enum JSON_TYPE : byte
|
||||||
{
|
{
|
||||||
/// Indicates the type of a $(D JSONValue).
|
/// Indicates the type of a $(D JSONValue).
|
||||||
STRING,
|
NULL,
|
||||||
|
STRING, /// ditto
|
||||||
INTEGER, /// ditto
|
INTEGER, /// ditto
|
||||||
UINTEGER,/// ditto
|
UINTEGER,/// ditto
|
||||||
FLOAT, /// ditto
|
FLOAT, /// ditto
|
||||||
OBJECT, /// ditto
|
OBJECT, /// ditto
|
||||||
ARRAY, /// ditto
|
ARRAY, /// ditto
|
||||||
TRUE, /// ditto
|
TRUE, /// ditto
|
||||||
FALSE, /// ditto
|
FALSE /// ditto
|
||||||
NULL /// ditto
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -349,6 +349,22 @@ struct JSONValue
|
||||||
return *enforce!JSONException(k in store.object,
|
return *enforce!JSONException(k in store.object,
|
||||||
"Key not found: " ~ k);
|
"Key not found: " ~ k);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Operator sets $(D value) for element of JSON object by $(D key)
|
||||||
|
/// If JSON value is null, then operator initializes it with object and then
|
||||||
|
/// sets $(D value) for it.
|
||||||
|
/// Throws $(D JSONException) if $(D type) is not $(D JSON_TYPE.OBJECT)
|
||||||
|
/// or $(D JSON_TYPE.NULL).
|
||||||
|
void opIndexAssign(T)(auto ref T value, string key)
|
||||||
|
{
|
||||||
|
enforceEx!JSONException(type == JSON_TYPE.OBJECT || type == JSON_TYPE.NULL,
|
||||||
|
"JSONValue must be object or null");
|
||||||
|
|
||||||
|
if( type == JSON_TYPE.NULL )
|
||||||
|
this = (JSONValue[string]).init;
|
||||||
|
|
||||||
|
store.object[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
void opIndexAssign(T)(T arg, size_t i)
|
void opIndexAssign(T)(T arg, size_t i)
|
||||||
{
|
{
|
||||||
|
@ -1190,3 +1206,33 @@ deprecated unittest
|
||||||
jv.type = JSON_TYPE.TRUE;
|
jv.type = JSON_TYPE.TRUE;
|
||||||
assert(jv.type == JSON_TYPE.TRUE);
|
assert(jv.type == JSON_TYPE.TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
// Bugzilla 12969
|
||||||
|
|
||||||
|
JSONValue jv;
|
||||||
|
jv["int"] = 123;
|
||||||
|
|
||||||
|
assert(jv.type == JSON_TYPE.OBJECT);
|
||||||
|
assert("int" in jv);
|
||||||
|
assert(jv["int"].integer == 123);
|
||||||
|
|
||||||
|
jv["array"] = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
|
assert(jv["array"].type == JSON_TYPE.ARRAY);
|
||||||
|
assert(jv["array"][2].integer == 3);
|
||||||
|
|
||||||
|
jv["str"] = "D language";
|
||||||
|
assert(jv["str"].type == JSON_TYPE.STRING);
|
||||||
|
assert(jv["str"].str == "D language");
|
||||||
|
|
||||||
|
jv["bool"] = false;
|
||||||
|
assert(jv["bool"].type == JSON_TYPE.FALSE);
|
||||||
|
|
||||||
|
assert(jv.object.length == 4);
|
||||||
|
|
||||||
|
jv = [5, 4, 3, 2, 1];
|
||||||
|
assert( jv.type == JSON_TYPE.ARRAY );
|
||||||
|
assert( jv[3].integer == 2 );
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue