-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
194 lines (166 loc) · 3.9 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
/*
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>
namespace tutorial_20_1
{
template< typename F >
struct serialize_only_proxy_t
{
using field_type = const F;
const F * m_field;
};
template< typename F >
serialize_only_proxy_t<F> serialize_only( const F & field ) noexcept
{
return { &field };
}
} /* namespace tutorial_20_1 */
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_1::serialize_only_proxy_t<Field_Type>,
Manopt_Policy,
Validator >
: public binder_data_holder_t<
Reader_Writer,
typename tutorial_20_1::serialize_only_proxy_t<Field_Type>::field_type,
Manopt_Policy,
Validator >
{
using serialize_only_proxy =
tutorial_20_1::serialize_only_proxy_t<Field_Type>;
using actual_field_type = typename serialize_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 serialize_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_read_from_implementation_t<
binder_data_holder_t<
Reader_Writer,
const tutorial_20_1::serialize_only_proxy_t<Field_Type>,
Manopt_Policy,
Validator
>
>
{
using data_holder_t = binder_data_holder_t<
Reader_Writer,
const tutorial_20_1::serialize_only_proxy_t<Field_Type>,
Manopt_Policy,
Validator >;
static void
read_from(
const data_holder_t & /*binder_data*/,
const rapidjson::Value & /*object*/ )
{
// Nothing to do.
}
};
} /* 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_1::serialize_only( ids() ) )
& json_dto::mandatory( "payload", m_payload )
& json_dto::optional( "priority",
tutorial_20_1::serialize_only( m_priority ),
0u )
& json_dto::optional( "version",
tutorial_20_1::serialize_only( 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;
}
const std::string json_data{
R"JSON({
"ids" : [0, 2, 4, 8, 16, 32],
"payload" : 88
})JSON" };
int
main( int , char *[] )
{
try
{
{
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
<< "\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;
}