mirror of
https://github.com/nlohmann/json.git
synced 2026-05-19 20:55:24 +00:00
deploy: 58cfecf7f7
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace ns
|
||||
{
|
||||
enum TaskState
|
||||
{
|
||||
TS_STOPPED,
|
||||
TS_RUNNING,
|
||||
TS_COMPLETED,
|
||||
TS_INVALID = -1
|
||||
};
|
||||
|
||||
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(TaskState,
|
||||
{
|
||||
{ TS_INVALID, nullptr },
|
||||
{ TS_STOPPED, "stopped" },
|
||||
{ TS_RUNNING, "running" },
|
||||
{ TS_COMPLETED, "completed" }
|
||||
})
|
||||
|
||||
enum class Color
|
||||
{
|
||||
red, green, blue, unknown
|
||||
};
|
||||
|
||||
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(Color,
|
||||
{
|
||||
{ Color::unknown, "unknown" }, { Color::red, "red" },
|
||||
{ Color::green, "green" }, { Color::blue, "blue" }
|
||||
})
|
||||
} // namespace ns
|
||||
|
||||
int main()
|
||||
{
|
||||
// serialization
|
||||
json j_stopped = ns::TS_STOPPED;
|
||||
json j_red = ns::Color::red;
|
||||
std::cout << "ns::TS_STOPPED -> " << j_stopped
|
||||
<< ", ns::Color::red -> " << j_red << std::endl;
|
||||
|
||||
// deserialization
|
||||
json j_running = "running";
|
||||
json j_blue = "blue";
|
||||
auto running = j_running.get<ns::TaskState>();
|
||||
auto blue = j_blue.get<ns::Color>();
|
||||
std::cout << j_running << " -> " << running
|
||||
<< ", " << j_blue << " -> " << static_cast<int>(blue) << std::endl;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
ns::TS_STOPPED -> "stopped", ns::Color::red -> "red"
|
||||
"running" -> 1, "blue" -> 2
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace ns
|
||||
{
|
||||
enum class Color
|
||||
{
|
||||
red, green, blue, unknown
|
||||
};
|
||||
|
||||
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(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.get<ns::Color>();
|
||||
auto red = j_red.get<ns::Color>();
|
||||
std::cout << j_rot << " -> " << static_cast<int>(rot) << std::endl;
|
||||
std::cout << j_red << " -> " << static_cast<int>(red) << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
0 -> "red"
|
||||
"rot" -> 0
|
||||
"red" -> 0
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace ns
|
||||
{
|
||||
|
||||
enum class Color
|
||||
{
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
unknown // not mapped in JSON_SERIALIZE_ENUM_STRICT
|
||||
};
|
||||
|
||||
NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(Color,
|
||||
{
|
||||
{Color::red, "red"},
|
||||
{Color::green, "green"},
|
||||
{Color::blue, "blue"}
|
||||
})
|
||||
|
||||
} // namespace ns
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
// invalid serialization
|
||||
try
|
||||
{
|
||||
// ns::color::unknown was not mapped in macro
|
||||
json invalid_serialization = ns::Color::unknown;
|
||||
}
|
||||
catch (const json::exception e)
|
||||
{
|
||||
std::cout << "deserialization failed: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
// invalid deserialization
|
||||
try
|
||||
{
|
||||
// what does not map to an enum
|
||||
json invalid_deserialization("what");
|
||||
ns::Color color = invalid_deserialization.get<ns::Color>();
|
||||
}
|
||||
catch (const json::exception e)
|
||||
{
|
||||
std::cout << "deserialization failed: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
deserialization failed: [json.exception.out_of_range.410] enum value out of range for Color
|
||||
deserialization failed: [json.exception.out_of_range.410] enum value out of range for Color: "what"
|
||||
Reference in New Issue
Block a user