2022-05-03 12:21:01 +00:00
|
|
|
#include <qpdf/assert_test.h>
|
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
#include <qpdf/JSONHandler.hh>
|
2022-01-28 12:46:04 +00:00
|
|
|
#include <qpdf/QPDFUsage.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2022-01-19 14:31:28 +00:00
|
|
|
#include <iostream>
|
2022-03-07 13:46:53 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
print_null(std::string const& path)
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << path << ": null" << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
print_string(std::string const& path, std::string const& value)
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << path << ": string: " << value << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
print_number(std::string const& path, std::string const& value)
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << path << ": number: " << value << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
print_bool(std::string const& path, bool value)
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << path << ": bool: " << (value ? "true" : "false") << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
print_json(std::string const& path, JSON value)
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << path << ": json: " << value.unparse() << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static JSONHandler::void_handler_t
|
|
|
|
make_print_message(std::string msg)
|
2022-01-20 13:53:53 +00:00
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
return [msg](std::string const& path) { std::cout << path << ": json: " << msg << std::endl; };
|
2022-01-20 13:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
test_scalar()
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << "-- scalar --" << std::endl;
|
|
|
|
JSONHandler h;
|
|
|
|
h.addStringHandler(print_string);
|
|
|
|
JSON j = JSON::parse("\"potato\"");
|
|
|
|
h.handle(".", j);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static std::shared_ptr<JSONHandler>
|
|
|
|
make_all_handler()
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
auto h = std::make_shared<JSONHandler>();
|
2022-04-02 21:14:10 +00:00
|
|
|
h->addDictHandlers(print_json, make_print_message("dict end"));
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h1 = std::make_shared<JSONHandler>();
|
|
|
|
h1->addStringHandler(print_string);
|
2022-01-20 13:53:53 +00:00
|
|
|
h->addDictKeyHandler("one", h1);
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h2 = std::make_shared<JSONHandler>();
|
|
|
|
h2->addNumberHandler(print_number);
|
2022-01-20 13:53:53 +00:00
|
|
|
h->addDictKeyHandler("two", h2);
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h3 = std::make_shared<JSONHandler>();
|
|
|
|
h3->addBoolHandler(print_bool);
|
2022-01-20 13:53:53 +00:00
|
|
|
h->addDictKeyHandler("three", h3);
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h4 = std::make_shared<JSONHandler>();
|
|
|
|
h4->addAnyHandler(print_json);
|
2022-01-20 13:53:53 +00:00
|
|
|
h->addDictKeyHandler("four", h4);
|
|
|
|
h->addDictKeyHandler("phour", h4); // share h4
|
2022-01-19 14:31:28 +00:00
|
|
|
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>();
|
2022-01-20 13:53:53 +00:00
|
|
|
h->addDictKeyHandler("five", h5s);
|
2022-04-02 21:14:10 +00:00
|
|
|
h5s->addArrayHandlers(print_json, make_print_message("array end"), h5);
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h6 = std::make_shared<JSONHandler>();
|
2022-04-02 21:14:10 +00:00
|
|
|
h6->addDictHandlers(print_json, make_print_message("dict end"));
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h6a = std::make_shared<JSONHandler>();
|
2022-01-20 13:53:53 +00:00
|
|
|
h6->addDictKeyHandler("a", h6a);
|
2022-04-02 21:14:10 +00:00
|
|
|
h6a->addDictHandlers(print_json, make_print_message("dict end"));
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h6ab = std::make_shared<JSONHandler>();
|
2022-01-20 13:53:53 +00:00
|
|
|
h6a->addDictKeyHandler("b", h6ab);
|
2022-01-19 14:31:28 +00:00
|
|
|
auto h6ax = std::make_shared<JSONHandler>();
|
|
|
|
h6ax->addAnyHandler(print_json);
|
|
|
|
h6a->addFallbackDictHandler(h6ax);
|
2022-01-20 13:53:53 +00:00
|
|
|
h6->addDictKeyHandler("b", h6ab); // share
|
2022-01-19 14:31:28 +00:00
|
|
|
h6ab->addStringHandler(print_string);
|
2022-01-20 13:53:53 +00:00
|
|
|
h->addDictKeyHandler("six", h6);
|
2022-01-19 14:31:28 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
test_all()
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << "-- all --" << std::endl;
|
|
|
|
auto h = make_all_handler();
|
2022-01-28 14:05:06 +00:00
|
|
|
/* cSpell: ignore phour */
|
2022-01-19 14:31:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
test_errors()
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
std::cout << "-- errors --" << std::endl;
|
|
|
|
auto h = make_all_handler();
|
|
|
|
auto t = [h](std::string const& msg, std::function<void()> fn) {
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2022-01-19 14:31:28 +00:00
|
|
|
fn();
|
|
|
|
assert(false);
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFUsage& e) {
|
2022-01-19 14:31:28 +00:00
|
|
|
std::cout << msg << ": " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
t("bad type at top", [&h]() { h->handle(".", JSON::makeString("oops")); });
|
|
|
|
t("unexpected key", [&h]() {
|
2022-01-19 14:31:28 +00:00
|
|
|
JSON j = JSON::parse(R"({"x": "y"})");
|
|
|
|
h->handle(".", j);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
|
|
|
test_scalar();
|
|
|
|
test_all();
|
|
|
|
test_errors();
|
|
|
|
return 0;
|
|
|
|
}
|