1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 20:16:03 +01:00

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.
This commit is contained in:
John Ericson 2025-09-12 08:19:37 -04:00
parent aef431fbd1
commit c242706319
4 changed files with 65 additions and 50 deletions

View file

@ -3,6 +3,7 @@
#include "nix/util/error.hh"
#include "nix/util/types.hh"
#include "nix/util/json-non-null.hh"
#include <nlohmann/json_fwd.hpp>
@ -89,6 +90,13 @@ public:
MissingExperimentalFeature(ExperimentalFeature missingFeature);
};
/**
* `ExperimentalFeature` is always rendered as a string.
*/
template<>
struct json_avoids_null<ExperimentalFeature> : std::true_type
{};
/**
* Semi-magic conversion to and from json.
* See the nlohmann/json readme for more details.

View file

@ -0,0 +1,55 @@
#pragma once
///@file
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
namespace nix {
/**
* For `adl_serializer<std::optional<T>>` below, we need to track what
* types are not already using `null`. Only for them can we use `null`
* to represent `std::nullopt`.
*/
template<typename T>
struct json_avoids_null;
/**
* Handle numbers in default impl
*/
template<typename T>
struct json_avoids_null : std::bool_constant<std::is_integral<T>::value>
{};
template<>
struct json_avoids_null<std::nullptr_t> : std::false_type
{};
template<>
struct json_avoids_null<bool> : std::true_type
{};
template<>
struct json_avoids_null<std::string> : std::true_type
{};
template<typename T>
struct json_avoids_null<std::vector<T>> : std::true_type
{};
template<typename T>
struct json_avoids_null<std::list<T>> : std::true_type
{};
template<typename T, typename Compare>
struct json_avoids_null<std::set<T, Compare>> : std::true_type
{};
template<typename K, typename V, typename Compare>
struct json_avoids_null<std::map<K, V, Compare>> : std::true_type
{};
} // namespace nix

View file

@ -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<std::optional<T>>` below, we need to track what
* types are not already using `null`. Only for them can we use `null`
* to represent `std::nullopt`.
*/
template<typename T>
struct json_avoids_null;
/**
* Handle numbers in default impl
*/
template<typename T>
struct json_avoids_null : std::bool_constant<std::is_integral<T>::value>
{};
template<>
struct json_avoids_null<std::nullptr_t> : std::false_type
{};
template<>
struct json_avoids_null<bool> : std::true_type
{};
template<>
struct json_avoids_null<std::string> : std::true_type
{};
template<typename T>
struct json_avoids_null<std::vector<T>> : std::true_type
{};
template<typename T>
struct json_avoids_null<std::list<T>> : std::true_type
{};
template<typename T, typename Compare>
struct json_avoids_null<std::set<T, Compare>> : std::true_type
{};
template<typename K, typename V>
struct json_avoids_null<std::map<K, V>> : std::true_type
{};
/**
* `ExperimentalFeature` is always rendered as a string.
*/
template<>
struct json_avoids_null<ExperimentalFeature> : std::true_type
{};
} // namespace nix
namespace nlohmann {

View file

@ -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',