12#include "lix/libutil/json-fwd.hh"
14#include <nlohmann/json.hpp>
27const JSON & ensureType(
29 JSON::value_type expectedType);
38 : std::bool_constant<std::is_integral_v<T> || std::is_floating_point_v<T> || IntegralEnum<T>>
56template<
typename K,
typename V>
60template<
typename Json,
typename T>
61 requires requires(Json & j, T value) { to_json(j, value); }
62void call_to_json(Json & j,
const T & value)
67template<
typename Json,
typename T>
68 requires requires(Json && j, T & value) { from_json(std::forward<Json>(j), value); }
69void call_from_json(Json && j, T & value)
71 from_json(std::forward<Json>(j), value);
78 template<
typename Json>
79 requires requires(Json & j, T value) { detail::call_to_json(j, value); }
80 static void to_json(Json & j,
const T & value)
82 detail::call_to_json(j, value);
85 template<
typename Json>
86 requires requires(Json && j, T & value) {
87 detail::call_from_json(std::forward<Json>(j), value);
89 static void from_json(Json && j, T & value)
91 detail::call_from_json(std::forward<Json>(j), value);
95 template<
typename Json>
96 requires requires(Json & j, T value) { T::to_json(j, value); }
97 static void to_json(Json & j,
const T & value)
102 template<
typename Json>
103 requires requires(Json && j) {
105 T::from_json(std::forward<Json>(j))
106 } -> std::same_as<T>;
108 static auto from_json(Json && j)
110 return T::from_json(std::forward<Json>(j));
113 template<
typename Json>
115 static void to_json(Json && json,
const T & value)
117 json =
static_cast<std::underlying_type_t<T>
>(value);
120 template<
typename Json>
122 static void from_json(
const Json & json, T & value)
124 value =
static_cast<T
>(json.template get<std::underlying_type_t<T>>());
135 static void from_json(
const auto & json, std::optional<T> & t)
138 t = json.is_null() ? std::nullopt : std::make_optional(json.template get<T>());
145 static void to_json(
auto & json,
const std::optional<T> & t)
156MakeError(ParseError, Error);
162template<
typename Source>
163JSON parse(
Source && source, std::optional<std::string_view> context = {})
167 return JSON::parse(std::forward<Source>(source));
168 }
catch (JSON::exception & e) {
169 ParseError error{
"failed to parse JSON: %s", e.what()};
171 error.addTrace(
nullptr,
fmt(
"while parsing %s", *context));
Definition json-fwd.hh:24
This file defines two main structs/classes used in nix error handling.
std::string fmt(const std::string &s)
Definition fmt.hh:127
Definition serialise.hh:66
static void to_json(auto &json, const std::optional< T > &t)
Convert an optional type to a JSON type treating std::nullopt as null.
Definition json.hh:145
static void from_json(const auto &json, std::optional< T > &t)
Convert a JSON type to an optional<T> treating null as std::nullopt.
Definition json.hh:135
Definition json-fwd.hh:27