From d4713a818c4a5d11e607384c5baf8584dcf79700 Mon Sep 17 00:00:00 2001 From: Magdalena Schreter Date: Mon, 23 May 2022 23:36:08 +0200 Subject: [PATCH] Demangle json during output Co-authored-by: Peter Munch --- .../incompatibilities/20220525SchreterMunch | 7 ++++ source/base/parameter_handler.cc | 34 ++++++++++++++++++- .../parameter_acceptor_09.output | 2 +- .../parameter_handler_27.output | 6 ++-- .../parameter_handler_28.output | 6 ++-- .../parameter_handler_read_json.output | 12 +++---- .../parameter_handler_write_json.output | 24 ++++++------- 7 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 doc/news/changes/incompatibilities/20220525SchreterMunch diff --git a/doc/news/changes/incompatibilities/20220525SchreterMunch b/doc/news/changes/incompatibilities/20220525SchreterMunch new file mode 100644 index 0000000000..c8a2dbf28d --- /dev/null +++ b/doc/news/changes/incompatibilities/20220525SchreterMunch @@ -0,0 +1,7 @@ +Changed: For ParameterHandler::OutputStyle::JSON or +ParameterHandler::OutputStyle::ShortJSON, the function +ParameterHandler::print_parameters() demangles now the +parameters before printing the output. Users do not +need to demangle the output themselves anymore. +
+(Magdalena Schreter, Peter Munch, 2022/05/25) diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 1eaf231056..f22211794f 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -333,6 +333,36 @@ namespace } } + /** + * Demangle all parameters recursively and attach them to @p tree_out. + * @p parameter_node indicates, whether a given node @p tree_in + * is a parameter node (as opposed to being a subsection or alias + * node). + */ + void + recursively_demangle(const boost::property_tree::ptree &tree_in, + boost::property_tree::ptree & tree_out, + const bool parameter_node = false) + { + for (const auto &p : tree_in) + { + if (parameter_node) + { + tree_out.put_child(p.first, p.second); + } + else + { + boost::property_tree::ptree temp; + + if (const auto val = p.second.get_value_optional()) + temp.put_value(*val); + + recursively_demangle(p.second, temp, is_parameter_node(p.second)); + tree_out.put_child(demangle(p.first), temp); + } + } + } + /** * Assert validity of of given output @p style. */ @@ -1308,7 +1338,9 @@ ParameterHandler::print_parameters(std::ostream & out, if ((style & JSON) != 0) { - write_json(out, current_entries); + boost::property_tree::ptree current_entries_damangled; + recursively_demangle(current_entries, current_entries_damangled); + write_json(out, current_entries_damangled); return out; } diff --git a/tests/parameter_handler/parameter_acceptor_09.output b/tests/parameter_handler/parameter_acceptor_09.output index 5dda9338fa..ca7aa0a934 100644 --- a/tests/parameter_handler/parameter_acceptor_09.output +++ b/tests/parameter_handler/parameter_acceptor_09.output @@ -15,7 +15,7 @@ DEAL::Generate and read input.json { "Test": { - "A_20point": + "A point": { "value": "0, 0, 0", "default_value": "0, 0, 0", diff --git a/tests/parameter_handler/parameter_handler_27.output b/tests/parameter_handler/parameter_handler_27.output index 890ec1bca0..db5c34a362 100644 --- a/tests/parameter_handler/parameter_handler_27.output +++ b/tests/parameter_handler/parameter_handler_27.output @@ -20,7 +20,7 @@ DEAL::======================================== { "Testing": { - "A_20bool": + "A bool": { "value": "true", "default_value": "true", @@ -29,7 +29,7 @@ DEAL::======================================== "pattern_description": "[Bool]", "actions": "1" }, - "A_20double": + "A double": { "value": "1", "default_value": "1", @@ -38,7 +38,7 @@ DEAL::======================================== "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", "actions": "0" }, - "A_20string": + "A string": { "value": "Ciao", "default_value": "Ciao", diff --git a/tests/parameter_handler/parameter_handler_28.output b/tests/parameter_handler/parameter_handler_28.output index 2ed6063af1..76f1735884 100644 --- a/tests/parameter_handler/parameter_handler_28.output +++ b/tests/parameter_handler/parameter_handler_28.output @@ -13,10 +13,10 @@ DEAL::======================================== DEAL::ShortJSON format: DEAL::======================================== { - "A_20double": "1", - "Section_20two": + "A double": "1", + "Section two": { - "Another_20double": "2" + "Another double": "2" } } diff --git a/tests/parameter_handler/parameter_handler_read_json.output b/tests/parameter_handler/parameter_handler_read_json.output index 6b2b26bbdb..70095fb249 100644 --- a/tests/parameter_handler/parameter_handler_read_json.output +++ b/tests/parameter_handler/parameter_handler_read_json.output @@ -18,9 +18,9 @@ "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]", "actions": "1" }, - "Testing_25testing": + "Testing%testing": { - "double_2bdouble": + "double+double": { "value": "7.1415926", "default_value": "6.14159", @@ -29,7 +29,7 @@ "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", "actions": "6" }, - "int_2aint": + "int*int": { "value": "2", "default_value": "2", @@ -38,7 +38,7 @@ "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]", "actions": "5" }, - "string_26list": + "string&list": { "value": "< & > ; \/", "default_value": "< & > ; \/", @@ -50,7 +50,7 @@ }, "ss1": { - "double_201": + "double 1": { "value": "2.234", "default_value": "1.234", @@ -61,7 +61,7 @@ }, "ss2": { - "double_202": + "double 2": { "value": "5.321", "default_value": "4.321", diff --git a/tests/parameter_handler/parameter_handler_write_json.output b/tests/parameter_handler/parameter_handler_write_json.output index 824c2c36e5..ab55c98c3e 100644 --- a/tests/parameter_handler/parameter_handler_write_json.output +++ b/tests/parameter_handler/parameter_handler_write_json.output @@ -16,9 +16,9 @@ "pattern": "1", "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]" }, - "Testing_25testing": + "Testing%testing": { - "double_2bdouble": + "double+double": { "value": "6.1415926", "default_value": "6.1415926", @@ -26,7 +26,7 @@ "pattern": "6", "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]" }, - "int_2aint": + "int*int": { "value": "2", "default_value": "2", @@ -34,7 +34,7 @@ "pattern": "5", "pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]" }, - "string_26list": + "string&list": { "value": "< & > ; \/", "default_value": "< & > ; \/", @@ -45,7 +45,7 @@ }, "ss1": { - "double_201": + "double 1": { "value": "1.234", "default_value": "1.234", @@ -55,7 +55,7 @@ }, "ss2": { - "double_202": + "double 2": { "value": "4.321", "default_value": "4.321", @@ -70,18 +70,18 @@ { "int1": "1", "int2": "2", - "Testing_25testing": + "Testing%testing": { - "double_2bdouble": "6.1415926", - "int_2aint": "2", - "string_26list": "< & > ; \/" + "double+double": "6.1415926", + "int*int": "2", + "string&list": "< & > ; \/" }, "ss1": { - "double_201": "1.234", + "double 1": "1.234", "ss2": { - "double_202": "4.321" + "double 2": "4.321" } } } -- 2.39.5