forked from bk192077/struct_mapping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumeration.cpp
79 lines (65 loc) · 1.4 KB
/
enumeration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "struct_mapping/struct_mapping.h"
#include <iostream>
#include <list>
#include <map>
#include <sstream>
#include <string>
namespace sm = struct_mapping;
enum class Color
{
red,
blue,
green,
};
static Color ColorFromString(const std::string& value)
{
if (value == "red")
{
return Color::red;
}
else if (value == "blue")
{
return Color::blue;
}
return Color::green;
}
static std::string ColorToString(Color color)
{
switch (color)
{
case Color::red: return "red";
case Color::green: return "green";
default: return "blue";
}
}
struct Palette
{
Color main_color;
Color background_color;
std::list<Color> special_colors;
std::map<std::string, Color> colors;
};
int main()
{
sm::MemberString<Color>::set(ColorFromString, ColorToString);
sm::reg(&Palette::main_color, "main_color", sm::Required{});
sm::reg(&Palette::background_color, "background_color", sm::Default{Color::blue});
sm::reg(&Palette::special_colors, "special_colors");
sm::reg(&Palette::colors, "colors");
Palette palette;
std::istringstream json_data(R"json(
{
"main_color": "green",
"special_colors": ["green", "green", "red"],
"colors": {
"dark": "green",
"light": "red",
"neutral": "blue"
}
}
)json");
sm::map_json_to_struct(palette, json_data);
std::ostringstream out_json_data;
struct_mapping::map_struct_to_json(palette, out_json_data, " ");
std::cout << out_json_data.str() << std::endl;
}