libutil: remove json-utils.hh

we can merge it into json.hh instead.

Change-Id: Ic40c25fa759bf52bb69eae5d7c0597f260fd94a6
This commit is contained in:
eldritch horrors
2025-03-23 22:29:58 +00:00
parent dd7cb01708
commit 56df5ba164
11 changed files with 34 additions and 51 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
#include "lix/libstore/daemon.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/topo-sort.hh"
#include "lix/libutil/json-utils.hh"
#include "lix/libutil/json.hh"
#include "lix/libutil/cgroup.hh"
#include "lix/libstore/build/personality.hh"
#include "lix/libutil/namespaces.hh"
-1
View File
@@ -7,7 +7,6 @@
#include "lix/libutil/types.hh"
#include "lix/libstore/common-protocol.hh"
#include "lix/libstore/common-protocol-impl.hh"
#include "lix/libutil/json-utils.hh"
#include "lix/libutil/strings.hh"
#include "lix/libutil/backed-string-view.hh"
-1
View File
@@ -2,7 +2,6 @@
///@file
#include "lix/libutil/config.hh"
#include "lix/libutil/json-utils.hh"
// Required for instances of to_json and from_json for ExperimentalFeature
#include "lix/libutil/experimental-features-json.hh"
#include "lix/libutil/json.hh"
+1 -1
View File
@@ -2,7 +2,7 @@
#include "lix/libutil/args/root.hh"
#include "lix/libutil/hash.hh"
#include "lix/libutil/strings.hh"
#include "lix/libutil/json-utils.hh" // IWYU pragma: keep (instances)
#include "lix/libutil/json.hh" // IWYU pragma: keep (instances)
#include "lix/libutil/environment-variables.hh"
#include "lix/libutil/experimental-features-json.hh" // IWYU pragma: keep (instances)
+1 -1
View File
@@ -2,7 +2,7 @@
///@file
#include "lix/libutil/deprecated-features.hh"
#include "lix/libutil/json-utils.hh"
#include "lix/libutil/json-fwd.hh"
namespace nix {
+1 -1
View File
@@ -2,7 +2,7 @@
///@file
#include "lix/libutil/experimental-features.hh"
#include "lix/libutil/json-utils.hh"
#include "lix/libutil/json-fwd.hh"
namespace nix {
+1 -1
View File
@@ -1,4 +1,4 @@
#include "lix/libutil/json-utils.hh"
#include "lix/libutil/json.hh"
#include "lix/libutil/error.hh"
namespace nix {
-42
View File
@@ -1,42 +0,0 @@
#pragma once
///@file
#include "lix/libutil/json.hh"
/**
* This "instance" is widely requested, see
* https://github.com/nlohmann/json/issues/1749, but momentum has stalled
* out. Writing there here in Nix as a stop-gap.
*
* We need to make sure the underlying type does not use `null` for this to
* round trip. We do that with a static assert.
*/
template<typename T>
struct nix::json::adl_serializer<std::optional<T>> {
/**
* @brief Convert a JSON type to an `optional<T>` treating
* `null` as `std::nullopt`.
*/
static void from_json(const auto & json, std::optional<T> & t) {
static_assert(
nix::json::avoids_null<T>::value,
"null is already in use for underlying type's JSON");
t = json.is_null()
? std::nullopt
: std::make_optional(json.template get<T>());
}
/**
* @brief Convert an optional type to a JSON type treating `std::nullopt`
* as `null`.
*/
static void to_json(auto & json, const std::optional<T> & t) {
static_assert(
nix::json::avoids_null<T>::value,
"null is already in use for underlying type's JSON");
if (t)
json = *t;
else
json = nullptr;
}
};
+28
View File
@@ -105,6 +105,34 @@ struct adl_serializer<T, void>
return T::from_json(std::forward<Json>(j));
}
};
template<typename T>
struct adl_serializer<std::optional<T>>
{
/**
* @brief Convert a JSON type to an `optional<T>` treating
* `null` as `std::nullopt`.
*/
static void from_json(const auto & json, std::optional<T> & t)
{
static_assert(avoids_null<T>::value, "null is already in use for underlying type's JSON");
t = json.is_null() ? std::nullopt : std::make_optional(json.template get<T>());
}
/**
* @brief Convert an optional type to a JSON type treating `std::nullopt`
* as `null`.
*/
static void to_json(auto & json, const std::optional<T> & t)
{
static_assert(avoids_null<T>::value, "null is already in use for underlying type's JSON");
if (t) {
json = *t;
} else {
json = nullptr;
}
}
};
}
}
-1
View File
@@ -95,7 +95,6 @@ libutil_headers = files(
'input-accessor.hh',
'json.hh',
'json-fwd.hh',
'json-utils.hh',
'logging.hh',
'logging-json.hh',
'lru-cache.hh',
+1 -1
View File
@@ -3,7 +3,7 @@
#include <gtest/gtest.h>
#include "lix/libutil/json-utils.hh"
#include "lix/libutil/json.hh"
namespace nix {