-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathdoc_forward_conversion.cpp
More file actions
255 lines (195 loc) · 5.77 KB
/
doc_forward_conversion.cpp
File metadata and controls
255 lines (195 loc) · 5.77 KB
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
//
// Copyright (c) 2021 Dmitry Arkhipov (grisumbras@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//
// tag::doc_forward_conversion_1[]
namespace boost {
namespace json {
class value;
struct value_from_tag;
template< class T >
struct try_value_to_tag;
template< class T1, class T2 >
struct result_for;
template< class T >
void value_from( T&& t, value& jv );
template< class T >
typename result_for< T, value >::type
try_value_to( const value& jv );
}
}
// end::doc_forward_conversion_1[]
// tag::doc_forward_conversion_4[]
namespace boost {
namespace json {
template< class T >
struct has_value_from;
template< class T >
struct has_value_to;
}
}
// end::doc_forward_conversion_4[]
// tag::doc_forward_conversion_3[]
namespace boost {
namespace json {
class value;
struct value_from_tag;
template< class T >
struct try_value_to_tag;
template< class T1, class T2 >
struct result_for;
template< class T, class Context >
void value_from( T&& t, value& jv, const Context& ctx );
template< class T, class Context >
typename result_for< T, value >::type
try_value_to( const value& jv, const Context& ctx );
}
}
// end::doc_forward_conversion_3[]
#include "doc_types.hpp"
#include <system_error>
// tag::doc_forward_conversion_2[]
namespace user_ns
{
template< class JsonValue >
void tag_invoke(
const boost::json::value_from_tag&, JsonValue& jv, const ip_address& addr )
{
const unsigned char* b = addr.begin();
jv = { b[0], b[1], b[2], b[3] };
}
template< class JsonValue >
typename boost::json::result_for< ip_address, JsonValue >::type
tag_invoke(
const boost::json::try_value_to_tag< ip_address >&,
const JsonValue& jv )
{
using namespace boost::json;
if( !jv.is_array() )
return make_error_code( std::errc::invalid_argument );
auto const& arr = jv.get_array();
if( arr.size() != 4 )
return make_error_code( std::errc::invalid_argument );
auto oct1 = try_value_to< unsigned char >( arr[0] );
if( !oct1 )
return make_error_code( std::errc::invalid_argument );
auto oct2 = try_value_to< unsigned char >( arr[1] );
if( !oct2 )
return make_error_code( std::errc::invalid_argument );
auto oct3 = try_value_to< unsigned char >( arr[2] );
if( !oct3 )
return make_error_code( std::errc::invalid_argument );
auto oct4 = try_value_to< unsigned char >( arr[3] );
if( !oct4 )
return make_error_code( std::errc::invalid_argument );
return ip_address{ *oct1, *oct2, *oct3, *oct4 };
}
}
// end::doc_forward_conversion_2[]
// tag::doc_forward_conversion_3[]
namespace user_ns
{
struct as_string
{ };
template< class JsonValue >
void
tag_invoke(
const boost::json::value_from_tag&, JsonValue& jv,
const ip_address& addr,
const as_string& )
{
auto& js = jv.emplace_string();
js.resize( 4 * 3 + 3 + 1 ); // XXX.XXX.XXX.XXX\0
auto it = addr.begin();
auto n = std::snprintf(
js.data(), js.size(), "%hhu.%hhu.%hhu.%hhu", it[0], it[1], it[2], it[3] );
js.resize(n);
}
template< class JsonValue >
typename boost::json::result_for< ip_address, JsonValue >::type
tag_invoke(
const boost::json::try_value_to_tag< ip_address >&,
const JsonValue& jv,
const as_string& )
{
const auto* js = jv.if_string();
if( ! js )
return make_error_code( std::errc::invalid_argument );
unsigned char octets[4];
int result = std::sscanf(
js->data(), "%hhu.%hhu.%hhu.%hhu", octets, octets + 1, octets + 2, octets + 3 );
if( result != 4 )
return make_error_code( std::errc::invalid_argument );
return ip_address( octets[0], octets[1], octets[2], octets[3] );
}
}
// end::doc_forward_conversion_3[]
// tag::doc_forward_conversion_4[]
namespace third_party
{
struct json_storage
{
boost::json::value& jv;
template< class T >
friend
typename std::enable_if< boost::json::has_value_from<T>::value >::type
process(json_storage& s, T const& t)
{
boost::json::value_from(t, s.jv);
}
template< class T >
friend
typename std::enable_if< ! boost::json::has_value_from<T>::value >::type
process(json_storage& s, T const&)
{
boost::json::value_from("unknown value", s.jv);
}
};
}
// end::doc_forward_conversion_4[]
#include <boost/json/value_from.hpp>
#include <boost/json/value_to.hpp>
#include "test_suite.hpp"
namespace boost {
namespace json {
class doc_forward_conversion
{
public:
void
run()
{
value jv{ 212, 115, 81, 22 };
auto const addr = value_to< user_ns::ip_address >( jv );
BOOST_TEST( get<0>(addr) == 212 );
BOOST_TEST( get<1>(addr) == 115 );
BOOST_TEST( get<2>(addr) == 81 );
BOOST_TEST( get<3>(addr) == 22 );
value const jv2 = value_from( addr );
BOOST_TEST( jv == jv2 );
jv = value_from( addr, user_ns::as_string() );
BOOST_TEST( jv.is_string() );
string const& js = jv.get_string();
BOOST_TEST( js == "212.115.81.22" );
auto const addr2 = value_to< user_ns::ip_address >(
jv, user_ns::as_string() );
BOOST_TEST( get<0>(addr2) == 212 );
BOOST_TEST( get<1>(addr2) == 115 );
BOOST_TEST( get<2>(addr2) == 81 );
BOOST_TEST( get<3>(addr2) == 22 );
jv.emplace_null();
third_party::json_storage s{jv};
process(s, addr2);
BOOST_TEST(( jv == value{ 212, 115, 81, 22 } ));
struct some_struct {};
jv.emplace_null();
process(s, some_struct{});
BOOST_TEST(( jv == value{ "unknown value" } ));
}
};
TEST_SUITE(doc_forward_conversion, "boost.json.doc_forward_conversion");
} // namespace json
} // namespace boost