diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-01-19 15:31:28 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-01-30 19:11:03 +0100 |
commit | 37105710ee0b332a3020d4b3220c95b8f4267555 (patch) | |
tree | ee4b520b1c141033ba16ba3696def57fc90f60fa /libtests | |
parent | a6df6fdaf724ed5fc6f7e8c021f7804bd5a9c0e2 (diff) | |
download | qpdf-37105710ee0b332a3020d4b3220c95b8f4267555.tar.zst |
Implement JSONHandler for recursively processing JSON
Diffstat (limited to 'libtests')
-rw-r--r-- | libtests/build.mk | 1 | ||||
-rw-r--r-- | libtests/json.cc | 39 | ||||
-rw-r--r-- | libtests/json_handler.cc | 128 | ||||
-rw-r--r-- | libtests/libtests.testcov | 2 | ||||
-rw-r--r-- | libtests/qtest/json_handler.test | 17 | ||||
-rw-r--r-- | libtests/qtest/json_handler/json_handler.out | 22 |
6 files changed, 208 insertions, 1 deletions
diff --git a/libtests/build.mk b/libtests/build.mk index a23b9c28..682c2b6d 100644 --- a/libtests/build.mk +++ b/libtests/build.mk @@ -13,6 +13,7 @@ BINS_libtests = \ hex \ input_source \ json \ + json_handler \ json_parse \ lzw \ main_from_wmain \ diff --git a/libtests/json.cc b/libtests/json.cc index 7bea5589..006f00cf 100644 --- a/libtests/json.cc +++ b/libtests/json.cc @@ -1,7 +1,7 @@ #include <qpdf/JSON.hh> #include <qpdf/QPDFObjectHandle.hh> #include <iostream> -#include <assert.h> +#include <cassert> static void check(JSON const& j, std::string const& exp) { @@ -20,12 +20,25 @@ static void test_main() "\\u0003\\t\\b\\r\\n<4>\""); JSON jnull = JSON::makeNull(); check(jnull, "null"); + assert(jnull.isNull()); + std::string value; + assert(! jnull.getNumber(value)); JSON jarr = JSON::makeArray(); check(jarr, "[]"); JSON jstr2 = JSON::makeString("a\tb"); + assert(jstr2.getString(value)); + assert(value == "a\tb"); + assert(! jstr2.getNumber(value)); JSON jint = JSON::makeInt(16059); JSON jdouble = JSON::makeReal(3.14159); JSON jexp = JSON::makeNumber("2.1e5"); + JSON jbool1 = JSON::makeBool(true); + JSON jbool2 = JSON::makeBool(false); + bool bvalue = false; + assert(jbool1.getBool(bvalue)); + assert(bvalue); + assert(jbool2.getBool(bvalue)); + assert(! bvalue); jarr.addArrayElement(jstr2); jarr.addArrayElement(jnull); jarr.addArrayElement(jint); @@ -39,6 +52,18 @@ static void test_main() " 3.14159,\n" " 2.1e5\n" "]"); + std::vector<std::string> avalue; + assert(jarr.forEachArrayItem([&avalue](JSON j) { + avalue.push_back(j.unparse()); + })); + std::vector<std::string> xavalue = { + "\"a\\tb\"", + "null", + "16059", + "3.14159", + "2.1e5", + }; + assert(avalue == xavalue); JSON jmap = JSON::makeDictionary(); check(jmap, "{}"); jmap.addDictionaryMember("b", jstr2); @@ -73,6 +98,18 @@ static void test_main() check(QPDFObjectHandle::newReal(".34").getJSON(), "0.34"); check(QPDFObjectHandle::newReal("-0.56").getJSON(), "-0.56"); check(QPDFObjectHandle::newReal("-.78").getJSON(), "-0.78"); + JSON jmap2 = JSON::parse(R"({"a": 1, "b": "two", "c": [true]})"); + std::map<std::string, std::string> dvalue; + assert(jmap2.forEachDictItem([&dvalue] + (std::string const& k, JSON j) { + dvalue[k] = j.unparse(); + })); + std::map<std::string, std::string> xdvalue = { + {"a", "1"}, + {"b", "\"two\""}, + {"c", "[\n true\n]"}, + }; + assert(dvalue == xdvalue); } static void check_schema(JSON& obj, JSON& schema, bool exp, diff --git a/libtests/json_handler.cc b/libtests/json_handler.cc new file mode 100644 index 00000000..d5e6aea3 --- /dev/null +++ b/libtests/json_handler.cc @@ -0,0 +1,128 @@ +#include <qpdf/JSONHandler.hh> +#include <qpdf/QUtil.hh> +#include <iostream> +#include <cassert> + +static void print_null(std::string const& path) +{ + std::cout << path << ": null" << std::endl; +} + +static void print_string(std::string const& path, std::string const& value) +{ + std::cout << path << ": string: " << value << std::endl; +} + +static void print_number(std::string const& path, std::string const& value) +{ + std::cout << path << ": number: " << value << std::endl; +} + +static void print_bool(std::string const& path, bool value) +{ + std::cout << path << ": bool: " << (value ? "true" : "false") << std::endl; +} + +static void print_json(std::string const& path, JSON value) +{ + std::cout << path << ": json: " << value.unparse() << std::endl; +} + +static void test_scalar() +{ + std::cout << "-- scalar --" << std::endl; + JSONHandler h; + h.addStringHandler(print_string); + JSON j = JSON::parse("\"potato\""); + h.handle(".", j); +} + +static std::shared_ptr<JSONHandler> make_all_handler() +{ + auto h = std::make_shared<JSONHandler>(); + auto& m = h->addDictHandlers(); + auto h1 = std::make_shared<JSONHandler>(); + h1->addStringHandler(print_string); + m["one"] = h1; + auto h2 = std::make_shared<JSONHandler>(); + h2->addNumberHandler(print_number); + m["two"] = h2; + auto h3 = std::make_shared<JSONHandler>(); + h3->addBoolHandler(print_bool); + m["three"] = h3; + auto h4 = std::make_shared<JSONHandler>(); + h4->addAnyHandler(print_json); + m["four"] = h4; + m["phour"] = h4; // share h4 + auto h5 = std::make_shared<JSONHandler>(); + // Allow to be either string or bool + h5->addBoolHandler(print_bool); + h5->addStringHandler(print_string); + h5->addNullHandler(print_null); + auto h5s = std::make_shared<JSONHandler>(); + m["five"] = h5s; + h5s->addArrayHandler(h5); + auto h6 = std::make_shared<JSONHandler>(); + auto& m6 = h6->addDictHandlers(); + auto h6a = std::make_shared<JSONHandler>(); + m6["a"] = h6a; + auto& m6a = h6a->addDictHandlers(); + auto h6ab = std::make_shared<JSONHandler>(); + m6a["b"] = h6ab; + auto h6ax = std::make_shared<JSONHandler>(); + h6ax->addAnyHandler(print_json); + h6a->addFallbackDictHandler(h6ax); + m6["b"] = h6ab; // share + h6ab->addStringHandler(print_string); + m["six"] = h6; + return h; +} + +static void test_all() +{ + std::cout << "-- all --" << std::endl; + auto h = make_all_handler(); + JSON j = JSON::parse(R"({ + "one": "potato", + "two": 3.14, + "three": true, + "four": ["a", 1], + "five": ["x", false, "y", null, true], + "phour": null, + "six": {"a": {"b": "quack", "Q": "baaa"}, "b": "moo"} +})"); + h->handle(".", j); +} + +static void test_errors() +{ + std::cout << "-- errors --" << std::endl; + auto h = make_all_handler(); + auto t = [h](std::string const& msg, std::function<void()> fn) { + try + { + fn(); + assert(false); + } + catch (JSONHandler::Error& e) + { + std::cout << msg << ": " << e.what() << std::endl; + } + }; + + t("bad type at top", [&h](){ + h->handle(".", JSON::makeString("oops")); + }); + t("unexpected key", [&h](){ + JSON j = JSON::parse(R"({"x": "y"})"); + h->handle(".", j); + }); +} + +int main(int argc, char* argv[]) +{ + test_scalar(); + test_all(); + test_errors(); + return 0; +} diff --git a/libtests/libtests.testcov b/libtests/libtests.testcov index 5f36819a..6284c0e8 100644 --- a/libtests/libtests.testcov +++ b/libtests/libtests.testcov @@ -87,3 +87,5 @@ JSON parse leading zero 0 JSON parse number no digits 0 JSON parse premature end of u 0 JSON parse bad hex after u 0 +JSONHandler unhandled value 0 +JSONHandler unexpected key 0 diff --git a/libtests/qtest/json_handler.test b/libtests/qtest/json_handler.test new file mode 100644 index 00000000..6df51fc1 --- /dev/null +++ b/libtests/qtest/json_handler.test @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +require 5.008; +use warnings; +use strict; + +chdir("json_handler") or die "chdir testdir failed: $!\n"; + +require TestDriver; + +my $td = new TestDriver('json_handler'); + +$td->runtest("JSON handler", + {$td->COMMAND => "json_handler"}, + {$td->FILE => "json_handler.out", $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); + +$td->report(1); diff --git a/libtests/qtest/json_handler/json_handler.out b/libtests/qtest/json_handler/json_handler.out new file mode 100644 index 00000000..13554af3 --- /dev/null +++ b/libtests/qtest/json_handler/json_handler.out @@ -0,0 +1,22 @@ +-- scalar -- +.: string: potato +-- all -- +.five[0]: string: x +.five[1]: bool: false +.five[2]: string: y +.five[3]: null +.five[4]: bool: true +.four: json: [ + "a", + 1 +] +.one: string: potato +.phour: json: null +.six.a.Q: json: "baaa" +.six.a.b: string: quack +.six.b: string: moo +.three: bool: true +.two: number: 3.14 +-- errors -- +bad type at top: JSON handler: value at . is not of expected type +unexpected key: JSON handler found unexpected key x in object at . |