-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
264 lines (226 loc) · 5.67 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
A sample of creation own specialization of json_dto's
binder_data_holder_t and binder_read_from_implementation.
*/
#include <cstdio>
#include <iostream>
#include <map>
#include <json_dto/pub.hpp>
#include <json_dto/validators.hpp>
namespace tutorial_20_3
{
template< typename F >
struct ignore_after_deserialization_proxy_t
{
using field_type = const F;
const F * m_field;
};
template< typename F >
ignore_after_deserialization_proxy_t<F>
ignore_after_deserialization( const F & field ) noexcept
{
return { &field };
}
} /* namespace tutorial_20_3 */
namespace json_dto
{
template<
typename Reader_Writer,
typename Field_Type,
typename Manopt_Policy,
typename Validator >
class binder_data_holder_t<
Reader_Writer,
const tutorial_20_3::ignore_after_deserialization_proxy_t<Field_Type>,
Manopt_Policy,
Validator >
: public binder_data_holder_t<
Reader_Writer,
typename tutorial_20_3::ignore_after_deserialization_proxy_t<Field_Type>::field_type,
Manopt_Policy,
Validator >
{
using proxy_type =
tutorial_20_3::ignore_after_deserialization_proxy_t<Field_Type>;
using actual_field_type = typename proxy_type::field_type;
using base_type = binder_data_holder_t<
Reader_Writer,
actual_field_type,
Manopt_Policy,
Validator >;
public:
binder_data_holder_t(
Reader_Writer && reader_writer,
string_ref_t field_name,
const proxy_type & proxy,
Manopt_Policy && manopt_policy,
Validator && validator )
: base_type{
std::move(reader_writer),
field_name,
*(proxy.m_field),
std::move(manopt_policy),
std::move(validator)
}
{}
};
template<
typename Reader_Writer,
typename Field_Type,
typename Manopt_Policy,
typename Validator >
struct binder_read_from_implementation_t<
binder_data_holder_t<
Reader_Writer,
const tutorial_20_3::ignore_after_deserialization_proxy_t<Field_Type>,
Manopt_Policy,
Validator
>
>
{
using proxy_type = tutorial_20_3::ignore_after_deserialization_proxy_t<Field_Type>;
using data_holder_t = binder_data_holder_t<
Reader_Writer,
const proxy_type,
Manopt_Policy,
Validator >;
static void
read_from(
const data_holder_t & binder_data,
const rapidjson::Value & object )
{
if( !object.IsObject() )
{
throw ex_t{
"unable to extract field \"" +
std::string{ binder_data.field_name().s } + "\": "
"parent json type must be object" };
}
// Temporary object for holding deserialized value.
Field_Type tmp_object{};
const auto it = object.FindMember( binder_data.field_name() );
if( object.MemberEnd() != it )
{
const auto & value = it->value;
if( !value.IsNull() )
{
binder_data.reader_writer().read( tmp_object, value );
}
else
{
binder_data.manopt_policy().on_null( tmp_object );
}
}
else
{
binder_data.manopt_policy().on_field_not_defined( tmp_object );
}
binder_data.validator()( tmp_object ); // validate value.
// NOTE: the value from tmp_object will be lost.
}
};
} /* namespace json_dto */
struct example_data
{
std::vector< std::uint32_t > ids() const { return { 1u, 2u, 3u, 4u }; }
std::uint32_t m_payload{};
std::uint32_t m_priority{};
int m_version_base{ 18 };
int version() const noexcept { return m_version_base + 2; }
example_data() = default;
example_data( std::uint32_t payload ) : m_payload{ payload } {}
template < typename Json_Io >
void
json_io( Json_Io & io )
{
io
& json_dto::mandatory( "ids",
tutorial_20_3::ignore_after_deserialization( ids() ) )
& json_dto::mandatory( "payload", m_payload )
& json_dto::mandatory( "priority",
tutorial_20_3::ignore_after_deserialization( m_priority ),
json_dto::min_max_constraint(
std::uint32_t{0u},
std::uint32_t{9u} ) )
& json_dto::optional( "version",
tutorial_20_3::ignore_after_deserialization( version() ),
18 );
}
};
std::ostream &
operator<<( std::ostream & to, const example_data & what )
{
to << "(ids: (";
const auto & ids = what.ids();
for( const auto id : ids ) to << id << " ";
to << "), payload: " << what.m_payload << ", priority: "
<< what.m_priority << ", version: "
<< what.version() << ")";
return to;
}
int
main( int , char *[] )
{
try
{
{
const std::string json_data{
R"JSON({
"ids" : [0, 2, 4, 8, 16, 32],
"payload" : 88,
"priority" : 3
})JSON" };
auto data = json_dto::from_json< example_data >( json_data );
std::cout << "Deserialized from JSON: " << data << std::endl;
std::cout << "Serialized again: " << json_dto::to_json( data )
<< std::endl;
}
{
const example_data data{ 42u };
std::cout
<< "\nSerialized to JSON:\n"
<< json_dto::to_json( data ) << std::endl;
}
{
example_data data{ 42u };
data.m_priority = 15u;
data.m_version_base = 16;
std::cout << "\nTry to serialize object with invalid `priority` value"
<< std::endl;
try
{
const auto json_value = json_dto::to_json( data );
std::cout << "\nSerialized to JSON:\n" << json_value << std::endl;
}
catch( const std::exception & x )
{
std::cout << "Expected exception: " << x.what() << std::endl;
}
}
{
std::cout << "\nTry to deserialize object with invalid `priority` value"
<< std::endl;
try
{
const std::string json_data{
R"JSON({
"ids" : [0, 2, 4, 8, 16, 32],
"payload" : 88,
"priority" : 33
})JSON" };
auto data = json_dto::from_json< example_data >( json_data );
std::cout << "Deserialized from JSON: " << data << std::endl;
}
catch( const std::exception & x )
{
std::cout << "Expected exception: " << x.what() << std::endl;
}
}
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << "." << std::endl;
return 1;
}
return 0;
}