From 1db0a7ffcee6f6ae6bd3298a960665378d304fa1 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 20 Jan 2022 08:53:53 -0500 Subject: JSONHandler: rework dictionary and array handlers --- libtests/json_handler.cc | 44 +++++++++++++++++++--------- libtests/qtest/json_handler/json_handler.out | 9 ++++++ 2 files changed, 39 insertions(+), 14 deletions(-) (limited to 'libtests') diff --git a/libtests/json_handler.cc b/libtests/json_handler.cc index d5e6aea3..7f6349f9 100644 --- a/libtests/json_handler.cc +++ b/libtests/json_handler.cc @@ -28,6 +28,13 @@ static void print_json(std::string const& path, JSON value) std::cout << path << ": json: " << value.unparse() << std::endl; } +static JSONHandler::void_handler_t make_print_message(std::string msg) +{ + return [msg](std::string const& path) { + std::cout << path << ": json: " << msg << std::endl; + }; +} + static void test_scalar() { std::cout << "-- scalar --" << std::endl; @@ -40,41 +47,50 @@ static void test_scalar() static std::shared_ptr make_all_handler() { auto h = std::make_shared(); - auto& m = h->addDictHandlers(); + h->addDictHandlers( + make_print_message("dict begin"), + make_print_message("dict end")); auto h1 = std::make_shared(); h1->addStringHandler(print_string); - m["one"] = h1; + h->addDictKeyHandler("one", h1); auto h2 = std::make_shared(); h2->addNumberHandler(print_number); - m["two"] = h2; + h->addDictKeyHandler("two", h2); auto h3 = std::make_shared(); h3->addBoolHandler(print_bool); - m["three"] = h3; + h->addDictKeyHandler("three", h3); auto h4 = std::make_shared(); h4->addAnyHandler(print_json); - m["four"] = h4; - m["phour"] = h4; // share h4 + h->addDictKeyHandler("four", h4); + h->addDictKeyHandler("phour", h4); // share h4 auto h5 = std::make_shared(); // Allow to be either string or bool h5->addBoolHandler(print_bool); h5->addStringHandler(print_string); h5->addNullHandler(print_null); auto h5s = std::make_shared(); - m["five"] = h5s; - h5s->addArrayHandler(h5); + h->addDictKeyHandler("five", h5s); + h5s->addArrayHandlers( + make_print_message("array begin"), + make_print_message("array end"), + h5); auto h6 = std::make_shared(); - auto& m6 = h6->addDictHandlers(); + h6->addDictHandlers( + make_print_message("dict begin"), + make_print_message("dict end")); auto h6a = std::make_shared(); - m6["a"] = h6a; - auto& m6a = h6a->addDictHandlers(); + h6->addDictKeyHandler("a", h6a); + h6a->addDictHandlers( + make_print_message("dict begin"), + make_print_message("dict end")); auto h6ab = std::make_shared(); - m6a["b"] = h6ab; + h6a->addDictKeyHandler("b", h6ab); auto h6ax = std::make_shared(); h6ax->addAnyHandler(print_json); h6a->addFallbackDictHandler(h6ax); - m6["b"] = h6ab; // share + h6->addDictKeyHandler("b", h6ab); // share h6ab->addStringHandler(print_string); - m["six"] = h6; + h->addDictKeyHandler("six", h6); return h; } diff --git a/libtests/qtest/json_handler/json_handler.out b/libtests/qtest/json_handler/json_handler.out index 13554af3..368c94b5 100644 --- a/libtests/qtest/json_handler/json_handler.out +++ b/libtests/qtest/json_handler/json_handler.out @@ -1,22 +1,31 @@ -- scalar -- .: string: potato -- all -- +.: json: dict begin +.five: json: array begin .five[0]: string: x .five[1]: bool: false .five[2]: string: y .five[3]: null .five[4]: bool: true +.five: json: array end .four: json: [ "a", 1 ] .one: string: potato .phour: json: null +.six: json: dict begin +.six.a: json: dict begin .six.a.Q: json: "baaa" .six.a.b: string: quack +.six.a: json: dict end .six.b: string: moo +.six: json: dict end .three: bool: true .two: number: 3.14 +.: json: dict end -- errors -- bad type at top: JSON handler: value at . is not of expected type +.: json: dict begin unexpected key: JSON handler found unexpected key x in object at . -- cgit v1.2.3-54-g00ecf