-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.cpp
205 lines (172 loc) · 4.03 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
/*
A sample of creation own specialization of json_dto's
binder_data_holder_t and binder_write_to_implementation.
*/
#include <cstdio>
#include <iostream>
#include <map>
#include <json_dto/pub.hpp>
namespace tutorial_20_2
{
template< typename F >
struct deserialize_only_proxy_t
{
using field_type = F;
F * m_field;
};
template< typename F >
deserialize_only_proxy_t<F> deserialize_only( F & field ) noexcept
{
static_assert( !std::is_const<F>::value,
"deserialize_only can't be used with const objects" );
return { &field };
}
} /* namespace tutorial_20_2 */
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_2::deserialize_only_proxy_t<Field_Type>,
Manopt_Policy,
Validator >
: public binder_data_holder_t<
Reader_Writer,
typename tutorial_20_2::deserialize_only_proxy_t<Field_Type>::field_type,
Manopt_Policy,
Validator >
{
using deserialize_only_proxy =
tutorial_20_2::deserialize_only_proxy_t<Field_Type>;
using actual_field_type = typename deserialize_only_proxy::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 deserialize_only_proxy & 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_write_to_implementation_t<
binder_data_holder_t<
Reader_Writer,
const tutorial_20_2::deserialize_only_proxy_t<Field_Type>,
Manopt_Policy,
Validator
>
>
{
using data_holder_t = binder_data_holder_t<
Reader_Writer,
const tutorial_20_2::deserialize_only_proxy_t<Field_Type>,
Manopt_Policy,
Validator >;
static void
write_to(
const data_holder_t & /*binder_data*/,
rapidjson::Value & /*object*/,
rapidjson::MemoryPoolAllocator<> & /*allocator*/ )
{
// Nothing to do.
}
};
} /* namespace json_dto */
struct example_data
{
std::uint32_t m_a{};
std::uint32_t m_b{};
std::uint32_t m_c{};
template < typename Json_Io >
void
json_io( Json_Io & io )
{
io
& json_dto::mandatory( "a",
tutorial_20_2::deserialize_only( m_a ) )
& json_dto::optional( "b",
tutorial_20_2::deserialize_only( m_b ), 42u )
& json_dto::optional_no_default( "c", m_c );
}
};
std::ostream &
operator<<( std::ostream & to, const example_data & what )
{
return (to << "(a: " << what.m_a << ", b: " << what.m_b
<< ", c: " << what.m_c << ")");
}
int
main( int , char *[] )
{
try
{
{
const std::string json_data = R"json(
{"a": 1, "b": 2}
)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 std::string json_data = R"json(
{"a": 1, "c": 2}
)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 std::string json_data = R"json(
{"b": 1, "c": 2}
)json";
try
{
auto data = json_dto::from_json< example_data >( json_data );
std::cout << "We shouldn't be here, but: " << data << std::endl;
}
catch( const std::exception & x )
{
std::cout << "expected exception: " << x.what() << std::endl;
}
}
{
example_data data;
data.m_a = 111u;
data.m_b = 222u;
data.m_c = 333u;
std::cout
<< "\nSerialized to JSON:\n"
<< json_dto::to_json( data ) << std::endl;
}
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << "." << std::endl;
return 1;
}
return 0;
}