diff --git a/include/univalue.h b/include/univalue.h index e732e27b532a7b..4fd2223b302e7a 100644 --- a/include/univalue.h +++ b/include/univalue.h @@ -70,7 +70,8 @@ class UniValue { size_t size() const { return values.size(); } bool getBool() const { return isTrue(); } - bool checkObject(const std::map& memberTypes); + void getObjMap(std::map& kv) const; + bool checkObject(const std::map& 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); } diff --git a/lib/univalue.cpp b/lib/univalue.cpp index 97a38c9b97be69..d8ad7c4b90c6b1 100644 --- a/lib/univalue.cpp +++ b/lib/univalue.cpp @@ -156,6 +156,16 @@ bool UniValue::pushKVs(const UniValue& obj) return true; } +void UniValue::getObjMap(std::map& 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++) { @@ -168,7 +178,7 @@ bool UniValue::findKey(const std::string& key, size_t& retIdx) const return false; } -bool UniValue::checkObject(const std::map& t) +bool UniValue::checkObject(const std::map& t) const { if (typ != VOBJ) return false; diff --git a/test/object.cpp b/test/object.cpp index c47f1048b354b6..02446292a1aa4a 100644 --- a/test/object.cpp +++ b/test/object.cpp @@ -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 kv; + obj.getObjMap(kv); + BOOST_CHECK_EQUAL(kv["age"].getValStr(), "43"); + BOOST_CHECK_EQUAL(kv["name"].getValStr(), "foo bar"); + } static const char *json1 =