From c2427063199574b44e07a032fc44aa7e3e710559 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 12 Sep 2025 08:19:37 -0400 Subject: [PATCH] Move `json_avoids_null` to its own header This is because we need it in declarations where we should not be including the full `nlohmann/json.hpp`. Already can clean up by moving the experimental feature "instance". Also, make the `std::map` instance better by allowing for other comparison functions. --- .../include/nix/util/experimental-features.hh | 8 +++ src/libutil/include/nix/util/json-non-null.hh | 55 +++++++++++++++++++ src/libutil/include/nix/util/json-utils.hh | 51 +---------------- src/libutil/include/nix/util/meson.build | 1 + 4 files changed, 65 insertions(+), 50 deletions(-) create mode 100644 src/libutil/include/nix/util/json-non-null.hh diff --git a/src/libutil/include/nix/util/experimental-features.hh b/src/libutil/include/nix/util/experimental-features.hh index 1eabc3461..0a8f15863 100644 --- a/src/libutil/include/nix/util/experimental-features.hh +++ b/src/libutil/include/nix/util/experimental-features.hh @@ -3,6 +3,7 @@ #include "nix/util/error.hh" #include "nix/util/types.hh" +#include "nix/util/json-non-null.hh" #include @@ -89,6 +90,13 @@ public: MissingExperimentalFeature(ExperimentalFeature missingFeature); }; +/** + * `ExperimentalFeature` is always rendered as a string. + */ +template<> +struct json_avoids_null : std::true_type +{}; + /** * Semi-magic conversion to and from json. * See the nlohmann/json readme for more details. diff --git a/src/libutil/include/nix/util/json-non-null.hh b/src/libutil/include/nix/util/json-non-null.hh new file mode 100644 index 000000000..6bacce58f --- /dev/null +++ b/src/libutil/include/nix/util/json-non-null.hh @@ -0,0 +1,55 @@ +#pragma once +///@file + +#include +#include +#include +#include +#include + +namespace nix { + +/** + * For `adl_serializer>` below, we need to track what + * types are not already using `null`. Only for them can we use `null` + * to represent `std::nullopt`. + */ +template +struct json_avoids_null; + +/** + * Handle numbers in default impl + */ +template +struct json_avoids_null : std::bool_constant::value> +{}; + +template<> +struct json_avoids_null : std::false_type +{}; + +template<> +struct json_avoids_null : std::true_type +{}; + +template<> +struct json_avoids_null : std::true_type +{}; + +template +struct json_avoids_null> : std::true_type +{}; + +template +struct json_avoids_null> : std::true_type +{}; + +template +struct json_avoids_null> : std::true_type +{}; + +template +struct json_avoids_null> : std::true_type +{}; + +} // namespace nix diff --git a/src/libutil/include/nix/util/json-utils.hh b/src/libutil/include/nix/util/json-utils.hh index 20c50f957..4b5fb4b21 100644 --- a/src/libutil/include/nix/util/json-utils.hh +++ b/src/libutil/include/nix/util/json-utils.hh @@ -6,6 +6,7 @@ #include "nix/util/error.hh" #include "nix/util/types.hh" +#include "nix/util/json-non-null.hh" namespace nix { @@ -59,56 +60,6 @@ Strings getStringList(const nlohmann::json & value); StringMap getStringMap(const nlohmann::json & value); StringSet getStringSet(const nlohmann::json & value); -/** - * For `adl_serializer>` below, we need to track what - * types are not already using `null`. Only for them can we use `null` - * to represent `std::nullopt`. - */ -template -struct json_avoids_null; - -/** - * Handle numbers in default impl - */ -template -struct json_avoids_null : std::bool_constant::value> -{}; - -template<> -struct json_avoids_null : std::false_type -{}; - -template<> -struct json_avoids_null : std::true_type -{}; - -template<> -struct json_avoids_null : std::true_type -{}; - -template -struct json_avoids_null> : std::true_type -{}; - -template -struct json_avoids_null> : std::true_type -{}; - -template -struct json_avoids_null> : std::true_type -{}; - -template -struct json_avoids_null> : std::true_type -{}; - -/** - * `ExperimentalFeature` is always rendered as a string. - */ -template<> -struct json_avoids_null : std::true_type -{}; - } // namespace nix namespace nlohmann { diff --git a/src/libutil/include/nix/util/meson.build b/src/libutil/include/nix/util/meson.build index bdf114259..07a4f1d11 100644 --- a/src/libutil/include/nix/util/meson.build +++ b/src/libutil/include/nix/util/meson.build @@ -42,6 +42,7 @@ headers = files( 'hash.hh', 'hilite.hh', 'json-impls.hh', + 'json-non-null.hh', 'json-utils.hh', 'logging.hh', 'lru-cache.hh',