Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/libutil/json-utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,29 @@ namespace nlohmann {
*/
template<typename T>
struct adl_serializer<std::optional<T>> {
static std::optional<T> from_json(const json & json) {
/**
* @brief Convert a JSON type to an `optional<T>` treating
* `null` as `std::nullopt`.
*/
static void from_json(const json & json, std::optional<T> & t) {
static_assert(
nix::json_avoids_null<T>::value,
"null is already in use for underlying type's JSON");
return json.is_null()
t = json.is_null()
? std::nullopt
: std::optional { adl_serializer<T>::from_json(json) };
: std::make_optional(json.template get<T>());
}
static void to_json(json & json, std::optional<T> t) {

/**
* @brief Convert an optional type to a JSON type treating `std::nullopt`
* as `null`.
*/
static void to_json(json & 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)
adl_serializer<T>::to_json(json, *t);
json = *t;
else
json = nullptr;
}
Expand Down
58 changes: 58 additions & 0 deletions src/libutil/tests/json-utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <vector>
#include <optional>

#include <gtest/gtest.h>

#include "json-utils.hh"

namespace nix {

/* Test `to_json` and `from_json` with `std::optional` types.
* We are specifically interested in whether we can _nest_ optionals in STL
* containers so we that we can leverage existing adl_serializer templates. */

TEST(to_json, optionalInt) {
std::optional<int> val = std::make_optional(420);
ASSERT_EQ(nlohmann::json(val), nlohmann::json(420));
val = std::nullopt;
ASSERT_EQ(nlohmann::json(val), nlohmann::json(nullptr));
}

TEST(to_json, vectorOfOptionalInts) {
std::vector<std::optional<int>> vals = {
std::make_optional(420),
std::nullopt,
};
ASSERT_EQ(nlohmann::json(vals), nlohmann::json::parse("[420,null]"));
}

TEST(to_json, optionalVectorOfInts) {
std::optional<std::vector<int>> val = std::make_optional(std::vector<int> {
-420,
420,
});
ASSERT_EQ(nlohmann::json(val), nlohmann::json::parse("[-420,420]"));
val = std::nullopt;
ASSERT_EQ(nlohmann::json(val), nlohmann::json(nullptr));
}

TEST(from_json, optionalInt) {
nlohmann::json json = 420;
std::optional<int> val = json;
ASSERT_TRUE(val.has_value());
ASSERT_EQ(*val, 420);
json = nullptr;
json.get_to(val);
ASSERT_FALSE(val.has_value());
}

TEST(from_json, vectorOfOptionalInts) {
nlohmann::json json = { 420, nullptr };
std::vector<std::optional<int>> vals = json;
ASSERT_EQ(vals.size(), 2);
ASSERT_TRUE(vals.at(0).has_value());
ASSERT_EQ(*vals.at(0), 420);
ASSERT_FALSE(vals.at(1).has_value());
}

} /* namespace nix */