Skip to content

Commit

Permalink
Add examples for (B/J)son.opIndex(Assign) and add missing overload of…
Browse files Browse the repository at this point in the history
… Json.opEquals.
  • Loading branch information
s-ludwig committed Apr 12, 2015
1 parent 1c22805 commit bee51bd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
24 changes: 24 additions & 0 deletions source/vibe/data/bson.d
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
55 changes: 42 additions & 13 deletions source/vibe/data/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit bee51bd

Please sign in to comment.