-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.cpp
247 lines (206 loc) · 5.48 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
A sample using json_dto with custom Reader_Writer.
*/
#include <iostream>
#include <string>
#include <ctime>
#include <json_dto/pub.hpp>
enum class level_t : std::uint16_t
{
low, normal, high
};
enum class category_t : int
{
info, notice, alarm
};
// Reader_Writer for textual representation of level_t/category_t.
struct textual_reader_writer_t
{
void
read( level_t & v, const rapidjson::Value & from ) const
{
using json_dto::read_json_value;
std::string str_v;
read_json_value( str_v, from );
if( "low" == str_v ) v = level_t::low;
else if( "normal" == str_v ) v = level_t::normal;
else if( "high" == str_v ) v = level_t::high;
else throw json_dto::ex_t{ "invalid value for level_t" };
}
void
write(
const level_t & v,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator ) const
{
using json_dto::write_json_value;
using json_dto::string_ref_t;
switch( v )
{
case level_t::low:
write_json_value( string_ref_t{ "low" }, to, allocator );
break;
case level_t::normal:
write_json_value( string_ref_t{ "normal" }, to, allocator );
break;
case level_t::high:
write_json_value( string_ref_t{ "high" }, to, allocator );
break;
}
}
void
read( category_t & v, const rapidjson::Value & from ) const
{
using json_dto::read_json_value;
std::string str_v;
read_json_value( str_v, from );
if( "info" == str_v ) v = category_t::info;
else if( "notice" == str_v ) v = category_t::notice;
else if( "alarm" == str_v ) v = category_t::alarm;
else throw json_dto::ex_t{ "invalid value for category_t" };
}
void
write(
const category_t & v,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator ) const
{
using json_dto::write_json_value;
using json_dto::string_ref_t;
switch( v )
{
case category_t::info:
write_json_value( string_ref_t{ "info" }, to, allocator );
break;
case category_t::notice:
write_json_value( string_ref_t{ "notice" }, to, allocator );
break;
case category_t::alarm:
write_json_value( string_ref_t{ "alarm" }, to, allocator );
break;
}
}
};
// Reader_Writer for numeric representation of level_t/category_t.
struct numeric_reader_writer_t
{
template< typename Field_Type >
void
read( Field_Type & v, const rapidjson::Value & from ) const
{
static_assert( std::is_enum<Field_Type>::value,
"Field_Type is expected to be an enum type" );
using json_dto::read_json_value;
using ut = std::underlying_type_t< Field_Type >;
ut representation;
read_json_value( representation, from );
v = static_cast<Field_Type>(representation);
}
template< typename Field_Type >
void
write(
const Field_Type & v,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator ) const
{
static_assert( std::is_enum<Field_Type>::value,
"Field_Type is expected to be an enum type" );
using json_dto::write_json_value;
using ut = std::underlying_type_t< Field_Type >;
const ut representation{ static_cast<ut>(v) };
write_json_value( representation, to, allocator );
}
};
// Description of message category and level.
struct message_properties_t
{
level_t m_level{ level_t::low };
category_t m_category{ category_t::info };
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::optional( textual_reader_writer_t{},
"level", m_level, level_t::low )
& json_dto::optional( textual_reader_writer_t{},
"cat", m_category, category_t::info );
}
};
// Message.
struct message_t
{
message_t() {}
message_t(
std::string from,
std::int64_t when,
std::string text )
: m_from{ std::move( from ) }
, m_when{ when }
, m_text{ std::move( text ) }
{}
// Who sent a message.
std::string m_from;
// When the message was sent (unixtime).
std::int64_t m_when;
// Message text.
std::string m_text;
// Message category as numeric code.
category_t m_category;
// Message importance level as numeric code.
level_t m_level;
// Optional description of message properties.
json_dto::nullable_t< message_properties_t > m_properties;
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::mandatory( "from", m_from )
& json_dto::mandatory( "when", m_when )
& json_dto::mandatory( "text", m_text )
& json_dto::mandatory( numeric_reader_writer_t{},
"category-code", m_category )
& json_dto::mandatory( numeric_reader_writer_t{},
"level-code", m_level )
& json_dto::optional( "properties", m_properties, nullptr );
}
};
const std::string json_data{
R"JSON({
"from" : "json_dto",
"when" : 1474884330,
"text" : "Hello world!",
"category-code" : 0,
"level-code" : 1
})JSON" };
int
main( int , char *[] )
{
try
{
{
auto msg = json_dto::from_json< message_t >( json_data );
const auto t = static_cast< std::time_t >( msg.m_when );
std::cout
<< "Deserialized from JSON:\n"
<< "\tfrom: " << msg.m_from << "\n"
<< "\twhen: " << std::ctime( &t )
<< "\tcat: " << static_cast<int>(msg.m_category)
<< "\tlvl: " << static_cast<unsigned>(msg.m_level)
<< "\ttext: " << msg.m_text << std::endl;
}
{
message_t msg{ "json_dto", std::time( nullptr ), "Hello once again!" };
msg.m_category = category_t::alarm;
msg.m_level = level_t::normal;
msg.m_properties = message_properties_t{
msg.m_level, msg.m_category };
std::cout
<< "\nSerialized to JSON:\n"
<< json_dto::to_json( msg ) << std::endl;
}
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << "." << std::endl;
return 1;
}
return 0;
}