libutil: remove json-impls.hh

we can do this much better now that we own the serializers.

Change-Id: I531ed44a7b6feac42ffd76719002b78488317d1a
This commit is contained in:
eldritch horrors
2025-03-23 22:29:58 +00:00
parent 45017f7508
commit dd7cb01708
5 changed files with 32 additions and 28 deletions
+5 -9
View File
@@ -148,11 +148,7 @@ bool OutputsSpec::isSubsetOf(const OutputsSpec & that) const
}, that.raw);
}
}
namespace nix::json {
OutputsSpec adl_serializer<OutputsSpec>::from_json(const JSON & json) {
OutputsSpec OutputsSpec::from_json(const JSON & json) {
auto names = json.get<StringSet>();
if (names == StringSet({"*"}))
return OutputsSpec::All {};
@@ -160,7 +156,7 @@ OutputsSpec adl_serializer<OutputsSpec>::from_json(const JSON & json) {
return OutputsSpec::Names { std::move(names) };
}
void adl_serializer<OutputsSpec>::to_json(JSON & json, OutputsSpec t) {
void OutputsSpec::to_json(JSON & json, const OutputsSpec & t) {
std::visit(overloaded {
[&](const OutputsSpec::All &) {
json = std::vector<std::string>({"*"});
@@ -172,7 +168,7 @@ void adl_serializer<OutputsSpec>::to_json(JSON & json, OutputsSpec t) {
}
ExtendedOutputsSpec adl_serializer<ExtendedOutputsSpec>::from_json(const JSON & json) {
ExtendedOutputsSpec ExtendedOutputsSpec::from_json(const JSON & json) {
if (json.is_null())
return ExtendedOutputsSpec::Default {};
else {
@@ -180,13 +176,13 @@ ExtendedOutputsSpec adl_serializer<ExtendedOutputsSpec>::from_json(const JSON &
}
}
void adl_serializer<ExtendedOutputsSpec>::to_json(JSON & json, ExtendedOutputsSpec t) {
void ExtendedOutputsSpec::to_json(JSON & json, const ExtendedOutputsSpec & t) {
std::visit(overloaded {
[&](const ExtendedOutputsSpec::Default &) {
json = nullptr;
},
[&](const ExtendedOutputsSpec::Explicit & e) {
adl_serializer<OutputsSpec>::to_json(json, e);
OutputsSpec::to_json(json, e);
},
}, t.raw);
}
+7 -4
View File
@@ -7,8 +7,8 @@
#include <variant>
#include "lix/libutil/comparator.hh"
#include "lix/libutil/json-impls.hh"
#include "lix/libutil/comparator.hh"
#include "lix/libutil/json-fwd.hh"
#include "lix/libutil/variant-wrapper.hh"
namespace nix {
@@ -89,6 +89,9 @@ struct OutputsSpec {
static std::optional<OutputsSpec> parseOpt(std::string_view s);
std::string to_string() const;
static void to_json(JSON & json, const OutputsSpec & t);
static OutputsSpec from_json(const JSON & json);
};
struct ExtendedOutputsSpec {
@@ -116,9 +119,9 @@ struct ExtendedOutputsSpec {
static std::optional<std::pair<std::string_view, ExtendedOutputsSpec>> parseOpt(std::string_view s);
std::string to_string() const;
static void to_json(JSON & json, const ExtendedOutputsSpec & spec);
static ExtendedOutputsSpec from_json(const JSON & t);
};
}
JSON_IMPL(OutputsSpec)
JSON_IMPL(ExtendedOutputsSpec)
-14
View File
@@ -1,14 +0,0 @@
#pragma once
///@file
#include "lix/libutil/json-fwd.hh"
// Following https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types
#define JSON_IMPL(TYPE) \
namespace nix::json { \
template <> \
struct adl_serializer<TYPE> { \
static TYPE from_json(const JSON & json); \
static void to_json(JSON & json, TYPE t); \
}; \
}
+20
View File
@@ -6,6 +6,7 @@
/// deal with `std::optional<T>` types until 3.11.3 at, and we need those.
#include "lix/libutil/json-fwd.hh" // IWYU pragma: keep
#include <concepts>
#include <nlohmann/json.hpp> // IWYU pragma: keep
#include <list>
#include <type_traits>
@@ -84,6 +85,25 @@ struct adl_serializer<T, void>
{
detail::call_from_json(std::forward<Json>(j), value);
}
// Following https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types
template<typename Json>
requires requires(Json & j, T value) { T::to_json(j, value); }
static void to_json(Json & j, const T & value)
{
T::to_json(j, value);
}
template<typename Json>
requires requires(Json && j) {
{
T::from_json(std::forward<Json>(j))
} -> std::same_as<T>;
}
static auto from_json(Json && j)
{
return T::from_json(std::forward<Json>(j));
}
};
}
-1
View File
@@ -95,7 +95,6 @@ libutil_headers = files(
'input-accessor.hh',
'json.hh',
'json-fwd.hh',
'json-impls.hh',
'json-utils.hh',
'logging.hh',
'logging-json.hh',