2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Add new method JSON::getDictItem

This commit is contained in:
m-holger 2024-01-14 16:38:51 +00:00
parent 7315aa8c9b
commit a41b789995
3 changed files with 19 additions and 0 deletions

View File

@ -162,6 +162,8 @@ class JSON
QPDF_DLL
bool isNull() const;
QPDF_DLL
JSON getDictItem(std::string const& key) const;
QPDF_DLL
bool forEachDictItem(std::function<void(std::string const& key, JSON value)> fn) const;
QPDF_DLL
bool forEachArrayItem(std::function<void(JSON value)> fn) const;

View File

@ -410,6 +410,17 @@ JSON::isNull() const
return m->value->type_code == vt_null;
}
JSON
JSON::getDictItem(std::string const& key) const
{
if (auto v = dynamic_cast<JSON_dictionary const*>(m->value.get())) {
if (auto it = v->members.find(key); it != v->members.end()) {
return it->second;
}
}
return makeNull();
}
bool
JSON::forEachDictItem(std::function<void(std::string const& key, JSON value)> fn) const
{

View File

@ -78,6 +78,12 @@ test_main()
jmap.addDictionaryMember("empty_dict", JSON::makeDictionary());
jmap.addDictionaryMember("empty_list", JSON::makeArray());
jmap.addDictionaryMember("single", JSON::makeArray()).addArrayElement(JSON::makeInt(12));
std::string jm_str;
assert(jmap.getDictItem("b").getString(jm_str));
assert(!jmap.getDictItem("b2").getString(jm_str));
assert(!jstr2.getDictItem("b").getString(jm_str));
assert(jm_str == "a\tb");
check(
jmap,
"{\n"