diff options
author | Jay Berkenbilt <ejb@ql.org> | 2024-01-10 02:28:28 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2024-01-11 12:13:57 +0100 |
commit | 7de0b3f3c083990842523112959f8e27a0d2e5a0 (patch) | |
tree | e4bba51e3129c2a1b558b6a1cce0e2cf4e4c6974 /libqpdf | |
parent | 12f7a4461b18b4be94002fa0043fd0e98e80a274 (diff) | |
download | qpdf-7de0b3f3c083990842523112959f8e27a0d2e5a0.tar.zst |
JSONHandler: add fallback handler support
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/JSONHandler.cc | 13 | ||||
-rw-r--r-- | libqpdf/qpdf/JSONHandler.hh | 3 |
2 files changed, 15 insertions, 1 deletions
diff --git a/libqpdf/JSONHandler.cc b/libqpdf/JSONHandler.cc index 4a69fd60..a8116cb0 100644 --- a/libqpdf/JSONHandler.cc +++ b/libqpdf/JSONHandler.cc @@ -17,10 +17,10 @@ struct Handlers JSONHandler::void_handler_t dict_end_handler{nullptr}; JSONHandler::json_handler_t array_start_handler{nullptr}; JSONHandler::void_handler_t array_end_handler{nullptr}; - JSONHandler::void_handler_t final_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; + std::shared_ptr<JSONHandler> fallback_handler; }; class JSONHandler::Members @@ -111,6 +111,12 @@ JSONHandler::addArrayHandlers( } void +JSONHandler::addFallbackHandler(std::shared_ptr<JSONHandler> h) +{ + m->h.fallback_handler = std::move(h); +} + +void JSONHandler::handle(std::string const& path, JSON j) { if (m->h.any_handler) { @@ -169,6 +175,11 @@ JSONHandler::handle(std::string const& path, JSON j) return; } + if (m->h.fallback_handler) { + m->h.fallback_handler->handle(path, j); + return; + } + // 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 diff --git a/libqpdf/qpdf/JSONHandler.hh b/libqpdf/qpdf/JSONHandler.hh index 9b2a0b33..1c53e32d 100644 --- a/libqpdf/qpdf/JSONHandler.hh +++ b/libqpdf/qpdf/JSONHandler.hh @@ -45,6 +45,9 @@ class JSONHandler void addArrayHandlers( json_handler_t start_fn, void_handler_t end_fn, std::shared_ptr<JSONHandler> item_handlers); + // If no handlers is called, the fallback handler will be used to try to handle the item. + void addFallbackHandler(std::shared_ptr<JSONHandler>); + // Apply handlers recursively to a JSON object. void handle(std::string const& path, JSON j); |