1
0
mirror of https://github.com/nlohmann/json synced 2024-11-25 13:20:05 +00:00
nlohmannjson/docs/examples/nlohmann_json_namespace_begin.c++17.cpp
Niels Lohmann 7b6cf5918b
Documentation change (#3672)
Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>
2022-08-05 19:51:39 +02:00

34 lines
687 B
C++

#include <iostream>
#include <optional>
#include <nlohmann/json.hpp>
// partial specialization (see https://json.nlohmann.me/features/arbitrary_types/)
NLOHMANN_JSON_NAMESPACE_BEGIN
template <typename T>
struct adl_serializer<std::optional<T>>
{
static void to_json(json& j, const std::optional<T>& opt)
{
if (opt == std::nullopt)
{
j = nullptr;
}
else
{
j = *opt;
}
}
};
NLOHMANN_JSON_NAMESPACE_END
int main()
{
std::optional<int> o1 = 1;
std::optional<int> o2 = std::nullopt;
NLOHMANN_JSON_NAMESPACE::json j;
j.push_back(o1);
j.push_back(o2);
std::cout << j << std::endl;
}