1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 08:31:16 +02:00

fix: nlohmann::adl_serializer for std::optional (#9147)

This allows templates such as `NLOHMANN_DEFINE_TYPE_*` templates and other generators with things like `std::vector<std::optional<T>>`.

Co-authored-by: John Ericson <John.Ericson@Obsidian.Systems>
This commit is contained in:
Alex Ameen 2023-11-29 19:26:39 -06:00 committed by GitHub
parent a8fea5a54f
commit 02bd821f2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 5 deletions

View file

@ -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 */