1
0
mirror of https://github.com/nlohmann/json synced 2024-11-26 05:40:07 +00:00
nlohmannjson/docs/examples/nlohmann_json_serialize_enum_2.cpp
No c71ecde505
Use template get instead of get in examples (#4039)
Co-authored-by: tusooa <tusooa@kazv.moe>
2023-06-11 10:06:24 +02:00

34 lines
810 B
C++

#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace ns
{
enum class Color
{
red, green, blue, unknown
};
NLOHMANN_JSON_SERIALIZE_ENUM(Color,
{
{ Color::unknown, "unknown" }, { Color::red, "red" },
{ Color::green, "green" }, { Color::blue, "blue" },
{ Color::red, "rot" } // a second conversion for Color::red
})
}
int main()
{
// serialization
json j_red = ns::Color::red;
std::cout << static_cast<int>(ns::Color::red) << " -> " << j_red << std::endl;
// deserialization
json j_rot = "rot";
auto rot = j_rot.template get<ns::Color>();
auto red = j_red.template get<ns::Color>();
std::cout << j_rot << " -> " << static_cast<int>(rot) << std::endl;
std::cout << j_red << " -> " << static_cast<int>(red) << std::endl;
}