4#include <nlohmann/json.hpp>
20const nlohmann::json &
valueAt(
const nlohmann::json::object_t & map, std::string_view key);
26const nlohmann::json *
optionalValueAt(
const nlohmann::json::object_t & value, std::string_view key);
31const nlohmann::json &
valueAt(nlohmann::json::object_t && map, std::string_view key) =
delete;
32const nlohmann::json *
optionalValueAt(nlohmann::json::object_t && value, std::string_view key) =
delete;
38const nlohmann::json *
getNullable(
const nlohmann::json & value);
39const nlohmann::json::object_t & getObject(
const nlohmann::json & value);
40const nlohmann::json::array_t & getArray(
const nlohmann::json & value);
41const nlohmann::json::string_t & getString(
const nlohmann::json & value);
42const nlohmann::json::number_unsigned_t & getUnsigned(
const nlohmann::json & value);
45auto getInteger(
const nlohmann::json & value) -> std::enable_if_t<std::is_signed_v<T> && std::is_integral_v<T>, T>
47 if (
auto ptr = value.get_ptr<
const nlohmann::json::number_unsigned_t *>()) {
48 if (*ptr <= std::make_unsigned_t<T>(std::numeric_limits<T>::max())) {
51 }
else if (
auto ptr = value.get_ptr<
const nlohmann::json::number_integer_t *>()) {
52 if (*ptr >= std::numeric_limits<T>::min() && *ptr <= std::numeric_limits<T>::max()) {
56 auto typeName = value.is_number_float() ?
"floating point number" : value.type_name();
57 throw Error(
"Expected JSON value to be an integral number but it is of type '%s': %s", typeName, value.dump());
59 throw Error(
"Out of range: JSON value '%s' cannot be casted to %d-bit integer", value.dump(), 8 *
sizeof(T));
62template<
typename...
Args>
63std::map<std::string,
Args...> getMap(
const nlohmann::json::object_t & jsonObject,
auto && f)
65 std::map<std::string,
Args...> map;
67 for (
const auto & [key, value] : jsonObject)
68 map.insert_or_assign(key, f(value));
73const nlohmann::json::boolean_t & getBoolean(
const nlohmann::json & value);
74Strings getStringList(
const nlohmann::json & value);
75StringMap getStringMap(
const nlohmann::json & value);
76StringSet getStringSet(
const nlohmann::json & value);
79static inline std::optional<T> ptrToOwned(
const nlohmann::json * ptr)
82 return std::optional{*ptr};
100struct adl_serializer<std::optional<T>>
106 static void from_json(
const json & json, std::optional<T> & t)
108 static_assert(nix::json_avoids_null<T>::value,
"null is already in use for underlying type's JSON");
109 t = json.is_null() ? std::nullopt : std::make_optional(json.template get<T>());
116 static void to_json(json & json,
const std::optional<T> & t)
118 static_assert(nix::json_avoids_null<T>::value,
"null is already in use for underlying type's JSON");
This file defines two main structs/classes used in nix error handling.
ExperimentalFeature
Definition experimental-features.hh:19
const nlohmann::json * optionalValueAt(const nlohmann::json::object_t &value, std::string_view key)
Definition json-utils.cc:16
const nlohmann::json * getNullable(const nlohmann::json &value)
Definition json-utils.cc:21
const nlohmann::json & valueAt(const nlohmann::json::object_t &map, std::string_view key)
Definition json-utils.cc:8
static void to_json(json &json, const std::optional< T > &t)
Convert an optional type to a JSON type treating std::nullopt as null.
Definition json-utils.hh:116
static void from_json(const json &json, std::optional< T > &t)
Convert a JSON type to an optional<T> treating null as std::nullopt.
Definition json-utils.hh:106
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26
std::set< std::string, std::less<> > StringSet
Definition types.hh:45