diff --git a/lix/libstore/outputs-spec.cc b/lix/libstore/outputs-spec.cc index 41b92a1ba..aadc1420d 100644 --- a/lix/libstore/outputs-spec.cc +++ b/lix/libstore/outputs-spec.cc @@ -148,11 +148,7 @@ bool OutputsSpec::isSubsetOf(const OutputsSpec & that) const }, that.raw); } -} - -namespace nix::json { - -OutputsSpec adl_serializer::from_json(const JSON & json) { +OutputsSpec OutputsSpec::from_json(const JSON & json) { auto names = json.get(); if (names == StringSet({"*"})) return OutputsSpec::All {}; @@ -160,7 +156,7 @@ OutputsSpec adl_serializer::from_json(const JSON & json) { return OutputsSpec::Names { std::move(names) }; } -void adl_serializer::to_json(JSON & json, OutputsSpec t) { +void OutputsSpec::to_json(JSON & json, const OutputsSpec & t) { std::visit(overloaded { [&](const OutputsSpec::All &) { json = std::vector({"*"}); @@ -172,7 +168,7 @@ void adl_serializer::to_json(JSON & json, OutputsSpec t) { } -ExtendedOutputsSpec adl_serializer::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::from_json(const JSON & } } -void adl_serializer::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::to_json(json, e); + OutputsSpec::to_json(json, e); }, }, t.raw); } diff --git a/lix/libstore/outputs-spec.hh b/lix/libstore/outputs-spec.hh index 2e2170c33..c889f7810 100644 --- a/lix/libstore/outputs-spec.hh +++ b/lix/libstore/outputs-spec.hh @@ -7,8 +7,8 @@ #include #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 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> 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) diff --git a/lix/libutil/json-impls.hh b/lix/libutil/json-impls.hh deleted file mode 100644 index d53b99087..000000000 --- a/lix/libutil/json-impls.hh +++ /dev/null @@ -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 { \ - static TYPE from_json(const JSON & json); \ - static void to_json(JSON & json, TYPE t); \ - }; \ - } diff --git a/lix/libutil/json.hh b/lix/libutil/json.hh index e2cd81574..9ecee6e20 100644 --- a/lix/libutil/json.hh +++ b/lix/libutil/json.hh @@ -6,6 +6,7 @@ /// deal with `std::optional` types until 3.11.3 at, and we need those. #include "lix/libutil/json-fwd.hh" // IWYU pragma: keep +#include #include // IWYU pragma: keep #include #include @@ -84,6 +85,25 @@ struct adl_serializer { detail::call_from_json(std::forward(j), value); } + + // Following https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types + template + 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 + requires requires(Json && j) { + { + T::from_json(std::forward(j)) + } -> std::same_as; + } + static auto from_json(Json && j) + { + return T::from_json(std::forward(j)); + } }; } diff --git a/lix/libutil/meson.build b/lix/libutil/meson.build index d35e65d7f..63ce56905 100644 --- a/lix/libutil/meson.build +++ b/lix/libutil/meson.build @@ -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',