2022-01-19 14:31:28 +00:00
|
|
|
#include <qpdf/JSONHandler.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2022-01-28 12:46:04 +00:00
|
|
|
#include <qpdf/QPDFUsage.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2022-01-19 14:31:28 +00:00
|
|
|
|
2023-06-12 08:02:20 +00:00
|
|
|
struct Handlers
|
|
|
|
{
|
|
|
|
Handlers() = default;
|
|
|
|
|
|
|
|
JSONHandler::json_handler_t any_handler{nullptr};
|
|
|
|
JSONHandler::void_handler_t null_handler{nullptr};
|
|
|
|
JSONHandler::string_handler_t string_handler{nullptr};
|
|
|
|
JSONHandler::string_handler_t number_handler{nullptr};
|
|
|
|
JSONHandler::bool_handler_t bool_handler{nullptr};
|
|
|
|
JSONHandler::json_handler_t dict_start_handler{nullptr};
|
|
|
|
JSONHandler::void_handler_t dict_end_handler{nullptr};
|
|
|
|
JSONHandler::json_handler_t array_start_handler{nullptr};
|
|
|
|
JSONHandler::void_handler_t array_end_handler{nullptr};
|
|
|
|
std::map<std::string, std::shared_ptr<JSONHandler>> dict_handlers;
|
|
|
|
std::shared_ptr<JSONHandler> fallback_dict_handler;
|
|
|
|
std::shared_ptr<JSONHandler> array_item_handler;
|
2024-01-10 01:28:28 +00:00
|
|
|
std::shared_ptr<JSONHandler> fallback_handler;
|
2023-06-12 08:02:20 +00:00
|
|
|
};
|
|
|
|
|
2023-05-29 13:35:45 +00:00
|
|
|
class JSONHandler::Members
|
|
|
|
{
|
|
|
|
friend class JSONHandler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~Members() = default;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Members() = default;
|
|
|
|
Members(Members const&) = delete;
|
|
|
|
|
|
|
|
Handlers h;
|
|
|
|
};
|
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
JSONHandler::JSONHandler() :
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-29 13:35:45 +00:00
|
|
|
JSONHandler::~JSONHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:46:04 +00:00
|
|
|
void
|
|
|
|
JSONHandler::usage(std::string const& msg)
|
|
|
|
{
|
|
|
|
throw QPDFUsage(msg);
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
void
|
|
|
|
JSONHandler::addAnyHandler(json_handler_t fn)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.any_handler = fn;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONHandler::addNullHandler(void_handler_t fn)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.null_handler = fn;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONHandler::addStringHandler(string_handler_t fn)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.string_handler = fn;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONHandler::addNumberHandler(string_handler_t fn)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.number_handler = fn;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONHandler::addBoolHandler(bool_handler_t fn)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.bool_handler = fn;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 13:53:53 +00:00
|
|
|
void
|
2022-01-30 13:18:04 +00:00
|
|
|
JSONHandler::addDictHandlers(json_handler_t start_fn, void_handler_t end_fn)
|
2022-01-20 13:53:53 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.dict_start_handler = start_fn;
|
|
|
|
m->h.dict_end_handler = end_fn;
|
2022-01-20 13:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-05-21 17:35:09 +00:00
|
|
|
JSONHandler::addDictKeyHandler(std::string const& key, std::shared_ptr<JSONHandler> dkh)
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.dict_handlers[key] = dkh;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONHandler::addFallbackDictHandler(std::shared_ptr<JSONHandler> fdh)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.fallback_dict_handler = fdh;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
JSONHandler::addArrayHandlers(
|
2023-05-21 17:35:09 +00:00
|
|
|
json_handler_t start_fn, void_handler_t end_fn, std::shared_ptr<JSONHandler> ah)
|
2022-01-19 14:31:28 +00:00
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.array_start_handler = start_fn;
|
|
|
|
m->h.array_end_handler = end_fn;
|
|
|
|
m->h.array_item_handler = ah;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 01:28:28 +00:00
|
|
|
void
|
|
|
|
JSONHandler::addFallbackHandler(std::shared_ptr<JSONHandler> h)
|
|
|
|
{
|
|
|
|
m->h.fallback_handler = std::move(h);
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
void
|
|
|
|
JSONHandler::handle(std::string const& path, JSON j)
|
|
|
|
{
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->h.any_handler) {
|
|
|
|
m->h.any_handler(path, j);
|
2022-01-19 14:31:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-12 12:29:58 +00:00
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
bool bvalue = false;
|
2022-01-28 14:05:06 +00:00
|
|
|
std::string s_value;
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->h.null_handler && j.isNull()) {
|
|
|
|
m->h.null_handler(path);
|
2023-06-12 12:29:58 +00:00
|
|
|
return;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->h.string_handler && j.getString(s_value)) {
|
|
|
|
m->h.string_handler(path, s_value);
|
2023-06-12 12:29:58 +00:00
|
|
|
return;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->h.number_handler && j.getNumber(s_value)) {
|
|
|
|
m->h.number_handler(path, s_value);
|
2023-06-12 12:29:58 +00:00
|
|
|
return;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->h.bool_handler && j.getBool(bvalue)) {
|
|
|
|
m->h.bool_handler(path, bvalue);
|
2023-06-12 12:29:58 +00:00
|
|
|
return;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->h.dict_start_handler && j.isDictionary()) {
|
|
|
|
m->h.dict_start_handler(path, j);
|
2022-01-19 14:31:28 +00:00
|
|
|
std::string path_base = path;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (path_base != ".") {
|
2022-01-19 14:31:28 +00:00
|
|
|
path_base += ".";
|
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
j.forEachDictItem([&path, &path_base, this](std::string const& k, JSON v) {
|
|
|
|
auto i = m->h.dict_handlers.find(k);
|
|
|
|
if (i == m->h.dict_handlers.end()) {
|
|
|
|
if (m->h.fallback_dict_handler.get()) {
|
|
|
|
m->h.fallback_dict_handler->handle(path_base + k, v);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2023-05-21 17:35:09 +00:00
|
|
|
QTC::TC("libtests", "JSONHandler unexpected key");
|
|
|
|
usage("JSON handler found unexpected key " + k + " in object at " + path);
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-05-21 17:35:09 +00:00
|
|
|
} else {
|
|
|
|
i->second->handle(path_base + k, v);
|
|
|
|
}
|
|
|
|
});
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.dict_end_handler(path);
|
2023-06-12 12:29:58 +00:00
|
|
|
return;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-05-21 13:42:34 +00:00
|
|
|
if (m->h.array_start_handler && j.isArray()) {
|
|
|
|
m->h.array_start_handler(path, j);
|
2022-01-19 14:31:28 +00:00
|
|
|
size_t i = 0;
|
|
|
|
j.forEachArrayItem([&i, &path, this](JSON v) {
|
2023-05-21 17:35:09 +00:00
|
|
|
m->h.array_item_handler->handle(path + "[" + std::to_string(i) + "]", v);
|
2022-01-19 14:31:28 +00:00
|
|
|
++i;
|
|
|
|
});
|
2023-05-21 13:42:34 +00:00
|
|
|
m->h.array_end_handler(path);
|
2023-06-12 12:29:58 +00:00
|
|
|
return;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 01:28:28 +00:00
|
|
|
if (m->h.fallback_handler) {
|
|
|
|
m->h.fallback_handler->handle(path, j);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:29:58 +00:00
|
|
|
// It would be nice to include information about what type the object was and what types were
|
|
|
|
// allowed, but we're relying on schema validation to make sure input is properly structured
|
|
|
|
// before calling the handlers. It would be different if this code were trying to be part of a
|
|
|
|
// general-purpose JSON package.
|
|
|
|
QTC::TC("libtests", "JSONHandler unhandled value");
|
|
|
|
usage("JSON handler: value at " + path + " is not of expected type");
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|