-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
160 lines (133 loc) · 3 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
/*
A sample using json_dto
*/
#include <thread>
#include <iostream>
#include <string>
#include <ctime>
#include <json_dto/pub.hpp>
// Message source.
struct message_source_t
{
message_source_t() {}
message_source_t(
std::int32_t thread_id,
std::string subsystem )
: m_thread_id{ thread_id }
, m_subsystem{ std::move( subsystem ) }
{}
std::int32_t m_thread_id{ 0 };
std::string m_subsystem{};
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::optional( "thread_id", m_thread_id, 0 )
& json_dto::mandatory( "subsystem", m_subsystem );
}
};
// Single mesage.
struct message_t
{
message_t() {}
message_t(
message_source_t from,
std::int64_t when,
std::string text )
: m_from{ std::move( from ) }
, m_when{ when }
, m_text{ std::move( text ) }
{}
message_t(
std::int64_t when,
std::string text )
: m_when{ when }
, m_text{ std::move( text ) }
{}
// Who sent a message.
json_dto::nullable_t< message_source_t > m_from{};
// When the message was sent (unixtime).
std::int64_t m_when{};
// Message text.
std::string m_text{};
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::optional( "from", m_from, nullptr )
& json_dto::mandatory( "when", m_when )
& json_dto::mandatory( "text", m_text );
}
};
// A bunch of messages, all from one source.
struct message_pack_t
{
message_pack_t() {}
message_pack_t(
message_source_t from )
: m_from{ std::move( from ) }
{}
// Who sent a messages.
message_source_t m_from;
std::vector< message_t > m_messages;
template< typename Json_Io >
void json_io( Json_Io & io )
{
io & json_dto::mandatory( "from", m_from )
& json_dto::mandatory( "messages", m_messages );
}
};
const std::string json_data{
R"JSON({
"from" :
{
"subsystem": "json_dto"
},
"messages" :
[
{
"when" : 1474884330,
"text" : "Hello"
},
{
"when" : 1474884331,
"text" : " world!"
}
]
})JSON" };
int
main( int , char *[] )
{
try
{
{
auto msg_pack = json_dto::from_json< message_pack_t >( json_data );
std::cout
<< "Deserialized from JSON:\n"
<< "\tfrom: " << msg_pack.m_from.m_subsystem << "\n"
<< "\tmessages:\n";
for( const auto & msg : msg_pack.m_messages )
{
const auto t = static_cast< std::time_t >( msg.m_when );
std::cout
<< "\t\tmessage:\n"
<< "\t\t\twhen: " << std::ctime( &t )
<< "\t\t\ttext: " << msg.m_text << "\n";
}
std::cout << std::endl;
}
{
message_pack_t msg_pack{ message_source_t{ 42, "json_dto sample" } };
msg_pack.m_messages.emplace_back( std::time( nullptr ), "Hello" );
msg_pack.m_messages.emplace_back( std::time( nullptr ), "once" );
msg_pack.m_messages.emplace_back( std::time( nullptr ), "again!" );
std::cout
<< "\nSerialized to JSON:\n"
<< json_dto::to_json( msg_pack ) << std::endl;
}
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << "." << std::endl;
return 1;
}
return 0;
}