mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 23:50:31 +03:00
Added an in operator to std.json.JSONValue. Tested when const/non-const
This commit is contained in:
parent
58112802ed
commit
f457e4781c
1 changed files with 12 additions and 0 deletions
12
std/json.d
12
std/json.d
|
@ -342,6 +342,13 @@ struct JSONValue
|
||||||
return store.object[k];
|
return store.object[k];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto opBinaryRight(string op : "in")(string k) const
|
||||||
|
{
|
||||||
|
enforceEx!JSONException(type == JSON_TYPE.OBJECT,
|
||||||
|
"JSONValue is not an object");
|
||||||
|
return k in store.object;
|
||||||
|
}
|
||||||
|
|
||||||
/// Implements the foreach $(D opApply) interface for json arrays.
|
/// Implements the foreach $(D opApply) interface for json arrays.
|
||||||
int opApply(int delegate(size_t index, ref JSONValue) dg)
|
int opApply(int delegate(size_t index, ref JSONValue) dg)
|
||||||
{
|
{
|
||||||
|
@ -878,6 +885,11 @@ unittest
|
||||||
assert(jv.type == JSON_TYPE.OBJECT);
|
assert(jv.type == JSON_TYPE.OBJECT);
|
||||||
assertNotThrown(jv.object);
|
assertNotThrown(jv.object);
|
||||||
assertNotThrown(jv["key"]);
|
assertNotThrown(jv["key"]);
|
||||||
|
assert("key" in jv);
|
||||||
|
assert("notAnElement" !in jv);
|
||||||
|
const cjv = jv;
|
||||||
|
assert("key" in cjv);
|
||||||
|
|
||||||
foreach(string key, value; jv)
|
foreach(string key, value; jv)
|
||||||
{
|
{
|
||||||
static assert(is(typeof(value) == JSONValue));
|
static assert(is(typeof(value) == JSONValue));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue