Skip to content

Commit 06da2d5

Browse files
committed
Make JSON and TOML look like two different backends
1 parent 960ab21 commit 06da2d5

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ set(openPMD_EXAMPLE_NAMES
817817
10_streaming_read
818818
12_span_write
819819
13_write_dynamic_configuration
820-
14_json_template
820+
14_toml_template
821821
)
822822
set(openPMD_PYTHON_EXAMPLE_NAMES
823823
2_read_serial

examples/14_json_template.cpp renamed to examples/14_toml_template.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ int main()
55
std::string config = R"(
66
{
77
"iteration_encoding": "variable_based",
8-
"json": {
8+
"toml": {
99
"mode": "template"
1010
}
1111
}
1212
)";
1313

1414
openPMD::Series writeTemplate(
15-
"../samples/jsonTemplate.toml", openPMD::Access::CREATE, config);
15+
"../samples/tomlTemplate.toml", openPMD::Access::CREATE, config);
1616
auto iteration = writeTemplate.writeIterations()[0];
1717

1818
openPMD::Dataset ds{openPMD::Datatype::FLOAT, {5, 5}};

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,27 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
126126
, m_fileFormat{format}
127127
, m_originalExtension{std::move(originalExtension)}
128128
{
129-
if (config.json().contains("json"))
129+
std::string const configLocation = [&]() {
130+
switch (format)
131+
{
132+
case FileFormat::Json:
133+
return "json";
134+
case FileFormat::Toml:
135+
return "toml";
136+
}
137+
throw std::runtime_error("Unreachable!");
138+
}();
139+
if (config.json().contains(configLocation))
130140
{
131-
auto jsonConfig = config["json"];
141+
auto jsonConfig = config[configLocation];
132142
if (jsonConfig.json().contains("mode"))
133143
{
134144
auto modeOption = openPMD::json::asLowerCaseStringDynamic(
135145
jsonConfig["mode"].json());
136146
if (!modeOption.has_value())
137147
{
138148
throw error::BackendConfigSchema(
139-
{"json", "mode"},
149+
{configLocation, "mode"},
140150
"Invalid value of non-string type (accepted values are "
141151
"'dataset' and 'template'.");
142152
}
@@ -152,7 +162,7 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
152162
else
153163
{
154164
throw error::BackendConfigSchema(
155-
{"json", "mode"},
165+
{configLocation, "mode"},
156166
"Invalid value: '" + mode +
157167
"' (accepted values are 'dataset' and 'template'.");
158168
}
@@ -164,13 +174,13 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
164174
{
165175
case openPMD::json::SupportedLanguages::JSON:
166176
std::cerr << "Warning: parts of the backend configuration for "
167-
"JSON backend remain unused:\n"
177+
"JSON/TOML backend remain unused:\n"
168178
<< shadow << std::endl;
169179
break;
170180
case openPMD::json::SupportedLanguages::TOML: {
171181
auto asToml = openPMD::json::jsonToToml(shadow);
172182
std::cerr << "Warning: parts of the backend configuration for "
173-
"JSON backend remain unused:\n"
183+
"JSON/TOML backend remain unused:\n"
174184
<< asToml << std::endl;
175185
break;
176186
}

src/auxiliary/JSON.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ std::optional<std::string> asLowerCaseStringDynamic(nlohmann::json const &value)
510510
return maybeString;
511511
}
512512

513-
std::vector<std::string> backendKeys{"adios1", "adios2", "json", "hdf5"};
513+
std::vector<std::string> backendKeys{
514+
"adios1", "adios2", "json", "toml", "hdf5"};
514515

515516
void warnGlobalUnusedOptions(TracingJSON const &config)
516517
{

test/SerialIOTest.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,11 +1517,16 @@ TEST_CASE("dtype_test", "[serial]")
15171517
{
15181518
for (auto const &t : testedFileExtensions())
15191519
{
1520-
if (t == "toml" || t == "json")
1520+
if (t == "json")
15211521
{
15221522
dtype_test(t);
15231523
dtype_test(t, R"(json.mode = "template")");
15241524
}
1525+
else if (t == "toml")
1526+
{
1527+
dtype_test(t);
1528+
dtype_test(t, R"(toml.mode = "template")");
1529+
}
15251530
else
15261531
{
15271532
dtype_test(t);

0 commit comments

Comments
 (0)