mirror of
https://github.com/nlohmann/json
synced 2024-11-22 12:00:05 +00:00
7b6cf5918b
Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>
34 lines
687 B
C++
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;
|
|
}
|