mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-02 22:50:20 +00:00
Refactor JSON type checks
This commit is contained in:
parent
72bf719772
commit
d80b63c3c0
@ -339,13 +339,33 @@ class JSON
|
|||||||
static void
|
static void
|
||||||
writeClose(Pipeline* p, bool first, size_t depth, char const* delimeter);
|
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
|
struct JSON_value
|
||||||
{
|
{
|
||||||
|
JSON_value(value_type_e type_code) :
|
||||||
|
type_code(type_code)
|
||||||
|
{
|
||||||
|
}
|
||||||
virtual ~JSON_value() = default;
|
virtual ~JSON_value() = default;
|
||||||
virtual void write(Pipeline*, size_t depth) const = 0;
|
virtual void write(Pipeline*, size_t depth) const = 0;
|
||||||
|
const value_type_e type_code{vt_none};
|
||||||
};
|
};
|
||||||
struct JSON_dictionary: public JSON_value
|
struct JSON_dictionary: public JSON_value
|
||||||
{
|
{
|
||||||
|
JSON_dictionary() :
|
||||||
|
JSON_value(vt_dictionary)
|
||||||
|
{
|
||||||
|
}
|
||||||
virtual ~JSON_dictionary() = default;
|
virtual ~JSON_dictionary() = default;
|
||||||
virtual void write(Pipeline*, size_t depth) const;
|
virtual void write(Pipeline*, size_t depth) const;
|
||||||
std::map<std::string, std::shared_ptr<JSON_value>> members;
|
std::map<std::string, std::shared_ptr<JSON_value>> members;
|
||||||
@ -353,6 +373,10 @@ class JSON
|
|||||||
};
|
};
|
||||||
struct JSON_array: public JSON_value
|
struct JSON_array: public JSON_value
|
||||||
{
|
{
|
||||||
|
JSON_array() :
|
||||||
|
JSON_value(vt_array)
|
||||||
|
{
|
||||||
|
}
|
||||||
virtual ~JSON_array() = default;
|
virtual ~JSON_array() = default;
|
||||||
virtual void write(Pipeline*, size_t depth) const;
|
virtual void write(Pipeline*, size_t depth) const;
|
||||||
std::vector<std::shared_ptr<JSON_value>> elements;
|
std::vector<std::shared_ptr<JSON_value>> elements;
|
||||||
@ -383,6 +407,10 @@ class JSON
|
|||||||
};
|
};
|
||||||
struct JSON_null: public JSON_value
|
struct JSON_null: public JSON_value
|
||||||
{
|
{
|
||||||
|
JSON_null() :
|
||||||
|
JSON_value(vt_null)
|
||||||
|
{
|
||||||
|
}
|
||||||
virtual ~JSON_null() = default;
|
virtual ~JSON_null() = default;
|
||||||
virtual void write(Pipeline*, size_t depth) const;
|
virtual void write(Pipeline*, size_t depth) const;
|
||||||
};
|
};
|
||||||
|
@ -133,6 +133,7 @@ JSON::JSON_array::write(Pipeline* p, size_t depth) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
JSON::JSON_string::JSON_string(std::string const& utf8) :
|
JSON::JSON_string::JSON_string(std::string const& utf8) :
|
||||||
|
JSON_value(vt_string),
|
||||||
utf8(utf8),
|
utf8(utf8),
|
||||||
encoded(encode_string(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::JSON_number::JSON_number(long long value) :
|
||||||
|
JSON_value(vt_number),
|
||||||
encoded(std::to_string(value))
|
encoded(std::to_string(value))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON::JSON_number::JSON_number(double value) :
|
JSON::JSON_number::JSON_number(double value) :
|
||||||
|
JSON_value(vt_number),
|
||||||
encoded(QUtil::double_to_string(value, 6))
|
encoded(QUtil::double_to_string(value, 6))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON::JSON_number::JSON_number(std::string const& value) :
|
JSON::JSON_number::JSON_number(std::string const& value) :
|
||||||
|
JSON_value(vt_number),
|
||||||
encoded(value)
|
encoded(value)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -166,6 +170,7 @@ JSON::JSON_number::write(Pipeline* p, size_t) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
JSON::JSON_bool::JSON_bool(bool val) :
|
JSON::JSON_bool::JSON_bool(bool val) :
|
||||||
|
JSON_value(vt_bool),
|
||||||
value(val)
|
value(val)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -183,6 +188,7 @@ JSON::JSON_null::write(Pipeline* p, size_t) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
JSON::JSON_blob::JSON_blob(std::function<void(Pipeline*)> fn) :
|
JSON::JSON_blob::JSON_blob(std::function<void(Pipeline*)> fn) :
|
||||||
|
JSON_value(vt_blob),
|
||||||
fn(fn)
|
fn(fn)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -376,56 +382,52 @@ JSON::makeBlob(std::function<void(Pipeline*)> fn)
|
|||||||
bool
|
bool
|
||||||
JSON::isArray() const
|
JSON::isArray() const
|
||||||
{
|
{
|
||||||
return nullptr != dynamic_cast<JSON_array const*>(this->m->value.get());
|
return m->value->type_code == vt_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
JSON::isDictionary() const
|
JSON::isDictionary() const
|
||||||
{
|
{
|
||||||
return nullptr !=
|
return m->value->type_code == vt_dictionary;
|
||||||
dynamic_cast<JSON_dictionary const*>(this->m->value.get());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
JSON::getString(std::string& utf8) const
|
JSON::getString(std::string& utf8) const
|
||||||
{
|
{
|
||||||
auto v = dynamic_cast<JSON_string const*>(this->m->value.get());
|
if (m->value->type_code == vt_string) {
|
||||||
if (v == nullptr) {
|
auto v = dynamic_cast<JSON_string const*>(this->m->value.get());
|
||||||
return false;
|
utf8 = v->utf8;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
utf8 = v->utf8;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
JSON::getNumber(std::string& value) const
|
JSON::getNumber(std::string& value) const
|
||||||
{
|
{
|
||||||
auto v = dynamic_cast<JSON_number const*>(this->m->value.get());
|
if (m->value->type_code == vt_number) {
|
||||||
if (v == nullptr) {
|
auto v = dynamic_cast<JSON_number const*>(this->m->value.get());
|
||||||
return false;
|
value = v->encoded;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
value = v->encoded;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
JSON::getBool(bool& value) const
|
JSON::getBool(bool& value) const
|
||||||
{
|
{
|
||||||
auto v = dynamic_cast<JSON_bool const*>(this->m->value.get());
|
if (m->value->type_code == vt_bool) {
|
||||||
if (v == nullptr) {
|
auto v = dynamic_cast<JSON_bool const*>(this->m->value.get());
|
||||||
return false;
|
value = v->value;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
value = v->value;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
JSON::isNull() const
|
JSON::isNull() const
|
||||||
{
|
{
|
||||||
if (dynamic_cast<JSON_null const*>(this->m->value.get())) {
|
return m->value->type_code == vt_null;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
Loading…
Reference in New Issue
Block a user