Skip to content

Commit a33c59a

Browse files
committed
Select template mode via JSON param
1 parent ab29e02 commit a33c59a

File tree

6 files changed

+74
-11
lines changed

6 files changed

+74
-11
lines changed

examples/14_json_template.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
int main()
44
{
5+
std::string config = R"(
6+
iteration_encoding = "variable_based"
7+
8+
[json]
9+
mode = "template"
10+
)";
11+
512
openPMD::Series writeTemplate(
6-
"../samples/jsonTemplate.json",
7-
openPMD::Access::CREATE,
8-
R"(iteration_encoding = "variable_based")");
13+
"../samples/jsonTemplate.json", openPMD::Access::CREATE, config);
914
auto iteration = writeTemplate.writeIterations()[0];
1015

1116
auto temperature =

include/openPMD/IO/JSON/JSONIOHandler.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323

2424
#include "openPMD/IO/AbstractIOHandler.hpp"
2525
#include "openPMD/IO/JSON/JSONIOHandlerImpl.hpp"
26+
#include "openPMD/auxiliary/JSON_internal.hpp"
2627

2728
namespace openPMD
2829
{
2930
class JSONIOHandler : public AbstractIOHandler
3031
{
3132
public:
32-
JSONIOHandler(std::string path, Access at);
33+
JSONIOHandler(
34+
std::string path, Access at, openPMD::json::TracingJSON config);
3335

3436
~JSONIOHandler() override;
3537

include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "openPMD/IO/Access.hpp"
2727
#include "openPMD/IO/JSON/JSONFilePosition.hpp"
2828
#include "openPMD/auxiliary/Filesystem.hpp"
29+
#include "openPMD/auxiliary/JSON_internal.hpp"
2930
#include "openPMD/config.hpp"
3031

3132
#include <nlohmann/json.hpp>
@@ -153,7 +154,8 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
153154
using json = nlohmann::json;
154155

155156
public:
156-
explicit JSONIOHandlerImpl(AbstractIOHandler *);
157+
explicit JSONIOHandlerImpl(
158+
AbstractIOHandler *, openPMD::json::TracingJSON config);
157159

158160
~JSONIOHandlerImpl() override;
159161

@@ -230,7 +232,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
230232
Template
231233
};
232234

233-
IOMode m_mode = IOMode::Template;
235+
IOMode m_mode = IOMode::Dataset;
234236

235237
// HELPER FUNCTIONS
236238

src/IO/AbstractIOHandlerHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ std::shared_ptr<AbstractIOHandler> createIOHandler<json::TracingJSON>(
115115
"ADIOS2", path, access, std::move(options), "ssc");
116116
case Format::JSON:
117117
return constructIOHandler<JSONIOHandler, openPMD_HAVE_JSON>(
118-
"JSON", path, access);
118+
"JSON", path, access, std::move(options));
119119
default:
120120
throw std::runtime_error(
121121
"Unknown file format! Did you specify a file ending?");

src/IO/JSON/JSONIOHandler.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ namespace openPMD
2525
{
2626
JSONIOHandler::~JSONIOHandler() = default;
2727

28-
JSONIOHandler::JSONIOHandler(std::string path, Access at)
29-
: AbstractIOHandler{path, at}, m_impl{JSONIOHandlerImpl{this}}
28+
JSONIOHandler::JSONIOHandler(
29+
std::string path, Access at, openPMD::json::TracingJSON jsonCfg)
30+
: AbstractIOHandler{path, at}, m_impl{this, std::move(jsonCfg)}
3031
{}
3132

3233
std::future<void> JSONIOHandler::flush(internal::FlushParams const &)

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,62 @@ namespace openPMD
5454
throw std::runtime_error((TEXT)); \
5555
}
5656

57-
JSONIOHandlerImpl::JSONIOHandlerImpl(AbstractIOHandler *handler)
57+
JSONIOHandlerImpl::JSONIOHandlerImpl(
58+
AbstractIOHandler *handler, openPMD::json::TracingJSON config)
5859
: AbstractIOHandlerImpl(handler)
59-
{}
60+
{
61+
if (config.json().contains("json"))
62+
{
63+
auto jsonConfig = config["json"];
64+
if (jsonConfig.json().contains("mode"))
65+
{
66+
auto modeOption = openPMD::json::asLowerCaseStringDynamic(
67+
jsonConfig["mode"].json());
68+
if (!modeOption.has_value())
69+
{
70+
throw error::BackendConfigSchema(
71+
{"json", "mode"},
72+
"Invalid value of non-string type (accepted values are "
73+
"'dataset' and 'template'.");
74+
}
75+
auto mode = modeOption.value();
76+
if (mode == "dataset")
77+
{
78+
m_mode = IOMode::Dataset;
79+
}
80+
else if (mode == "template")
81+
{
82+
m_mode = IOMode::Template;
83+
}
84+
else
85+
{
86+
throw error::BackendConfigSchema(
87+
{"json", "mode"},
88+
"Invalid value: '" + mode +
89+
"' (accepted values are 'dataset' and 'template'.");
90+
}
91+
}
92+
auto shadow = jsonConfig.invertShadow();
93+
if (shadow.size() > 0)
94+
{
95+
switch (jsonConfig.originallySpecifiedAs)
96+
{
97+
case openPMD::json::SupportedLanguages::JSON:
98+
std::cerr << "Warning: parts of the backend configuration for "
99+
"JSON backend remain unused:\n"
100+
<< shadow << std::endl;
101+
break;
102+
case openPMD::json::SupportedLanguages::TOML: {
103+
auto asToml = openPMD::json::jsonToToml(shadow);
104+
std::cerr << "Warning: parts of the backend configuration for "
105+
"JSON backend remain unused:\n"
106+
<< asToml << std::endl;
107+
break;
108+
}
109+
}
110+
}
111+
}
112+
}
60113

61114
JSONIOHandlerImpl::~JSONIOHandlerImpl()
62115
{

0 commit comments

Comments
 (0)