Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
json-utils.hh
Go to the documentation of this file.
1#pragma once
3
4#include <nlohmann/json.hpp>
5
6#include "nix/util/error.hh"
7#include "nix/util/types.hh"
9
10namespace nix {
11
12enum struct ExperimentalFeature;
13
20const nlohmann::json & valueAt(const nlohmann::json::object_t & map, std::string_view key);
21
26const nlohmann::json * optionalValueAt(const nlohmann::json::object_t & value, std::string_view key);
27
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;
33
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);
43
44template<typename T>
45auto getInteger(const nlohmann::json & value) -> std::enable_if_t<std::is_signed_v<T> && std::is_integral_v<T>, T>
46{
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())) {
49 return *ptr;
50 }
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()) {
53 return *ptr;
54 }
55 } else {
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());
58 }
59 throw Error("Out of range: JSON value '%s' cannot be casted to %d-bit integer", value.dump(), 8 * sizeof(T));
60}
61
62template<typename... Args>
63std::map<std::string, Args...> getMap(const nlohmann::json::object_t & jsonObject, auto && f)
64{
65 std::map<std::string, Args...> map;
66
67 for (const auto & [key, value] : jsonObject)
68 map.insert_or_assign(key, f(value));
69
70 return map;
71}
72
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);
77
78template<typename T>
79static inline std::optional<T> ptrToOwned(const nlohmann::json * ptr)
80{
81 if (ptr)
82 return std::optional{*ptr};
83 else
84 return std::nullopt;
85}
86
87} // namespace nix
88
89namespace nlohmann {
90
99template<typename T>
100struct adl_serializer<std::optional<T>>
101{
106 static void from_json(const json & json, std::optional<T> & t)
107 {
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>());
110 }
111
116 static void to_json(json & json, const std::optional<T> & t)
117 {
118 static_assert(nix::json_avoids_null<T>::value, "null is already in use for underlying type's JSON");
119 if (t)
120 json = *t;
121 else
122 json = nullptr;
123 }
124};
125
126} // namespace nlohmann
Definition args.hh:29
Definition error.hh:310
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