Skip to content

Commit

Permalink
Update documentation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Dec 17, 2024
1 parent 8467356 commit a04fde9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
48 changes: 45 additions & 3 deletions include/openPMD/auxiliary/JSON.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,58 @@ namespace json
* users to overwrite default options, while keeping any other ones.
*
* @param defaultValue A string containing either a JSON or a TOML dataset.
* If the string begins with an `@`, the JSON/TOML dataset will be
* read from the filesystem at the specified path.
* @param overwrite A string containing either a JSON or TOML dataset (does
* not need to be the same as `defaultValue`).
* not need to be the same as `defaultValue`).
* If the string begins with an `@`, the JSON/TOML dataset will be
* read from the filesystem at the specified path.
* @return std::string The merged dataset, according to the above rules. If
* `defaultValue` was a JSON dataset, then as a JSON string, otherwise as a
* TOML string.
* `overwrite` was a JSON dataset, then as a JSON string, otherwise
* as a TOML string.
*/
std::string
merge(std::string const &defaultValue, std::string const &overwrite);

#if openPMD_HAVE_MPI
/**
* @brief Merge two JSON/TOML datasets into one.
*
* Merging rules:
* 1. If both `defaultValue` and `overwrite` are JSON/TOML objects, then the
* resulting JSON/TOML object will contain the union of both objects'
* keys. If a key is specified in both objects, the values corresponding
* to the key are merged recursively. Keys that point to a null value
* after this procedure will be pruned.
* 2. In any other case, the JSON/TOML dataset `defaultValue` is replaced in
* its entirety with the JSON/TOML dataset `overwrite`.
*
* Note that item 2 means that datasets of different type will replace each
* other without error.
* It also means that array types will replace each other without any notion
* of appending or merging.
*
* Possible use case:
* An application uses openPMD-api and wants to do the following:
* 1. Set some default backend options as JSON/TOML parameters.
* 2. Let its users specify custom backend options additionally.
*
* By using the json::merge() function, this application can then allow
* users to overwrite default options, while keeping any other ones.
*
* @param defaultValue A string containing either a JSON or a TOML dataset.
* If the string begins with an `@`, the JSON/TOML dataset will be
* read in parallel (using the MPI Communicator)
* from the filesystem at the specified path.
* @param overwrite A string containing either a JSON or TOML dataset (does
* not need to be the same as `defaultValue`).
* If the string begins with an `@`, the JSON/TOML dataset will be
* read in parallel (using the MPI Communicator)
* from the filesystem at the specified path.
* @return std::string The merged dataset, according to the above rules. If
* `overwrite` was a JSON dataset, then as a JSON string, otherwise
* as a TOML string.
*/
std::string merge(
std::string const &defaultValue,
std::string const &overwrite,
Expand Down
8 changes: 7 additions & 1 deletion src/binding/python/Series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,16 @@ users to overwrite default options, while keeping any other ones.
Parameters:
* default_value: A string containing either a JSON or a TOML dataset.
If the string begins with an `@`, the JSON/TOML dataset will be
read from the filesystem at the specified path.
An MPI communicator can be passed to read in parallel.
* overwrite: A string containing either a JSON or TOML dataset (does
not need to be the same as `defaultValue`).
If the string begins with an `@`, the JSON/TOML dataset will be
read from the filesystem at the specified path.
An MPI communicator can be passed to read in parallel.
* returns: The merged dataset, according to the above rules.
If `defaultValue` was a JSON dataset, then as a JSON string,
If `overwrite` was a JSON dataset, then as a JSON string,
otherwise as a TOML string.
)END"[1];

Expand Down
4 changes: 2 additions & 2 deletions test/JSONTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ right = "val"
}();

REQUIRE(json::merge(leftJson, rightJson) == resJson);
REQUIRE(json::merge(leftJson, rightToml) == resJson);
REQUIRE(sort_lines(json::merge(leftToml, rightJson)) == resToml);
REQUIRE(sort_lines(json::merge(leftJson, rightToml)) == resToml);
REQUIRE(json::merge(leftToml, rightJson) == resJson);
REQUIRE(sort_lines(json::merge(leftToml, rightToml)) == resToml);
}
}
Expand Down

0 comments on commit a04fde9

Please sign in to comment.