Skip to content

Commit

Permalink
Add getObjMap() helper method. Also, constify checkObject().
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarzik committed Aug 3, 2017
1 parent ceb1194 commit ba341a2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class UniValue {
size_t size() const { return values.size(); }

bool getBool() const { return isTrue(); }
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes);
void getObjMap(std::map<std::string,UniValue>& kv) const;
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
const UniValue& operator[](const std::string& key) const;
const UniValue& operator[](size_t index) const;
bool exists(const std::string& key) const { size_t i; return findKey(key, i); }
Expand Down
12 changes: 11 additions & 1 deletion lib/univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ bool UniValue::pushKVs(const UniValue& obj)
return true;
}

void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const
{
if (typ != VOBJ)
return;

kv.clear();
for (size_t i = 0; i < keys.size(); i++)
kv[keys[i]] = values[i];
}

bool UniValue::findKey(const std::string& key, size_t& retIdx) const
{
for (size_t i = 0; i < keys.size(); i++) {
Expand All @@ -168,7 +178,7 @@ bool UniValue::findKey(const std::string& key, size_t& retIdx) const
return false;
}

bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t)
bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t) const
{
if (typ != VOBJ)
return false;
Expand Down
8 changes: 8 additions & 0 deletions test/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ BOOST_AUTO_TEST_CASE(univalue_object)
obj.pushKV("age", uv);
BOOST_CHECK_EQUAL(obj.size(), 1);
BOOST_CHECK_EQUAL(obj["age"].getValStr(), "43");

obj.pushKV("name", "foo bar");

std::map<std::string,UniValue> kv;
obj.getObjMap(kv);
BOOST_CHECK_EQUAL(kv["age"].getValStr(), "43");
BOOST_CHECK_EQUAL(kv["name"].getValStr(), "foo bar");

}

static const char *json1 =
Expand Down

0 comments on commit ba341a2

Please sign in to comment.