From d80b63c3c0c43fd6bd9e275c7979ecde57d30264 Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 11 Feb 2023 17:46:30 +0000 Subject: Refactor JSON type checks --- include/qpdf/JSON.hh | 28 ++++++++++++++++++++++++++++ libqpdf/JSON.cc | 46 ++++++++++++++++++++++++---------------------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/include/qpdf/JSON.hh b/include/qpdf/JSON.hh index 64f3792c..28da3f24 100644 --- a/include/qpdf/JSON.hh +++ b/include/qpdf/JSON.hh @@ -339,13 +339,33 @@ class JSON static void writeClose(Pipeline* p, bool first, size_t depth, char const* delimeter); + enum value_type_e { + vt_none, + vt_dictionary, + vt_array, + vt_string, + vt_number, + vt_bool, + vt_null, + vt_blob, + }; + struct JSON_value { + JSON_value(value_type_e type_code) : + type_code(type_code) + { + } virtual ~JSON_value() = default; virtual void write(Pipeline*, size_t depth) const = 0; + const value_type_e type_code{vt_none}; }; struct JSON_dictionary: public JSON_value { + JSON_dictionary() : + JSON_value(vt_dictionary) + { + } virtual ~JSON_dictionary() = default; virtual void write(Pipeline*, size_t depth) const; std::map> members; @@ -353,6 +373,10 @@ class JSON }; struct JSON_array: public JSON_value { + JSON_array() : + JSON_value(vt_array) + { + } virtual ~JSON_array() = default; virtual void write(Pipeline*, size_t depth) const; std::vector> elements; @@ -383,6 +407,10 @@ class JSON }; struct JSON_null: public JSON_value { + JSON_null() : + JSON_value(vt_null) + { + } virtual ~JSON_null() = default; virtual void write(Pipeline*, size_t depth) const; }; diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc index 4370e4cd..aa9b6e41 100644 --- a/libqpdf/JSON.cc +++ b/libqpdf/JSON.cc @@ -133,6 +133,7 @@ JSON::JSON_array::write(Pipeline* p, size_t depth) const } JSON::JSON_string::JSON_string(std::string const& utf8) : + JSON_value(vt_string), utf8(utf8), encoded(encode_string(utf8)) { @@ -145,16 +146,19 @@ JSON::JSON_string::write(Pipeline* p, size_t) const } JSON::JSON_number::JSON_number(long long value) : + JSON_value(vt_number), encoded(std::to_string(value)) { } JSON::JSON_number::JSON_number(double value) : + JSON_value(vt_number), encoded(QUtil::double_to_string(value, 6)) { } JSON::JSON_number::JSON_number(std::string const& value) : + JSON_value(vt_number), encoded(value) { } @@ -166,6 +170,7 @@ JSON::JSON_number::write(Pipeline* p, size_t) const } JSON::JSON_bool::JSON_bool(bool val) : + JSON_value(vt_bool), value(val) { } @@ -183,6 +188,7 @@ JSON::JSON_null::write(Pipeline* p, size_t) const } JSON::JSON_blob::JSON_blob(std::function fn) : + JSON_value(vt_blob), fn(fn) { } @@ -376,56 +382,52 @@ JSON::makeBlob(std::function fn) bool JSON::isArray() const { - return nullptr != dynamic_cast(this->m->value.get()); + return m->value->type_code == vt_array; } bool JSON::isDictionary() const { - return nullptr != - dynamic_cast(this->m->value.get()); + return m->value->type_code == vt_dictionary; } bool JSON::getString(std::string& utf8) const { - auto v = dynamic_cast(this->m->value.get()); - if (v == nullptr) { - return false; + if (m->value->type_code == vt_string) { + auto v = dynamic_cast(this->m->value.get()); + utf8 = v->utf8; + return true; } - utf8 = v->utf8; - return true; + return false; } bool JSON::getNumber(std::string& value) const { - auto v = dynamic_cast(this->m->value.get()); - if (v == nullptr) { - return false; + if (m->value->type_code == vt_number) { + auto v = dynamic_cast(this->m->value.get()); + value = v->encoded; + return true; } - value = v->encoded; - return true; + return false; } bool JSON::getBool(bool& value) const { - auto v = dynamic_cast(this->m->value.get()); - if (v == nullptr) { - return false; + if (m->value->type_code == vt_bool) { + auto v = dynamic_cast(this->m->value.get()); + value = v->value; + return true; } - value = v->value; - return true; + return false; } bool JSON::isNull() const { - if (dynamic_cast(this->m->value.get())) { - return true; - } - return false; + return m->value->type_code == vt_null; } bool -- cgit v1.2.3-54-g00ecf