aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/JSON.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-01-19 15:31:28 +0100
committerJay Berkenbilt <ejb@ql.org>2022-01-30 19:11:03 +0100
commit37105710ee0b332a3020d4b3220c95b8f4267555 (patch)
treeee4b520b1c141033ba16ba3696def57fc90f60fa /libqpdf/JSON.cc
parenta6df6fdaf724ed5fc6f7e8c021f7804bd5a9c0e2 (diff)
downloadqpdf-37105710ee0b332a3020d4b3220c95b8f4267555.tar.zst
Implement JSONHandler for recursively processing JSON
Diffstat (limited to 'libqpdf/JSON.cc')
-rw-r--r--libqpdf/JSON.cc78
1 files changed, 78 insertions, 0 deletions
diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc
index 423c0b0a..af98553e 100644
--- a/libqpdf/JSON.cc
+++ b/libqpdf/JSON.cc
@@ -90,6 +90,7 @@ std::string JSON::JSON_array::unparse(size_t depth) const
}
JSON::JSON_string::JSON_string(std::string const& utf8) :
+ utf8(utf8),
encoded(encode_string(utf8))
{
}
@@ -312,6 +313,83 @@ JSON::isDictionary() const
}
bool
+JSON::getString(std::string& utf8) const
+{
+ auto v = dynamic_cast<JSON_string const*>(this->m->value.getPointer());
+ if (v == nullptr)
+ {
+ return false;
+ }
+ utf8 = v->utf8;
+ return true;
+}
+
+bool
+JSON::getNumber(std::string& value) const
+{
+ auto v = dynamic_cast<JSON_number const*>(this->m->value.getPointer());
+ if (v == nullptr)
+ {
+ return false;
+ }
+ value = v->encoded;
+ return true;
+}
+
+bool
+JSON::getBool(bool& value) const
+{
+ auto v = dynamic_cast<JSON_bool const*>(this->m->value.getPointer());
+ if (v == nullptr)
+ {
+ return false;
+ }
+ value = v->value;
+ return true;
+}
+
+bool
+JSON::isNull() const
+{
+ if (dynamic_cast<JSON_null const*>(this->m->value.getPointer()))
+ {
+ return true;
+ }
+ return false;
+}
+
+bool
+JSON::forEachDictItem(
+ std::function<void(std::string const& key, JSON value)> fn) const
+{
+ auto v = dynamic_cast<JSON_dictionary const*>(this->m->value.getPointer());
+ if (v == nullptr)
+ {
+ return false;
+ }
+ for (auto const& k: v->members)
+ {
+ fn(k.first, JSON(k.second));
+ }
+ return true;
+}
+
+bool
+JSON::forEachArrayItem(std::function<void(JSON value)> fn) const
+{
+ auto v = dynamic_cast<JSON_array const*>(this->m->value.getPointer());
+ if (v == nullptr)
+ {
+ return false;
+ }
+ for (auto const& i: v->elements)
+ {
+ fn(JSON(i));
+ }
+ return true;
+}
+
+bool
JSON::checkSchema(JSON schema, std::list<std::string>& errors)
{
return checkSchemaInternal(this->m->value.getPointer(),