Skip to content

Commit

Permalink
JSON backend: Fail when trying to open non-existing groups
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed May 30, 2023
1 parent 980d42c commit 399e6cd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl

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

// In order not to insert the same file name into the data structures
// with a new pointer (e.g. when reopening), search for a possibly
Expand Down
55 changes: 44 additions & 11 deletions src/IO/JSON/JSONIOHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "openPMD/Datatype.hpp"
#include "openPMD/DatatypeHelpers.hpp"
#include "openPMD/Error.hpp"
#include "openPMD/IO/Access.hpp"
#include "openPMD/ThrowError.hpp"
#include "openPMD/auxiliary/Filesystem.hpp"
#include "openPMD/auxiliary/Memory.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
Expand Down Expand Up @@ -165,13 +167,13 @@ void JSONIOHandlerImpl::createPath(
auto filepos = setAndGetFilePosition(writable, false);

jsonVal = &(*jsonVal)[filepos->id];
ensurePath(jsonVal, path);
ensurePath(jsonVal, path, m_handler->m_backendAccess);
path = filepos->id.to_string() + "/" + path;
}
else
{

ensurePath(jsonVal, path);
ensurePath(jsonVal, path, m_handler->m_backendAccess);
}

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

ensurePath(j, removeSlashes(parameters.path));
ensurePath(
j,
removeSlashes(parameters.path),
/* Must not modify j */ Access::READ_ONLY);

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

void JSONIOHandlerImpl::ensurePath(nlohmann::json *jsonp, std::string path)
void JSONIOHandlerImpl::ensurePath(
nlohmann::json *jsonp, std::string path, Access access)
{
auto groups = auxiliary::split(path, "/");
for (std::string &group : groups)
if (access::readOnly(access))
{
// Enforce a JSON object
// the library will automatically create a list if the first
// key added to it is parseable as an int
jsonp = &(*jsonp)[group];
if (jsonp->is_null())
for (std::string const &group : groups)
{
*jsonp = nlohmann::json::object();
if (!jsonp->contains(group))
{
throw error::ReadError(
error::AffectedObject::Group,
error::Reason::NotFound,
"JSON",
"Required group '" + path + "' not present.");
}
jsonp = &(*jsonp).at(group);
if (!jsonp->is_object())
{
throw error::ReadError(
error::AffectedObject::Group,
error::Reason::UnexpectedContent,
"JSON",
"Required group '" + path +
"' is present, but not a JSON object.");
}
}
}
else
{
for (std::string const &group : groups)
{
// Enforce a JSON object
// the library will automatically create a list if the first
// key added to it is parseable as an int
jsonp = &(*jsonp)[group];
if (jsonp->is_null())
{
*jsonp = nlohmann::json::object();
}
}
}
}
Expand Down

0 comments on commit 399e6cd

Please sign in to comment.