json/docs/mkdocs/docs/examples/nlohmann_json_deserialize_enum_strict.cpp
Harinath Nampally 0cda972377 fix ci check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
2025-01-26 21:24:23 -05:00

36 lines
609 B
C++

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