28
28
#include " openPMD/auxiliary/StringManip.hpp"
29
29
#include " openPMD/backend/Writable.hpp"
30
30
31
+ #include < toml.hpp>
32
+
31
33
#include < exception>
32
34
#include < iostream>
33
35
#include < optional>
@@ -917,7 +919,8 @@ void JSONIOHandlerImpl::readAttribute(
917
919
" [JSON] Attributes have to be written before reading." )
918
920
refreshFileFromParent (writable);
919
921
auto name = removeSlashes (parameters.name );
920
- auto &jsonLoc = obtainJsonContents (writable)[" attributes" ];
922
+ auto const &jsonContents = obtainJsonContents (writable);
923
+ auto const &jsonLoc = jsonContents[" attributes" ];
921
924
setAndGetFilePosition (writable);
922
925
std::string error_msg (" [JSON] No such attribute '" );
923
926
error_msg.append (name)
@@ -985,7 +988,12 @@ void JSONIOHandlerImpl::listAttributes(
985
988
" [JSON] Attributes have to be written before reading." )
986
989
refreshFileFromParent (writable);
987
990
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" ];
989
997
for (auto it = j.begin (); it != j.end (); it++)
990
998
{
991
999
parameters.attributes ->push_back (it.key ());
@@ -1111,6 +1119,7 @@ Extent JSONIOHandlerImpl::getMultiplicators(Extent const &extent)
1111
1119
return res;
1112
1120
}
1113
1121
1122
+ // @todo treatment of null value
1114
1123
nlohmann::json JSONIOHandlerImpl::initializeNDArray (Extent const &extent)
1115
1124
{
1116
1125
// idea: begin from the innermost shale and copy the result into the
@@ -1178,7 +1187,7 @@ std::string JSONIOHandlerImpl::removeSlashes(std::string s)
1178
1187
}
1179
1188
1180
1189
template <typename KeyT>
1181
- bool JSONIOHandlerImpl::hasKey (nlohmann::json &j, KeyT &&key)
1190
+ bool JSONIOHandlerImpl::hasKey (nlohmann::json const &j, KeyT &&key)
1182
1191
{
1183
1192
return j.find (std::forward<KeyT>(key)) != j.end ();
1184
1193
}
@@ -1240,7 +1249,15 @@ std::shared_ptr<nlohmann::json> JSONIOHandlerImpl::obtainJsonContents(File file)
1240
1249
// read from file
1241
1250
auto fh = getFilehandle (file, Access::READ_ONLY);
1242
1251
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
+ }
1244
1261
VERIFY (fh->good (), " [JSON] Failed reading from a file." );
1245
1262
m_jsonVals.emplace (file, res);
1246
1263
return res;
@@ -1266,7 +1283,15 @@ void JSONIOHandlerImpl::putJsonContents(
1266
1283
{
1267
1284
auto fh = getFilehandle (filename, Access::CREATE);
1268
1285
(*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
+ }
1270
1295
VERIFY (fh->good (), " [JSON] Failed writing data to disk." )
1271
1296
m_jsonVals.erase (it);
1272
1297
if (unsetDirty)
@@ -1472,7 +1497,7 @@ void JSONIOHandlerImpl::AttributeWriter::call(
1472
1497
1473
1498
template <typename T>
1474
1499
void JSONIOHandlerImpl::AttributeReader::call (
1475
- nlohmann::json &json, Parameter<Operation::READ_ATT> ¶meters)
1500
+ nlohmann::json const &json, Parameter<Operation::READ_ATT> ¶meters)
1476
1501
{
1477
1502
JsonToCpp<T> jtc;
1478
1503
*parameters.resource = jtc (json);
0 commit comments