Nix 2.26.3
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#include <list>
6
7#include "types.hh"
8
9namespace nix {
10
11enum struct ExperimentalFeature;
12
13const nlohmann::json * get(const nlohmann::json & map, const std::string & key);
14
15nlohmann::json * get(nlohmann::json & map, const std::string & key);
16
23const nlohmann::json & valueAt(
24 const nlohmann::json::object_t & map,
25 const std::string & key);
26
27std::optional<nlohmann::json> optionalValueAt(const nlohmann::json::object_t & value, const std::string & key);
28
33const nlohmann::json * getNullable(const nlohmann::json & value);
34const nlohmann::json::object_t & getObject(const nlohmann::json & value);
35const nlohmann::json::array_t & getArray(const nlohmann::json & value);
36const nlohmann::json::string_t & getString(const nlohmann::json & value);
37const nlohmann::json::number_integer_t & getInteger(const nlohmann::json & value);
38const nlohmann::json::boolean_t & getBoolean(const nlohmann::json & value);
39Strings getStringList(const nlohmann::json & value);
40StringMap getStringMap(const nlohmann::json & value);
41StringSet getStringSet(const nlohmann::json & value);
42
48template<typename T>
49struct json_avoids_null;
50
54template<typename T>
55struct json_avoids_null : std::bool_constant<std::is_integral<T>::value> {};
56
57template<>
58struct json_avoids_null<std::nullptr_t> : std::false_type {};
59
60template<>
61struct json_avoids_null<bool> : std::true_type {};
62
63template<>
64struct json_avoids_null<std::string> : std::true_type {};
65
66template<typename T>
67struct json_avoids_null<std::vector<T>> : std::true_type {};
68
69template<typename T>
70struct json_avoids_null<std::list<T>> : std::true_type {};
71
72template<typename K, typename V>
73struct json_avoids_null<std::map<K, V>> : std::true_type {};
74
78template<>
79struct json_avoids_null<ExperimentalFeature> : std::true_type {};
80
81}
82
83namespace nlohmann {
84
93template<typename T>
94struct adl_serializer<std::optional<T>>
95{
100 static void from_json(const json & json, std::optional<T> & t)
101 {
102 static_assert(
104 "null is already in use for underlying type's JSON");
105 t = json.is_null()
106 ? std::nullopt
107 : std::make_optional(json.template get<T>());
108 }
109
114 static void to_json(json & json, const std::optional<T> & t)
115 {
116 static_assert(
118 "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}
ExperimentalFeature
Definition experimental-features.hh:19
const T::key_type & key
Definition lexer.l:2763
T t
Definition lexer.l:154
const T & value
Definition lexer.l:492
Definition json-utils.hh:55
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:114
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:100