Skip to content

Commit 68dc005

Browse files
committed
Update documentation and tests
1 parent 16680dc commit 68dc005

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

include/openPMD/auxiliary/JSON.hpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,58 @@ namespace json
5959
* users to overwrite default options, while keeping any other ones.
6060
*
6161
* @param defaultValue A string containing either a JSON or a TOML dataset.
62+
* If the string begins with an `@`, the JSON/TOML dataset will be
63+
* read from the filesystem at the specified path.
6264
* @param overwrite A string containing either a JSON or TOML dataset (does
63-
* not need to be the same as `defaultValue`).
65+
* not need to be the same as `defaultValue`).
66+
* If the string begins with an `@`, the JSON/TOML dataset will be
67+
* read from the filesystem at the specified path.
6468
* @return std::string The merged dataset, according to the above rules. If
65-
* `defaultValue` was a JSON dataset, then as a JSON string, otherwise as a
66-
* TOML string.
69+
* `overwrite` was a JSON dataset, then as a JSON string, otherwise
70+
* as a TOML string.
6771
*/
6872
std::string
6973
merge(std::string const &defaultValue, std::string const &overwrite);
7074

7175
#if openPMD_HAVE_MPI
76+
/**
77+
* @brief Merge two JSON/TOML datasets into one.
78+
*
79+
* Merging rules:
80+
* 1. If both `defaultValue` and `overwrite` are JSON/TOML objects, then the
81+
* resulting JSON/TOML object will contain the union of both objects'
82+
* keys. If a key is specified in both objects, the values corresponding
83+
* to the key are merged recursively. Keys that point to a null value
84+
* after this procedure will be pruned.
85+
* 2. In any other case, the JSON/TOML dataset `defaultValue` is replaced in
86+
* its entirety with the JSON/TOML dataset `overwrite`.
87+
*
88+
* Note that item 2 means that datasets of different type will replace each
89+
* other without error.
90+
* It also means that array types will replace each other without any notion
91+
* of appending or merging.
92+
*
93+
* Possible use case:
94+
* An application uses openPMD-api and wants to do the following:
95+
* 1. Set some default backend options as JSON/TOML parameters.
96+
* 2. Let its users specify custom backend options additionally.
97+
*
98+
* By using the json::merge() function, this application can then allow
99+
* users to overwrite default options, while keeping any other ones.
100+
*
101+
* @param defaultValue A string containing either a JSON or a TOML dataset.
102+
* If the string begins with an `@`, the JSON/TOML dataset will be
103+
* read in parallel (using the MPI Communicator)
104+
* from the filesystem at the specified path.
105+
* @param overwrite A string containing either a JSON or TOML dataset (does
106+
* not need to be the same as `defaultValue`).
107+
* If the string begins with an `@`, the JSON/TOML dataset will be
108+
* read in parallel (using the MPI Communicator)
109+
* from the filesystem at the specified path.
110+
* @return std::string The merged dataset, according to the above rules. If
111+
* `overwrite` was a JSON dataset, then as a JSON string, otherwise
112+
* as a TOML string.
113+
*/
72114
std::string merge(
73115
std::string const &defaultValue,
74116
std::string const &overwrite,

src/binding/python/Series.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,16 @@ users to overwrite default options, while keeping any other ones.
421421
422422
Parameters:
423423
* default_value: A string containing either a JSON or a TOML dataset.
424+
If the string begins with an `@`, the JSON/TOML dataset will be
425+
read from the filesystem at the specified path.
426+
An MPI communicator can be passed to read in parallel.
424427
* overwrite: A string containing either a JSON or TOML dataset (does
425428
not need to be the same as `defaultValue`).
429+
If the string begins with an `@`, the JSON/TOML dataset will be
430+
read from the filesystem at the specified path.
431+
An MPI communicator can be passed to read in parallel.
426432
* returns: The merged dataset, according to the above rules.
427-
If `defaultValue` was a JSON dataset, then as a JSON string,
433+
If `overwrite` was a JSON dataset, then as a JSON string,
428434
otherwise as a TOML string.
429435
)END"[1];
430436

test/JSONTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ right = "val"
215215
}();
216216

217217
REQUIRE(json::merge(leftJson, rightJson) == resJson);
218-
REQUIRE(json::merge(leftJson, rightToml) == resJson);
219-
REQUIRE(sort_lines(json::merge(leftToml, rightJson)) == resToml);
218+
REQUIRE(sort_lines(json::merge(leftJson, rightToml)) == resToml);
219+
REQUIRE(json::merge(leftToml, rightJson) == resJson);
220220
REQUIRE(sort_lines(json::merge(leftToml, rightToml)) == resToml);
221221
}
222222
}

0 commit comments

Comments
 (0)