From 37105710ee0b332a3020d4b3220c95b8f4267555 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Wed, 19 Jan 2022 09:31:28 -0500 Subject: Implement JSONHandler for recursively processing JSON --- libqpdf/JSON.cc | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'libqpdf/JSON.cc') 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)) { } @@ -311,6 +312,83 @@ JSON::isDictionary() const this->m->value.getPointer()); } +bool +JSON::getString(std::string& utf8) const +{ + auto v = dynamic_cast(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(this->m->value.getPointer()); + if (v == nullptr) + { + return false; + } + value = v->encoded; + return true; +} + +bool +JSON::getBool(bool& value) const +{ + auto v = dynamic_cast(this->m->value.getPointer()); + if (v == nullptr) + { + return false; + } + value = v->value; + return true; +} + +bool +JSON::isNull() const +{ + if (dynamic_cast(this->m->value.getPointer())) + { + return true; + } + return false; +} + +bool +JSON::forEachDictItem( + std::function fn) const +{ + auto v = dynamic_cast(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 fn) const +{ + auto v = dynamic_cast(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& errors) { -- cgit v1.2.3-54-g00ecf