-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
249 lines (220 loc) · 5.36 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
248
/*
An example of extracting DTO values from already parsed document.
It also shows to_json/from_json functions that accept reader-writer
object.
*/
#include <iostream>
#include <string>
#include <ctime>
#include <json_dto/pub.hpp>
namespace tutorial_16_1
{
// Update period command.
struct update_period_t
{
std::string m_units;
int m_period;
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::mandatory( "units", m_units )
& json_dto::mandatory( "period", m_period );
}
};
std::ostream & operator<<( std::ostream & to, const update_period_t & v )
{
return (to << v.m_period << v.m_units);
}
// Read sensor command.
struct read_sensor_t
{
std::string m_sensor_id;
int m_priority;
std::string m_publish_to;
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::mandatory( "sensor_id", m_sensor_id )
& json_dto::mandatory( "priority", m_priority )
& json_dto::mandatory( "topic", m_publish_to );
}
};
std::ostream & operator<<( std::ostream & to, const read_sensor_t & v )
{
return (to << v.m_sensor_id << "(" << v.m_priority << ") -> "
<< v.m_publish_to);
}
// Additional information.
struct extension_t
{
std::string m_id;
std::string m_payload;
extension_t() = default;
extension_t( std::string id, std::string payload )
: m_id{ std::move(id) }
, m_payload{ std::move(payload) }
{}
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::mandatory("Id", m_id)
& json_dto::mandatory("Payload", m_payload)
;
}
};
std::ostream & operator<<( std::ostream & to, const extension_t & ext )
{
return (to << "('" << ext.m_id << "','" << ext.m_payload << "')");
}
std::ostream & operator<<( std::ostream & to, const std::vector<extension_t> & exts )
{
bool comma_needed = false;
for( const auto & e : exts )
{
if( comma_needed )
to << ", ";
to << e;
comma_needed = true;
}
return to;
}
// Reader_Writer for vector of extension_t objects.
struct extension_reader_writer_t
{
void
read( std::vector< extension_t > & to, const rapidjson::Value & from ) const
{
using json_dto::read_json_value;
to.clear();
if( from.IsObject() )
{
extension_t single_value;
read_json_value( single_value, from );
to.push_back( std::move(single_value) );
}
else if( from.IsArray() )
{
read_json_value( to, from );
}
else
{
throw std::runtime_error{ "Unexpected format of extension_t value" };
}
}
void
write(
const std::vector< extension_t > & v,
rapidjson::Value & to,
rapidjson::MemoryPoolAllocator<> & allocator ) const
{
using json_dto::write_json_value;
if( 1u == v.size() )
write_json_value( v.front(), to, allocator );
else
write_json_value( v, to, allocator );
}
};
void parse_and_handle_message( const std::string & raw_msg )
{
rapidjson::Document whole_msg;
whole_msg.Parse< rapidjson::kParseDefaultFlags >( raw_msg );
if( whole_msg.HasParseError() )
throw std::runtime_error(
std::string{ "unable to parse message: " } +
rapidjson::GetParseError_En( whole_msg.GetParseError() ) );
std::string msg_type = whole_msg[ "message_type" ].GetString();
const auto & payload = whole_msg[ "payload" ];
if( "Update-Period" == msg_type )
{
std::cout << "Update-Period command: "
<< json_dto::from_json< update_period_t >( payload )
<< std::endl;
}
else if( "Read-Sensor" == msg_type )
{
std::cout << "Read-Sensor command: "
<< json_dto::from_json< read_sensor_t >( payload )
<< std::endl;
}
else
std::cout << "Unknown command: '" << msg_type << "'" << std::endl;
if( whole_msg.HasMember( "extension" ) )
{
const auto extensions =
json_dto::from_json< std::vector< extension_t > >(
extension_reader_writer_t{},
whole_msg[ "extension" ] );
std::cout << "Extensions: " << extensions << std::endl;
}
else
std::cout << "No extensions" << std::endl;
std::cout << "--- completed ---" << std::endl;
}
} /* namespace tutorial_16_1 */
using namespace tutorial_16_1;
int
main( int , char *[] )
{
try
{
parse_and_handle_message(
R"JSON({
"message_type" : "Read-Sensor",
"payload" : {
"sensor_id" : "a43-25",
"priority" : 0,
"topic" : "/meters/a/43/25"
}
})JSON" );
parse_and_handle_message(
R"JSON({
"message_type" : "Read-Sensor",
"payload" : {
"sensor_id" : "a43-25",
"priority" : 0,
"topic" : "/meters/a/43/25"
},
"extension" : {"Id": "1", "Payload": "0000-0000-0001"}
})JSON" );
parse_and_handle_message(
R"JSON({
"message_type" : "Read-Sensor",
"payload" : {
"sensor_id" : "a43-25",
"priority" : 0,
"topic" : "/meters/a/43/25"
},
"extension" : [
{"Id": "1", "Payload": "0000-0000-0001"},
{"Id": "2", "Payload": "0000-0000-0002"}
]
})JSON" );
parse_and_handle_message(
R"JSON({
"message_type" : "Update-Period",
"payload" : {
"units" : "ms",
"period" : 350
}
})JSON" );
parse_and_handle_message(
R"JSON({
"message_type" : "Update-Period",
"payload" : {
"units" : "ms",
"period" : 350
},
"extension" : [
{"Id": "3", "Payload": "0000-0000-0003"},
{"Id": "4", "Payload": "0000-0000-0004"},
{"Id": "5", "Payload": "0000-0000-0005"}
]
})JSON" );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << "." << std::endl;
return 1;
}
return 0;
}