2023-06-16 19:19:14 +00:00
|
|
|
#include "json-utils.hh"
|
2023-07-28 20:23:56 +00:00
|
|
|
#include "error.hh"
|
2023-06-16 19:19:14 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
const nlohmann::json * get(const nlohmann::json & map, const std::string & key)
|
|
|
|
{
|
|
|
|
auto i = map.find(key);
|
|
|
|
if (i == map.end()) return nullptr;
|
|
|
|
return &*i;
|
|
|
|
}
|
|
|
|
|
|
|
|
nlohmann::json * get(nlohmann::json & map, const std::string & key)
|
|
|
|
{
|
|
|
|
auto i = map.find(key);
|
|
|
|
if (i == map.end()) return nullptr;
|
|
|
|
return &*i;
|
|
|
|
}
|
|
|
|
|
2023-07-28 20:23:56 +00:00
|
|
|
const nlohmann::json & valueAt(
|
|
|
|
const nlohmann::json & map,
|
|
|
|
const std::string & key)
|
|
|
|
{
|
|
|
|
if (!map.contains(key))
|
|
|
|
throw Error("Expected JSON object to contain key '%s' but it doesn't", key);
|
|
|
|
|
|
|
|
return map[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
const nlohmann::json & ensureType(
|
|
|
|
const nlohmann::json & value,
|
|
|
|
nlohmann::json::value_type expectedType
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (value.type() != expectedType)
|
|
|
|
throw Error(
|
|
|
|
"Expected JSON value to be of type '%s' but it is of type '%s'",
|
|
|
|
nlohmann::json(expectedType).type_name(),
|
|
|
|
value.type_name());
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2023-06-16 19:19:14 +00:00
|
|
|
}
|