diff options
Diffstat (limited to 'libqpdf/JSON.cc')
-rw-r--r-- | libqpdf/JSON.cc | 78 |
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(), |