diff --git a/examples/14_json_template.cpp b/examples/14_json_template.cpp index 2641731d02..1e1a26e2a0 100644 --- a/examples/14_json_template.cpp +++ b/examples/14_json_template.cpp @@ -2,10 +2,15 @@ int main() { + std::string config = R"( +iteration_encoding = "variable_based" + +[json] +mode = "template" +)"; + openPMD::Series writeTemplate( - "../samples/jsonTemplate.json", - openPMD::Access::CREATE, - R"(iteration_encoding = "variable_based")"); + "../samples/jsonTemplate.json", openPMD::Access::CREATE, config); auto iteration = writeTemplate.writeIterations()[0]; auto temperature = diff --git a/include/openPMD/IO/JSON/JSONIOHandler.hpp b/include/openPMD/IO/JSON/JSONIOHandler.hpp index 37b00fa165..3c593afb42 100644 --- a/include/openPMD/IO/JSON/JSONIOHandler.hpp +++ b/include/openPMD/IO/JSON/JSONIOHandler.hpp @@ -23,13 +23,15 @@ #include "openPMD/IO/AbstractIOHandler.hpp" #include "openPMD/IO/JSON/JSONIOHandlerImpl.hpp" +#include "openPMD/auxiliary/JSON_internal.hpp" namespace openPMD { class JSONIOHandler : public AbstractIOHandler { public: - JSONIOHandler(std::string path, Access at); + JSONIOHandler( + std::string path, Access at, openPMD::json::TracingJSON config); ~JSONIOHandler() override; diff --git a/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp b/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp index c7ad57a6b9..6d1b8bdc01 100644 --- a/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp +++ b/include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp @@ -26,6 +26,7 @@ #include "openPMD/IO/Access.hpp" #include "openPMD/IO/JSON/JSONFilePosition.hpp" #include "openPMD/auxiliary/Filesystem.hpp" +#include "openPMD/auxiliary/JSON_internal.hpp" #include "openPMD/config.hpp" #include @@ -153,7 +154,8 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl using json = nlohmann::json; public: - explicit JSONIOHandlerImpl(AbstractIOHandler *); + explicit JSONIOHandlerImpl( + AbstractIOHandler *, openPMD::json::TracingJSON config); ~JSONIOHandlerImpl() override; @@ -230,7 +232,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl Template }; - IOMode m_mode = IOMode::Template; + IOMode m_mode = IOMode::Dataset; // HELPER FUNCTIONS diff --git a/src/IO/AbstractIOHandlerHelper.cpp b/src/IO/AbstractIOHandlerHelper.cpp index 22fa6af60f..31ebdeed60 100644 --- a/src/IO/AbstractIOHandlerHelper.cpp +++ b/src/IO/AbstractIOHandlerHelper.cpp @@ -115,7 +115,7 @@ std::shared_ptr createIOHandler( "ADIOS2", path, access, std::move(options), "ssc"); case Format::JSON: return constructIOHandler( - "JSON", path, access); + "JSON", path, access, std::move(options)); default: throw std::runtime_error( "Unknown file format! Did you specify a file ending?"); diff --git a/src/IO/JSON/JSONIOHandler.cpp b/src/IO/JSON/JSONIOHandler.cpp index 15d18194c7..c0a16f2077 100644 --- a/src/IO/JSON/JSONIOHandler.cpp +++ b/src/IO/JSON/JSONIOHandler.cpp @@ -25,8 +25,9 @@ namespace openPMD { JSONIOHandler::~JSONIOHandler() = default; -JSONIOHandler::JSONIOHandler(std::string path, Access at) - : AbstractIOHandler{path, at}, m_impl{JSONIOHandlerImpl{this}} +JSONIOHandler::JSONIOHandler( + std::string path, Access at, openPMD::json::TracingJSON jsonCfg) + : AbstractIOHandler{path, at}, m_impl{this, std::move(jsonCfg)} {} std::future JSONIOHandler::flush(internal::FlushParams const &) diff --git a/src/IO/JSON/JSONIOHandlerImpl.cpp b/src/IO/JSON/JSONIOHandlerImpl.cpp index 17253eca76..5d9a7e83dc 100644 --- a/src/IO/JSON/JSONIOHandlerImpl.cpp +++ b/src/IO/JSON/JSONIOHandlerImpl.cpp @@ -54,9 +54,62 @@ namespace openPMD throw std::runtime_error((TEXT)); \ } -JSONIOHandlerImpl::JSONIOHandlerImpl(AbstractIOHandler *handler) +JSONIOHandlerImpl::JSONIOHandlerImpl( + AbstractIOHandler *handler, openPMD::json::TracingJSON config) : AbstractIOHandlerImpl(handler) -{} +{ + if (config.json().contains("json")) + { + auto jsonConfig = config["json"]; + if (jsonConfig.json().contains("mode")) + { + auto modeOption = openPMD::json::asLowerCaseStringDynamic( + jsonConfig["mode"].json()); + if (!modeOption.has_value()) + { + throw error::BackendConfigSchema( + {"json", "mode"}, + "Invalid value of non-string type (accepted values are " + "'dataset' and 'template'."); + } + auto mode = modeOption.value(); + if (mode == "dataset") + { + m_mode = IOMode::Dataset; + } + else if (mode == "template") + { + m_mode = IOMode::Template; + } + else + { + throw error::BackendConfigSchema( + {"json", "mode"}, + "Invalid value: '" + mode + + "' (accepted values are 'dataset' and 'template'."); + } + } + auto shadow = jsonConfig.invertShadow(); + if (shadow.size() > 0) + { + switch (jsonConfig.originallySpecifiedAs) + { + case openPMD::json::SupportedLanguages::JSON: + std::cerr << "Warning: parts of the backend configuration for " + "JSON backend remain unused:\n" + << shadow << std::endl; + break; + case openPMD::json::SupportedLanguages::TOML: { + auto asToml = openPMD::json::jsonToToml(shadow); + std::cerr << "Warning: parts of the backend configuration for " + "JSON backend remain unused:\n" + << asToml << std::endl; + break; + } + } + } + } +} JSONIOHandlerImpl::~JSONIOHandlerImpl() {