diff --git a/source/vibe/data/bson.d b/source/vibe/data/bson.d index 6f87e26252..94b1a7081e 100644 --- a/source/vibe/data/bson.d +++ b/source/vibe/data/bson.d @@ -545,6 +545,17 @@ struct Bson { m_data = newdata.data; } + /// + unittest { + Bson value = Bson.emptyObject; + value["a"] = 1; + value["b"] = true; + value["c"] = "foo"; + assert(value["a"] == Bson(1)); + assert(value["b"] == Bson(true)); + assert(value["c"] == Bson("foo")); + } + /** Allows index based access of a BSON array value. Returns a null value if the index is out of bounds. @@ -556,6 +567,19 @@ struct Bson { return Bson(null); } + /// + unittest { + Bson[] entries; + entries ~= Bson(1); + entries ~= Bson(true); + entries ~= Bson("foo"); + + Bson value = Bson(entries); + assert(value[0] == Bson(1)); + assert(value[1] == Bson(true)); + assert(value[2] == Bson("foo")); + } + /** Allows foreach iterating over BSON objects and arrays. diff --git a/source/vibe/data/json.d b/source/vibe/data/json.d index 33da1f7a10..74a431a5db 100644 --- a/source/vibe/data/json.d +++ b/source/vibe/data/json.d @@ -211,20 +211,10 @@ struct Json { @property Type type() const { return m_type; } /** - Check whether the JSON object contains the given key and if yes, - return a pointer to the corresponding object, otherwise return NULL. - */ - inout(Json*) opBinaryRight(string op : "in")(string key) inout { - checkType!(Json[string])(); - return key in m_object; - } - - /** - Allows direct indexing of array typed JSON values. + Clones a JSON value recursively. */ - ref inout(Json) opIndex(size_t idx) inout { checkType!(Json[])(); return m_array[idx]; } - - Json clone() const { + Json clone() + const { final switch (m_type) { case Type.undefined: return Json.undefined; case Type.null_: return Json(null); @@ -243,6 +233,32 @@ struct Json { } } + /** + Check whether the JSON object contains the given key and if yes, + return a pointer to the corresponding object, otherwise return NULL. + */ + inout(Json*) opBinaryRight(string op : "in")(string key) inout { + checkType!(Json[string])(); + return key in m_object; + } + + /** + Allows direct indexing of array typed JSON values. + */ + ref inout(Json) opIndex(size_t idx) inout { checkType!(Json[])(); return m_array[idx]; } + + /// + unittest { + Json value = Json.emptyArray; + value ~= 1; + value ~= true; + value ~= "foo"; + assert(value[0] == 1); + assert(value[1] == true); + assert(value[2] == "foo"); + } + + /** Allows direct indexing of object typed JSON values using a string as the key. @@ -276,6 +292,17 @@ struct Json { return m_object[key]; } + /// + unittest { + Json value = Json.emptyObject; + value["a"] = 1; + value["b"] = true; + value["c"] = "foo"; + assert(value["a"] == 1); + assert(value["b"] == true); + assert(value["c"] == "foo"); + } + /** Returns a slice of a JSON array. */ @@ -700,6 +727,8 @@ struct Json { /// ditto bool opEquals(bool v) const { return m_type == Type.bool_ && m_bool == v; } /// ditto + bool opEquals(int v) const { return m_type == Type.int_ && m_int == v; } + /// ditto bool opEquals(long v) const { return m_type == Type.int_ && m_int == v; } /// ditto bool opEquals(double v) const { return m_type == Type.float_ && m_float == v; }