json/docs/mkdocs/docs/examples/nlohmann_json_serialize_enum_strict.cpp
Harinath Nampally 11165ef68d address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
2025-01-26 18:13:37 -05:00

35 lines
603 B
C++

#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace ns
{
enum class Color
{
red, green, blue, pink
};
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(Color,
{
{ Color::red, "red" },
{ Color::green, "green" },
{ Color::blue, "blue" },
})
}
int main()
{
// serialization
try
{
json j_red = ns::Color::pink;
auto color = j_red.get<ns::Color>();
std::cout << static_cast<int>(color) << " -> " << j_red << std::endl;
}
catch (const nlohmann::json::exception& e)
{
std::cout << e.what() << std::endl;
}
}