This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
forked from ar90n/msgpack11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
184 lines (151 loc) · 5.45 KB
/
example.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <tuple>
#include <array>
#include <algorithm>
#include <msgpack11.hpp>
using namespace msgpack11;
void show_impl(MsgPack const& blob, int level);
void show_null(MsgPack const& blob, int level) {
std::cout << "nil";
}
void show_bool(MsgPack const& blob, int level) {
std::cout << (blob.bool_value() ? "true" : "false");
}
void show_float32(MsgPack const& blob, int level) {
std::cout << std::to_string(blob.float32_value());
}
void show_float64(MsgPack const& blob, int level) {
std::cout << std::to_string(blob.float64_value());
}
void show_int8(MsgPack const& blob, int level) {
std::cout << std::to_string(blob.int8_value());
}
void show_int16(MsgPack const& blob, int level) {
std::cout << blob.int16_value();
}
void show_int32(MsgPack const& blob, int level) {
std::cout << blob.int32_value();
}
void show_int64(MsgPack const& blob, int level) {
std::cout << blob.int64_value();
}
void show_uint8(MsgPack const& blob, uint level) {
std::cout << std::to_string(blob.uint8_value());
}
void show_uint16(MsgPack const& blob, uint level) {
std::cout << blob.uint16_value();
}
void show_uint32(MsgPack const& blob, uint level) {
std::cout << blob.uint32_value();
}
void show_uint64(MsgPack const& blob, uint level) {
std::cout << blob.uint64_value();
}
void show_string(MsgPack const& blob, int level) {
std::cout << blob.string_value();
}
void show_array(MsgPack const& blob, int level) {
std::cout << "[ ";
MsgPack::array const& elements = blob.array_items();
std::for_each( elements.begin(), elements.end(), [level](MsgPack const& elm) {
show_impl( elm, level );
std::cout << ", ";
});
std::cout << "]";
}
void show_binary(MsgPack const& blob, int level) {
std::cout << "[ ";
MsgPack::binary const& elements = blob.binary_items();
std::for_each( elements.begin(), elements.end(), [level](uint8_t const elm) {
std::cout << std::to_string(elm) << ", ";
});
std::cout << "]";
}
void show_object(MsgPack const& blob, int level) {
bool is_first = level == 0;
MsgPack::object const& elements = blob.object_items();
std::for_each( elements.begin(), elements.end(), [level, &is_first](std::pair< MsgPack, MsgPack > const& elm ) {
if(!is_first)
{
std::cout << std::endl;
}
is_first = false;
for( int i = 0; i < (2 * level) ; ++i )
{
std:: cout << " ";
}
show_impl( elm.first, level + 1 );
std::cout << " : ";
show_impl( elm.second, level + 1 );
});
}
void show_extension(MsgPack const& blob, int level) {
int8_t const info = std::get<0>(blob.extension_items());
MsgPack::binary const& elements = std::get<1>( blob.extension_items());
std::cout << info << " - ";
std::for_each( elements.begin(), elements.end(), [level](uint8_t const elm) {
std::cout << std::to_string(elm) << ",";
});
}
void show_impl(MsgPack const& blob, int level) {
using func_pair = std::tuple< bool (MsgPack::*)() const, std::function< void(MsgPack const&, int) > >;
static std::array<func_pair, 17> const func_table{
std::make_tuple(&MsgPack::is_null, show_null),
std::make_tuple(&MsgPack::is_bool, show_bool),
std::make_tuple(&MsgPack::is_float32, show_float32),
std::make_tuple(&MsgPack::is_float64, show_float64),
std::make_tuple(&MsgPack::is_int8, show_int8),
std::make_tuple(&MsgPack::is_int16, show_int16),
std::make_tuple(&MsgPack::is_int32, show_int32),
std::make_tuple(&MsgPack::is_int64, show_int64),
std::make_tuple(&MsgPack::is_uint8, show_uint8),
std::make_tuple(&MsgPack::is_uint16, show_uint16),
std::make_tuple(&MsgPack::is_uint32, show_uint32),
std::make_tuple(&MsgPack::is_uint64, show_uint64),
std::make_tuple(&MsgPack::is_string, show_string),
std::make_tuple(&MsgPack::is_array, show_array),
std::make_tuple(&MsgPack::is_binary, show_binary),
std::make_tuple(&MsgPack::is_object, show_object),
std::make_tuple(&MsgPack::is_extension, show_extension)
};
std::for_each(func_table.begin(), func_table.end(), [&blob,level](func_pair const& funcs) {
auto pred_pointer = std::get<0>(funcs);
auto show_func = std::get<1>(funcs);
if((blob.*pred_pointer)()) {
show_func(blob,level);
}
});
}
void show(MsgPack const& blob ) {
show_impl( blob, 0 );
std::cout << std::endl;
}
int main(int argc, char const *argv[]) {
MsgPack obj = MsgPack::object {
{ "key1", "value1" },
{ "key2", false },
{ "key3", MsgPack::array { 1, 2, 3 } },
{ "key4", MsgPack::object {
{"key4-a" , 1024},
{"key4-b" , MsgPack::binary { 0, 2, 4, 6 }}
}},
{ "key5", MsgPack::array {
MsgPack::object {
{"key5-1-a", 100},
{"key5-1-b", 200}
},
MsgPack::object {
{"key5-2-a", 300},
{"key5-2-b", 400}
}
}}
};
std::cout << "---- original ----" << std::endl;
show(obj);
// serialize and deserialize
std::string err;
MsgPack serdes_obj = MsgPack::parse(obj.dump(), err);
std::cout << "---- ser/des result ----" << std::endl;
show(serdes_obj);
return 0;
}