Skip to content

Commit

Permalink
ParameterHandler::parse_input_from_json(): mangle entries internally
Browse files Browse the repository at this point in the history
Co-authored-by: Magdalena Schreter-Fleischhacker <schreter.magdalena@gmail.com>
  • Loading branch information
peterrum and mschreter committed Jan 25, 2024
1 parent 2c950bf commit fe8535d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
4 changes: 4 additions & 0 deletions doc/news/changes/incompatibilities/20240126SchreterMunch
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Changed:`ParameterHandler::parse_input_from_json()` reads now a json-file
without mangled parameter names.
<br>
(Magdalena Schreter, Peter Munch, 2024/01/26)
26 changes: 15 additions & 11 deletions source/base/parameter_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ namespace
}

/**
* Demangle all parameters recursively and attach them to @p tree_out.
* Mangle or demangle all parameters recursively and attach them to @p tree_out.
* @p is_parameter_or_alias_node indicates, whether a given node
* @p tree_in is a parameter node or an alias node (as opposed to being
* a subsection).
*/
void
recursively_demangle(const boost::property_tree::ptree &tree_in,
boost::property_tree::ptree &tree_out,
const bool is_parameter_or_alias_node = false)
recursively_mangle_or_demangle(const boost::property_tree::ptree &tree_in,
boost::property_tree::ptree &tree_out,
const bool is_parameter_or_alias_node = false)
{
for (const auto &p : tree_in)
{
Expand All @@ -355,10 +355,10 @@ namespace
if (const auto val = p.second.get_value_optional<std::string>())
temp.put_value<std::string>(*val);

recursively_demangle(p.second,
temp,
is_parameter_node(p.second) ||
is_alias_node(p.second));
recursively_mangle_or_demangle(p.second,
temp,
is_parameter_node(p.second) ||
is_alias_node(p.second));
tree_out.put_child(demangle(p.first), temp);
}
}
Expand Down Expand Up @@ -808,9 +808,12 @@ ParameterHandler::parse_input_from_json(std::istream &in,
}

// The xml function is reused to read in the xml into the parameter file.
// This means that only mangled files can be read.
// This function can only read mangled files. Therefore, we create a mangeled
// tree first.
boost::property_tree::ptree node_tree_mangled;
recursively_mangle_or_demangle(node_tree, node_tree_mangled);
read_xml_recursively(
node_tree, "", path_separator, patterns, skip_undefined, *this);
node_tree_mangled, "", path_separator, patterns, skip_undefined, *this);
}


Expand Down Expand Up @@ -1343,7 +1346,8 @@ ParameterHandler::print_parameters(std::ostream &out,
if ((style & JSON) != 0)
{
boost::property_tree::ptree current_entries_damangled;
recursively_demangle(current_entries, current_entries_damangled);
recursively_mangle_or_demangle(current_entries,
current_entries_damangled);
write_json(out, current_entries_damangled);
return out;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@



// check ParameterHandler::parse_input_from_xml
// check ParameterHandler::parse_input_from_json

#include <deal.II/base/parameter_handler.h>

Expand Down Expand Up @@ -61,7 +61,7 @@ main()
prm.leave_subsection();

// read from json
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json.prm");
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_01.json");
prm.parse_input_from_json(in);

Assert(int1 == 2, ExcNotImplemented());
Expand Down
2 changes: 1 addition & 1 deletion tests/parameter_handler/parameter_handler_read_json_02.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ main()
prm.leave_subsection();

// read from json
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.prm");
std::ifstream in(SOURCE_DIR "/prm/parameter_handler_read_json_02.json");
prm.parse_input_from_json(in, true);

AssertDimension(int1, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"ss1":
{
"double_201":
"double 1":
{
"value": "2.234",
"default_value": "1.234",
Expand All @@ -28,7 +28,7 @@
},
"ss2":
{
"double_202":
"double 2":
{
"value": "5.321",
"default_value": "4.321",
Expand All @@ -38,25 +38,25 @@
}
}
},
"Testing_25testing":
"Testing%testing":
{
"string_26list":
"string&list":
{
"value": "< & > ; \/",
"default_value": "< & > ; \/",
"documentation": "docs 1",
"pattern": "4",
"pattern_description": "[Anything]"
},
"int_2aint":
"int*int":
{
"value": "2",
"default_value": "2",
"documentation": "",
"pattern": "5",
"pattern_description": "[Integer range -2147483648...2147483647 (inclusive)]"
},
"double_2bdouble":
"double+double":
{
"value": "7.1415926",
"default_value": "6.1415926",
Expand Down

0 comments on commit fe8535d

Please sign in to comment.