mirror of
https://github.com/nlohmann/json.git
synced 2026-03-01 23:06:27 +00:00
Overwork documentation (#3444)
* 📝 overwork macro documentation * 📝 address review comments * 🔧 add style check to Makefile * 🙈 overwork .gitignore * 📌 Pygments 2.12.0 is broken * ✏️ fix links * 🚸 adjust output to cppcheck * 📝 add titles to more admonitions * ✏️ fix typos * 📝 document future behavior change
This commit is contained in:
33
doc/examples/nlohmann_json_serialize_enum_2.cpp
Normal file
33
doc/examples/nlohmann_json_serialize_enum_2.cpp
Normal file
@@ -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(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;
|
||||
}
|
||||
Reference in New Issue
Block a user