2828#include " openPMD/auxiliary/StringManip.hpp"
2929#include " openPMD/backend/Writable.hpp"
3030
31+ #include < toml.hpp>
32+
3133#include < exception>
3234#include < iostream>
3335#include < optional>
@@ -917,7 +919,8 @@ void JSONIOHandlerImpl::readAttribute(
917919 " [JSON] Attributes have to be written before reading." )
918920 refreshFileFromParent (writable);
919921 auto name = removeSlashes (parameters.name );
920- auto &jsonLoc = obtainJsonContents (writable)[" attributes" ];
922+ auto const &jsonContents = obtainJsonContents (writable);
923+ auto const &jsonLoc = jsonContents[" attributes" ];
921924 setAndGetFilePosition (writable);
922925 std::string error_msg (" [JSON] No such attribute '" );
923926 error_msg.append (name)
@@ -985,7 +988,12 @@ void JSONIOHandlerImpl::listAttributes(
985988 " [JSON] Attributes have to be written before reading." )
986989 refreshFileFromParent (writable);
987990 auto filePosition = setAndGetFilePosition (writable);
988- auto &j = obtainJsonContents (writable)[" attributes" ];
991+ auto const &jsonContents = obtainJsonContents (writable);
992+ if (!jsonContents.contains (" attributes" ))
993+ {
994+ return ;
995+ }
996+ auto const &j = jsonContents[" attributes" ];
989997 for (auto it = j.begin (); it != j.end (); it++)
990998 {
991999 parameters.attributes ->push_back (it.key ());
@@ -1111,6 +1119,7 @@ Extent JSONIOHandlerImpl::getMultiplicators(Extent const &extent)
11111119 return res;
11121120}
11131121
1122+ // @todo treatment of null value
11141123nlohmann::json JSONIOHandlerImpl::initializeNDArray (Extent const &extent)
11151124{
11161125 // idea: begin from the innermost shale and copy the result into the
@@ -1178,7 +1187,7 @@ std::string JSONIOHandlerImpl::removeSlashes(std::string s)
11781187}
11791188
11801189template <typename KeyT>
1181- bool JSONIOHandlerImpl::hasKey (nlohmann::json &j, KeyT &&key)
1190+ bool JSONIOHandlerImpl::hasKey (nlohmann::json const &j, KeyT &&key)
11821191{
11831192 return j.find (std::forward<KeyT>(key)) != j.end ();
11841193}
@@ -1240,7 +1249,15 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
12401249 // read from file
12411250 auto fh = getFilehandle (file, Access::READ_ONLY);
12421251 std::shared_ptr<nlohmann::json> res = std::make_shared<nlohmann::json>();
1243- *fh >> *res;
1252+ switch (m_fileFormat)
1253+ {
1254+ case FileFormat::Json:
1255+ *fh >> *res;
1256+ break ;
1257+ case FileFormat::Toml:
1258+ *res = openPMD::json::tomlToJson (toml::parse (*fh, *file));
1259+ break ;
1260+ }
12441261 VERIFY (fh->good (), " [JSON] Failed reading from a file." );
12451262 m_jsonVals.emplace (file, res);
12461263 return res;
@@ -1266,7 +1283,15 @@ void JSONIOHandlerImpl::putJsonContents(
12661283 {
12671284 auto fh = getFilehandle (filename, Access::CREATE);
12681285 (*it->second )[" platform_byte_widths" ] = platformSpecifics ();
1269- *fh << *it->second << std::endl;
1286+ switch (m_fileFormat)
1287+ {
1288+ case FileFormat::Json:
1289+ *fh << *it->second << std::endl;
1290+ break ;
1291+ case FileFormat::Toml:
1292+ *fh << openPMD::json::jsonToToml (*it->second ) << std::endl;
1293+ break ;
1294+ }
12701295 VERIFY (fh->good (), " [JSON] Failed writing data to disk." )
12711296 m_jsonVals.erase (it);
12721297 if (unsetDirty)
@@ -1472,7 +1497,7 @@ void JSONIOHandlerImpl::AttributeWriter::call(
14721497
14731498template <typename T>
14741499void JSONIOHandlerImpl::AttributeReader::call (
1475- nlohmann::json &json, Parameter<Operation::READ_ATT> ¶meters)
1500+ nlohmann::json const &json, Parameter<Operation::READ_ATT> ¶meters)
14761501{
14771502 JsonToCpp<T> jtc;
14781503 *parameters.resource = jtc (json);
0 commit comments