Commit d80b63c3c0c43fd6bd9e275c7979ecde57d30264
Committed by
Jay Berkenbilt
1 parent
72bf7197
Refactor JSON type checks
Showing
2 changed files
with
52 additions
and
22 deletions
include/qpdf/JSON.hh
| @@ -339,13 +339,33 @@ class JSON | @@ -339,13 +339,33 @@ class JSON | ||
| 339 | static void | 339 | static void |
| 340 | writeClose(Pipeline* p, bool first, size_t depth, char const* delimeter); | 340 | writeClose(Pipeline* p, bool first, size_t depth, char const* delimeter); |
| 341 | 341 | ||
| 342 | + enum value_type_e { | ||
| 343 | + vt_none, | ||
| 344 | + vt_dictionary, | ||
| 345 | + vt_array, | ||
| 346 | + vt_string, | ||
| 347 | + vt_number, | ||
| 348 | + vt_bool, | ||
| 349 | + vt_null, | ||
| 350 | + vt_blob, | ||
| 351 | + }; | ||
| 352 | + | ||
| 342 | struct JSON_value | 353 | struct JSON_value |
| 343 | { | 354 | { |
| 355 | + JSON_value(value_type_e type_code) : | ||
| 356 | + type_code(type_code) | ||
| 357 | + { | ||
| 358 | + } | ||
| 344 | virtual ~JSON_value() = default; | 359 | virtual ~JSON_value() = default; |
| 345 | virtual void write(Pipeline*, size_t depth) const = 0; | 360 | virtual void write(Pipeline*, size_t depth) const = 0; |
| 361 | + const value_type_e type_code{vt_none}; | ||
| 346 | }; | 362 | }; |
| 347 | struct JSON_dictionary: public JSON_value | 363 | struct JSON_dictionary: public JSON_value |
| 348 | { | 364 | { |
| 365 | + JSON_dictionary() : | ||
| 366 | + JSON_value(vt_dictionary) | ||
| 367 | + { | ||
| 368 | + } | ||
| 349 | virtual ~JSON_dictionary() = default; | 369 | virtual ~JSON_dictionary() = default; |
| 350 | virtual void write(Pipeline*, size_t depth) const; | 370 | virtual void write(Pipeline*, size_t depth) const; |
| 351 | std::map<std::string, std::shared_ptr<JSON_value>> members; | 371 | std::map<std::string, std::shared_ptr<JSON_value>> members; |
| @@ -353,6 +373,10 @@ class JSON | @@ -353,6 +373,10 @@ class JSON | ||
| 353 | }; | 373 | }; |
| 354 | struct JSON_array: public JSON_value | 374 | struct JSON_array: public JSON_value |
| 355 | { | 375 | { |
| 376 | + JSON_array() : | ||
| 377 | + JSON_value(vt_array) | ||
| 378 | + { | ||
| 379 | + } | ||
| 356 | virtual ~JSON_array() = default; | 380 | virtual ~JSON_array() = default; |
| 357 | virtual void write(Pipeline*, size_t depth) const; | 381 | virtual void write(Pipeline*, size_t depth) const; |
| 358 | std::vector<std::shared_ptr<JSON_value>> elements; | 382 | std::vector<std::shared_ptr<JSON_value>> elements; |
| @@ -383,6 +407,10 @@ class JSON | @@ -383,6 +407,10 @@ class JSON | ||
| 383 | }; | 407 | }; |
| 384 | struct JSON_null: public JSON_value | 408 | struct JSON_null: public JSON_value |
| 385 | { | 409 | { |
| 410 | + JSON_null() : | ||
| 411 | + JSON_value(vt_null) | ||
| 412 | + { | ||
| 413 | + } | ||
| 386 | virtual ~JSON_null() = default; | 414 | virtual ~JSON_null() = default; |
| 387 | virtual void write(Pipeline*, size_t depth) const; | 415 | virtual void write(Pipeline*, size_t depth) const; |
| 388 | }; | 416 | }; |
libqpdf/JSON.cc
| @@ -133,6 +133,7 @@ JSON::JSON_array::write(Pipeline* p, size_t depth) const | @@ -133,6 +133,7 @@ JSON::JSON_array::write(Pipeline* p, size_t depth) const | ||
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | JSON::JSON_string::JSON_string(std::string const& utf8) : | 135 | JSON::JSON_string::JSON_string(std::string const& utf8) : |
| 136 | + JSON_value(vt_string), | ||
| 136 | utf8(utf8), | 137 | utf8(utf8), |
| 137 | encoded(encode_string(utf8)) | 138 | encoded(encode_string(utf8)) |
| 138 | { | 139 | { |
| @@ -145,16 +146,19 @@ JSON::JSON_string::write(Pipeline* p, size_t) const | @@ -145,16 +146,19 @@ JSON::JSON_string::write(Pipeline* p, size_t) const | ||
| 145 | } | 146 | } |
| 146 | 147 | ||
| 147 | JSON::JSON_number::JSON_number(long long value) : | 148 | JSON::JSON_number::JSON_number(long long value) : |
| 149 | + JSON_value(vt_number), | ||
| 148 | encoded(std::to_string(value)) | 150 | encoded(std::to_string(value)) |
| 149 | { | 151 | { |
| 150 | } | 152 | } |
| 151 | 153 | ||
| 152 | JSON::JSON_number::JSON_number(double value) : | 154 | JSON::JSON_number::JSON_number(double value) : |
| 155 | + JSON_value(vt_number), | ||
| 153 | encoded(QUtil::double_to_string(value, 6)) | 156 | encoded(QUtil::double_to_string(value, 6)) |
| 154 | { | 157 | { |
| 155 | } | 158 | } |
| 156 | 159 | ||
| 157 | JSON::JSON_number::JSON_number(std::string const& value) : | 160 | JSON::JSON_number::JSON_number(std::string const& value) : |
| 161 | + JSON_value(vt_number), | ||
| 158 | encoded(value) | 162 | encoded(value) |
| 159 | { | 163 | { |
| 160 | } | 164 | } |
| @@ -166,6 +170,7 @@ JSON::JSON_number::write(Pipeline* p, size_t) const | @@ -166,6 +170,7 @@ JSON::JSON_number::write(Pipeline* p, size_t) const | ||
| 166 | } | 170 | } |
| 167 | 171 | ||
| 168 | JSON::JSON_bool::JSON_bool(bool val) : | 172 | JSON::JSON_bool::JSON_bool(bool val) : |
| 173 | + JSON_value(vt_bool), | ||
| 169 | value(val) | 174 | value(val) |
| 170 | { | 175 | { |
| 171 | } | 176 | } |
| @@ -183,6 +188,7 @@ JSON::JSON_null::write(Pipeline* p, size_t) const | @@ -183,6 +188,7 @@ JSON::JSON_null::write(Pipeline* p, size_t) const | ||
| 183 | } | 188 | } |
| 184 | 189 | ||
| 185 | JSON::JSON_blob::JSON_blob(std::function<void(Pipeline*)> fn) : | 190 | JSON::JSON_blob::JSON_blob(std::function<void(Pipeline*)> fn) : |
| 191 | + JSON_value(vt_blob), | ||
| 186 | fn(fn) | 192 | fn(fn) |
| 187 | { | 193 | { |
| 188 | } | 194 | } |
| @@ -376,56 +382,52 @@ JSON::makeBlob(std::function<void(Pipeline*)> fn) | @@ -376,56 +382,52 @@ JSON::makeBlob(std::function<void(Pipeline*)> fn) | ||
| 376 | bool | 382 | bool |
| 377 | JSON::isArray() const | 383 | JSON::isArray() const |
| 378 | { | 384 | { |
| 379 | - return nullptr != dynamic_cast<JSON_array const*>(this->m->value.get()); | 385 | + return m->value->type_code == vt_array; |
| 380 | } | 386 | } |
| 381 | 387 | ||
| 382 | bool | 388 | bool |
| 383 | JSON::isDictionary() const | 389 | JSON::isDictionary() const |
| 384 | { | 390 | { |
| 385 | - return nullptr != | ||
| 386 | - dynamic_cast<JSON_dictionary const*>(this->m->value.get()); | 391 | + return m->value->type_code == vt_dictionary; |
| 387 | } | 392 | } |
| 388 | 393 | ||
| 389 | bool | 394 | bool |
| 390 | JSON::getString(std::string& utf8) const | 395 | JSON::getString(std::string& utf8) const |
| 391 | { | 396 | { |
| 392 | - auto v = dynamic_cast<JSON_string const*>(this->m->value.get()); | ||
| 393 | - if (v == nullptr) { | ||
| 394 | - return false; | 397 | + if (m->value->type_code == vt_string) { |
| 398 | + auto v = dynamic_cast<JSON_string const*>(this->m->value.get()); | ||
| 399 | + utf8 = v->utf8; | ||
| 400 | + return true; | ||
| 395 | } | 401 | } |
| 396 | - utf8 = v->utf8; | ||
| 397 | - return true; | 402 | + return false; |
| 398 | } | 403 | } |
| 399 | 404 | ||
| 400 | bool | 405 | bool |
| 401 | JSON::getNumber(std::string& value) const | 406 | JSON::getNumber(std::string& value) const |
| 402 | { | 407 | { |
| 403 | - auto v = dynamic_cast<JSON_number const*>(this->m->value.get()); | ||
| 404 | - if (v == nullptr) { | ||
| 405 | - return false; | 408 | + if (m->value->type_code == vt_number) { |
| 409 | + auto v = dynamic_cast<JSON_number const*>(this->m->value.get()); | ||
| 410 | + value = v->encoded; | ||
| 411 | + return true; | ||
| 406 | } | 412 | } |
| 407 | - value = v->encoded; | ||
| 408 | - return true; | 413 | + return false; |
| 409 | } | 414 | } |
| 410 | 415 | ||
| 411 | bool | 416 | bool |
| 412 | JSON::getBool(bool& value) const | 417 | JSON::getBool(bool& value) const |
| 413 | { | 418 | { |
| 414 | - auto v = dynamic_cast<JSON_bool const*>(this->m->value.get()); | ||
| 415 | - if (v == nullptr) { | ||
| 416 | - return false; | 419 | + if (m->value->type_code == vt_bool) { |
| 420 | + auto v = dynamic_cast<JSON_bool const*>(this->m->value.get()); | ||
| 421 | + value = v->value; | ||
| 422 | + return true; | ||
| 417 | } | 423 | } |
| 418 | - value = v->value; | ||
| 419 | - return true; | 424 | + return false; |
| 420 | } | 425 | } |
| 421 | 426 | ||
| 422 | bool | 427 | bool |
| 423 | JSON::isNull() const | 428 | JSON::isNull() const |
| 424 | { | 429 | { |
| 425 | - if (dynamic_cast<JSON_null const*>(this->m->value.get())) { | ||
| 426 | - return true; | ||
| 427 | - } | ||
| 428 | - return false; | 430 | + return m->value->type_code == vt_null; |
| 429 | } | 431 | } |
| 430 | 432 | ||
| 431 | bool | 433 | bool |