Skip to content

Commit 399e6cd

Browse files
committed
JSON backend: Fail when trying to open non-existing groups
1 parent 980d42c commit 399e6cd

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
284284

285285
// make sure that the given path exists in proper form in
286286
// the passed json value
287-
static void ensurePath(nlohmann::json *json, std::string path);
287+
static void ensurePath(nlohmann::json *json, std::string path, Access);
288288

289289
// In order not to insert the same file name into the data structures
290290
// with a new pointer (e.g. when reopening), search for a possibly

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "openPMD/Datatype.hpp"
2424
#include "openPMD/DatatypeHelpers.hpp"
2525
#include "openPMD/Error.hpp"
26+
#include "openPMD/IO/Access.hpp"
27+
#include "openPMD/ThrowError.hpp"
2628
#include "openPMD/auxiliary/Filesystem.hpp"
2729
#include "openPMD/auxiliary/Memory.hpp"
2830
#include "openPMD/auxiliary/StringManip.hpp"
@@ -165,13 +167,13 @@ void JSONIOHandlerImpl::createPath(
165167
auto filepos = setAndGetFilePosition(writable, false);
166168

167169
jsonVal = &(*jsonVal)[filepos->id];
168-
ensurePath(jsonVal, path);
170+
ensurePath(jsonVal, path, m_handler->m_backendAccess);
169171
path = filepos->id.to_string() + "/" + path;
170172
}
171173
else
172174
{
173175

174-
ensurePath(jsonVal, path);
176+
ensurePath(jsonVal, path, m_handler->m_backendAccess);
175177
}
176178

177179
m_dirty.emplace(file);
@@ -570,7 +572,10 @@ void JSONIOHandlerImpl::openPath(
570572
std::make_shared<JSONFilePosition>(json::json_pointer(path));
571573
}
572574

573-
ensurePath(j, removeSlashes(parameters.path));
575+
ensurePath(
576+
j,
577+
removeSlashes(parameters.path),
578+
/* Must not modify j */ Access::READ_ONLY);
574579

575580
writable->written = true;
576581
}
@@ -1110,18 +1115,46 @@ bool JSONIOHandlerImpl::hasKey(nlohmann::json &j, KeyT &&key)
11101115
return j.find(std::forward<KeyT>(key)) != j.end();
11111116
}
11121117

1113-
void JSONIOHandlerImpl::ensurePath(nlohmann::json *jsonp, std::string path)
1118+
void JSONIOHandlerImpl::ensurePath(
1119+
nlohmann::json *jsonp, std::string path, Access access)
11141120
{
11151121
auto groups = auxiliary::split(path, "/");
1116-
for (std::string &group : groups)
1122+
if (access::readOnly(access))
11171123
{
1118-
// Enforce a JSON object
1119-
// the library will automatically create a list if the first
1120-
// key added to it is parseable as an int
1121-
jsonp = &(*jsonp)[group];
1122-
if (jsonp->is_null())
1124+
for (std::string const &group : groups)
11231125
{
1124-
*jsonp = nlohmann::json::object();
1126+
if (!jsonp->contains(group))
1127+
{
1128+
throw error::ReadError(
1129+
error::AffectedObject::Group,
1130+
error::Reason::NotFound,
1131+
"JSON",
1132+
"Required group '" + path + "' not present.");
1133+
}
1134+
jsonp = &(*jsonp).at(group);
1135+
if (!jsonp->is_object())
1136+
{
1137+
throw error::ReadError(
1138+
error::AffectedObject::Group,
1139+
error::Reason::UnexpectedContent,
1140+
"JSON",
1141+
"Required group '" + path +
1142+
"' is present, but not a JSON object.");
1143+
}
1144+
}
1145+
}
1146+
else
1147+
{
1148+
for (std::string const &group : groups)
1149+
{
1150+
// Enforce a JSON object
1151+
// the library will automatically create a list if the first
1152+
// key added to it is parseable as an int
1153+
jsonp = &(*jsonp)[group];
1154+
if (jsonp->is_null())
1155+
{
1156+
*jsonp = nlohmann::json::object();
1157+
}
11251158
}
11261159
}
11271160
}

0 commit comments

Comments
 (0)