Commit ce3406e93f0de0946d2abed6179579caf1433d4e
1 parent
11a86e44
JSONHandler: pass JSON object to dict start function
If some keys depend on others, we have to check up front since there is no control of what order key handlers will be called. Anyway, keys are unordered in json, so we don't want to depend on ordering.
Showing
4 changed files
with
45 additions
and
14 deletions
libqpdf/JSONHandler.cc
| @@ -49,7 +49,7 @@ JSONHandler::addBoolHandler(bool_handler_t fn) | @@ -49,7 +49,7 @@ JSONHandler::addBoolHandler(bool_handler_t fn) | ||
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | void | 51 | void |
| 52 | -JSONHandler::addDictHandlers(void_handler_t start_fn, void_handler_t end_fn) | 52 | +JSONHandler::addDictHandlers(json_handler_t start_fn, void_handler_t end_fn) |
| 53 | { | 53 | { |
| 54 | this->m->h.dict_start_handler = start_fn; | 54 | this->m->h.dict_start_handler = start_fn; |
| 55 | this->m->h.dict_end_handler = end_fn; | 55 | this->m->h.dict_end_handler = end_fn; |
| @@ -111,7 +111,7 @@ JSONHandler::handle(std::string const& path, JSON j) | @@ -111,7 +111,7 @@ JSONHandler::handle(std::string const& path, JSON j) | ||
| 111 | } | 111 | } |
| 112 | if (this->m->h.dict_start_handler && j.isDictionary()) | 112 | if (this->m->h.dict_start_handler && j.isDictionary()) |
| 113 | { | 113 | { |
| 114 | - this->m->h.dict_start_handler(path); | 114 | + this->m->h.dict_start_handler(path, j); |
| 115 | std::string path_base = path; | 115 | std::string path_base = path; |
| 116 | if (path_base != ".") | 116 | if (path_base != ".") |
| 117 | { | 117 | { |
libqpdf/qpdf/JSONHandler.hh
| @@ -58,7 +58,7 @@ class JSONHandler | @@ -58,7 +58,7 @@ class JSONHandler | ||
| 58 | void addBoolHandler(bool_handler_t fn); | 58 | void addBoolHandler(bool_handler_t fn); |
| 59 | 59 | ||
| 60 | QPDF_DLL | 60 | QPDF_DLL |
| 61 | - void addDictHandlers(void_handler_t start_fn, void_handler_t end_fn); | 61 | + void addDictHandlers(json_handler_t start_fn, void_handler_t end_fn); |
| 62 | QPDF_DLL | 62 | QPDF_DLL |
| 63 | void addDictKeyHandler( | 63 | void addDictKeyHandler( |
| 64 | std::string const& key, std::shared_ptr<JSONHandler>); | 64 | std::string const& key, std::shared_ptr<JSONHandler>); |
| @@ -100,7 +100,7 @@ class JSONHandler | @@ -100,7 +100,7 @@ class JSONHandler | ||
| 100 | string_handler_t string_handler; | 100 | string_handler_t string_handler; |
| 101 | string_handler_t number_handler; | 101 | string_handler_t number_handler; |
| 102 | bool_handler_t bool_handler; | 102 | bool_handler_t bool_handler; |
| 103 | - void_handler_t dict_start_handler; | 103 | + json_handler_t dict_start_handler; |
| 104 | void_handler_t dict_end_handler; | 104 | void_handler_t dict_end_handler; |
| 105 | void_handler_t array_start_handler; | 105 | void_handler_t array_start_handler; |
| 106 | void_handler_t array_end_handler; | 106 | void_handler_t array_end_handler; |
libtests/json_handler.cc
| @@ -49,8 +49,7 @@ static std::shared_ptr<JSONHandler> make_all_handler() | @@ -49,8 +49,7 @@ static std::shared_ptr<JSONHandler> make_all_handler() | ||
| 49 | { | 49 | { |
| 50 | auto h = std::make_shared<JSONHandler>(); | 50 | auto h = std::make_shared<JSONHandler>(); |
| 51 | h->addDictHandlers( | 51 | h->addDictHandlers( |
| 52 | - make_print_message("dict begin"), | ||
| 53 | - make_print_message("dict end")); | 52 | + print_json, make_print_message("dict end")); |
| 54 | auto h1 = std::make_shared<JSONHandler>(); | 53 | auto h1 = std::make_shared<JSONHandler>(); |
| 55 | h1->addStringHandler(print_string); | 54 | h1->addStringHandler(print_string); |
| 56 | h->addDictKeyHandler("one", h1); | 55 | h->addDictKeyHandler("one", h1); |
| @@ -77,13 +76,11 @@ static std::shared_ptr<JSONHandler> make_all_handler() | @@ -77,13 +76,11 @@ static std::shared_ptr<JSONHandler> make_all_handler() | ||
| 77 | h5); | 76 | h5); |
| 78 | auto h6 = std::make_shared<JSONHandler>(); | 77 | auto h6 = std::make_shared<JSONHandler>(); |
| 79 | h6->addDictHandlers( | 78 | h6->addDictHandlers( |
| 80 | - make_print_message("dict begin"), | ||
| 81 | - make_print_message("dict end")); | 79 | + print_json, make_print_message("dict end")); |
| 82 | auto h6a = std::make_shared<JSONHandler>(); | 80 | auto h6a = std::make_shared<JSONHandler>(); |
| 83 | h6->addDictKeyHandler("a", h6a); | 81 | h6->addDictKeyHandler("a", h6a); |
| 84 | h6a->addDictHandlers( | 82 | h6a->addDictHandlers( |
| 85 | - make_print_message("dict begin"), | ||
| 86 | - make_print_message("dict end")); | 83 | + print_json, make_print_message("dict end")); |
| 87 | auto h6ab = std::make_shared<JSONHandler>(); | 84 | auto h6ab = std::make_shared<JSONHandler>(); |
| 88 | h6a->addDictKeyHandler("b", h6ab); | 85 | h6a->addDictKeyHandler("b", h6ab); |
| 89 | auto h6ax = std::make_shared<JSONHandler>(); | 86 | auto h6ax = std::make_shared<JSONHandler>(); |
libtests/qtest/json_handler/json_handler.out
| 1 | -- scalar -- | 1 | -- scalar -- |
| 2 | .: string: potato | 2 | .: string: potato |
| 3 | -- all -- | 3 | -- all -- |
| 4 | -.: json: dict begin | 4 | +.: json: { |
| 5 | + "five": [ | ||
| 6 | + "x", | ||
| 7 | + false, | ||
| 8 | + "y", | ||
| 9 | + null, | ||
| 10 | + true | ||
| 11 | + ], | ||
| 12 | + "four": [ | ||
| 13 | + "a", | ||
| 14 | + 1 | ||
| 15 | + ], | ||
| 16 | + "one": "potato", | ||
| 17 | + "phour": null, | ||
| 18 | + "six": { | ||
| 19 | + "a": { | ||
| 20 | + "Q": "baaa", | ||
| 21 | + "b": "quack" | ||
| 22 | + }, | ||
| 23 | + "b": "moo" | ||
| 24 | + }, | ||
| 25 | + "three": true, | ||
| 26 | + "two": 3.14 | ||
| 27 | +} | ||
| 5 | .five: json: array begin | 28 | .five: json: array begin |
| 6 | .five[0]: string: x | 29 | .five[0]: string: x |
| 7 | .five[1]: bool: false | 30 | .five[1]: bool: false |
| @@ -15,8 +38,17 @@ | @@ -15,8 +38,17 @@ | ||
| 15 | ] | 38 | ] |
| 16 | .one: string: potato | 39 | .one: string: potato |
| 17 | .phour: json: null | 40 | .phour: json: null |
| 18 | -.six: json: dict begin | ||
| 19 | -.six.a: json: dict begin | 41 | +.six: json: { |
| 42 | + "a": { | ||
| 43 | + "Q": "baaa", | ||
| 44 | + "b": "quack" | ||
| 45 | + }, | ||
| 46 | + "b": "moo" | ||
| 47 | +} | ||
| 48 | +.six.a: json: { | ||
| 49 | + "Q": "baaa", | ||
| 50 | + "b": "quack" | ||
| 51 | +} | ||
| 20 | .six.a.Q: json: "baaa" | 52 | .six.a.Q: json: "baaa" |
| 21 | .six.a.b: string: quack | 53 | .six.a.b: string: quack |
| 22 | .six.a: json: dict end | 54 | .six.a: json: dict end |
| @@ -27,5 +59,7 @@ | @@ -27,5 +59,7 @@ | ||
| 27 | .: json: dict end | 59 | .: json: dict end |
| 28 | -- errors -- | 60 | -- errors -- |
| 29 | bad type at top: JSON handler: value at . is not of expected type | 61 | bad type at top: JSON handler: value at . is not of expected type |
| 30 | -.: json: dict begin | 62 | +.: json: { |
| 63 | + "x": "y" | ||
| 64 | +} | ||
| 31 | unexpected key: JSON handler found unexpected key x in object at . | 65 | unexpected key: JSON handler found unexpected key x in object at . |