Skip to content

Commit

Permalink
Merge pull request toffaletti#19 from chipdude/master
Browse files Browse the repository at this point in the history
autovivify on set(), tweak creation tests
  • Loading branch information
toffaletti committed Apr 30, 2012
2 parents f366c87 + dac553d commit 3da40f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
23 changes: 19 additions & 4 deletions include/ten/json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ class json {
typedef void (json::*unspecified_bool_type)() const;
void true_value() const {}

// autovivify array or object
json_t *_o_get() {
if (!_p)
_p.take(json_object());
return get();
}
json_t *_a_get() {
if (!_p)
_p.take(json_array());
return get();
}

public:
// construction and assignment,
// including a selection of conversions from basic types
Expand Down Expand Up @@ -196,7 +208,10 @@ class json {
// equivalence

friend bool operator == (const json &lhs, const json &rhs) {
return json_equal(lhs._p.get(), rhs._p.get());
// jansson doesn't think null pointers are equal
auto jl = lhs._p.get();
auto jr = rhs._p.get();
return (!jl && !jr) || json_equal(jl, jr);
}
friend bool operator != (const json &lhs, const json &rhs) {
return !(lhs == rhs);
Expand Down Expand Up @@ -252,8 +267,8 @@ class json {
size_t osize() const { return json_object_size( get()); }
json get( const char *key) { return json(json_object_get( get(), key)); }
json get( const string &key) { return json(json_object_get( get(), key.c_str())); }
bool set( const char *key, const json &j) { return !json_object_set( get(), key, j.get()); }
bool set( const string &key, const json &j) { return !json_object_set( get(), key.c_str(), j.get()); }
bool set( const char *key, const json &j) { return !json_object_set( _o_get(), key, j.get()); }
bool set( const string &key, const json &j) { return !json_object_set( _o_get(), key.c_str(), j.get()); }
bool erase( const char *key) { return !json_object_del( get(), key); }
bool erase( const string &key) { return !json_object_del( get(), key.c_str()); }
bool oclear() { return !json_object_clear( get()); }
Expand All @@ -264,7 +279,7 @@ class json {
size_t asize() const { return json_array_size( get()); }
json get( int i) { return json(json_array_get( get(), i)); }
json get( size_t i) { return json(json_array_get( get(), i)); }
bool set( size_t i, const json &j) { return !json_array_set( get(), i, j.get()); }
bool set( size_t i, const json &j) { return !json_array_set( _a_get(), i, j.get()); }
bool insert( size_t i, const json &j) { return !json_array_insert( get(), i, j.get()); }
bool erase( size_t i) { return !json_array_remove( get(), i); }
bool arr_clear() { return !json_array_clear( get()); }
Expand Down
6 changes: 3 additions & 3 deletions tests/test_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,17 @@ BOOST_AUTO_TEST_CASE(json_conversions) {
}

BOOST_AUTO_TEST_CASE(json_create) {
json obj1{};
BOOST_CHECK(obj1);
json obj1;
obj1.set("test", "set");
BOOST_CHECK(obj1);
BOOST_CHECK(obj1.get("test"));
json root{
{"obj1", obj1}
};
BOOST_CHECK_EQUAL(root.get("obj1"), obj1);
obj1.set("this", "that");
json obj2{};
BOOST_CHECK_EQUAL(root.get("obj1").get("this").str(), "that");
json obj2{ {"answer", 42} };
obj1.set("obj2", obj2);
BOOST_CHECK_EQUAL(root.get("obj1").get("obj2"), obj2);
}
Expand Down

0 comments on commit 3da40f8

Please sign in to comment.