Commit ce3406e93f0de0946d2abed6179579caf1433d4e

Authored by Jay Berkenbilt
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.
libqpdf/JSONHandler.cc
... ... @@ -49,7 +49,7 @@ JSONHandler::addBoolHandler(bool_handler_t fn)
49 49 }
50 50  
51 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 54 this->m->h.dict_start_handler = start_fn;
55 55 this->m->h.dict_end_handler = end_fn;
... ... @@ -111,7 +111,7 @@ JSONHandler::handle(std::string const& path, JSON j)
111 111 }
112 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 115 std::string path_base = path;
116 116 if (path_base != ".")
117 117 {
... ...
libqpdf/qpdf/JSONHandler.hh
... ... @@ -58,7 +58,7 @@ class JSONHandler
58 58 void addBoolHandler(bool_handler_t fn);
59 59  
60 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 62 QPDF_DLL
63 63 void addDictKeyHandler(
64 64 std::string const& key, std::shared_ptr<JSONHandler>);
... ... @@ -100,7 +100,7 @@ class JSONHandler
100 100 string_handler_t string_handler;
101 101 string_handler_t number_handler;
102 102 bool_handler_t bool_handler;
103   - void_handler_t dict_start_handler;
  103 + json_handler_t dict_start_handler;
104 104 void_handler_t dict_end_handler;
105 105 void_handler_t array_start_handler;
106 106 void_handler_t array_end_handler;
... ...
libtests/json_handler.cc
... ... @@ -49,8 +49,7 @@ static std::shared_ptr&lt;JSONHandler&gt; make_all_handler()
49 49 {
50 50 auto h = std::make_shared<JSONHandler>();
51 51 h->addDictHandlers(
52   - make_print_message("dict begin"),
53   - make_print_message("dict end"));
  52 + print_json, make_print_message("dict end"));
54 53 auto h1 = std::make_shared<JSONHandler>();
55 54 h1->addStringHandler(print_string);
56 55 h->addDictKeyHandler("one", h1);
... ... @@ -77,13 +76,11 @@ static std::shared_ptr&lt;JSONHandler&gt; make_all_handler()
77 76 h5);
78 77 auto h6 = std::make_shared<JSONHandler>();
79 78 h6->addDictHandlers(
80   - make_print_message("dict begin"),
81   - make_print_message("dict end"));
  79 + print_json, make_print_message("dict end"));
82 80 auto h6a = std::make_shared<JSONHandler>();
83 81 h6->addDictKeyHandler("a", h6a);
84 82 h6a->addDictHandlers(
85   - make_print_message("dict begin"),
86   - make_print_message("dict end"));
  83 + print_json, make_print_message("dict end"));
87 84 auto h6ab = std::make_shared<JSONHandler>();
88 85 h6a->addDictKeyHandler("b", h6ab);
89 86 auto h6ax = std::make_shared<JSONHandler>();
... ...
libtests/qtest/json_handler/json_handler.out
1 1 -- scalar --
2 2 .: string: potato
3 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 28 .five: json: array begin
6 29 .five[0]: string: x
7 30 .five[1]: bool: false
... ... @@ -15,8 +38,17 @@
15 38 ]
16 39 .one: string: potato
17 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 52 .six.a.Q: json: "baaa"
21 53 .six.a.b: string: quack
22 54 .six.a: json: dict end
... ... @@ -27,5 +59,7 @@
27 59 .: json: dict end
28 60 -- errors --
29 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 65 unexpected key: JSON handler found unexpected key x in object at .
... ...