1
0
mirror of https://github.com/nlohmann/json.git synced 2025-08-06 07:02:42 +03:00

Deployed fe4b663 with MkDocs version: 1.4.2

This commit is contained in:
2023-03-08 12:43:29 +00:00
commit 55f10f44a9
702 changed files with 30461 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
#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;
}