]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/doc_quick_look.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / json / test / doc_quick_look.cpp
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 #include <boost/json.hpp>
11 #include <boost/container/pmr/vector.hpp>
12 #include <iostream>
13
14 #include "test_suite.hpp"
15
16 BOOST_JSON_NS_BEGIN
17
18 static void set1() {
19
20 //----------------------------------------------------------
21 {
22 //[doc_quick_look_1
23 object obj; // construct an empty object
24 obj[ "pi" ] = 3.141; // insert a double
25 obj[ "happy" ] = true; // insert a bool
26 obj[ "name" ] = "Boost"; // insert a string
27 obj[ "nothing" ] = nullptr; // insert a null
28 obj[ "answer" ].emplace_object()["everything"] = 42; // insert an object with 1 element
29 obj[ "list" ] = { 1, 0, 2 }; // insert an array with 3 elements
30 obj[ "object" ] = { {"currency", "USD"}, {"value", 42.99} }; // insert an object with 2 elements
31 //]
32 }
33 //----------------------------------------------------------
34 {
35 //[doc_quick_look_2
36 value jv = {
37 { "pi", 3.141 },
38 { "happy", true },
39 { "name", "Boost" },
40 { "nothing", nullptr },
41 { "answer", {
42 { "everything", 42 } } },
43 {"list", {1, 0, 2}},
44 {"object", {
45 { "currency", "USD" },
46 { "value", 42.99 }
47 } }
48 };
49 //]
50 }
51 //----------------------------------------------------------
52 {
53 //[doc_quick_look_3
54 array arr; // construct an empty array
55 arr = { 1, 2, 3 }; // replace the contents with 3 elements
56 value jv1( arr ); // this makes a copy of the array
57 value jv2( std::move(arr) ); // this performs a move-construction
58
59 assert( arr.empty() ); // moved-from arrays become empty
60 arr = { nullptr, true, "boost" }; // fill in the array again
61 //]
62 }
63 //----------------------------------------------------------
64 {
65 //[doc_quick_look_4
66 {
67 unsigned char buf[ 4096 ]; // storage for our array
68 static_resource mr( buf ); // memory resource which uses buf
69 array arr( &mr ); // construct using the memory resource
70 arr = { 1, 2, 3 }; // all allocated memory comes from `buf`
71 }
72 //]
73 }
74 //----------------------------------------------------------
75 {
76 //[doc_quick_look_5
77 {
78 monotonic_resource mr; // memory resource optimized for insertion
79 array arr( &mr ); // construct using the memory resource
80 arr.resize( 1 ); // make space for one element
81 arr[ 0 ] = { 1, 2, 3 }; // assign an array to element 0
82 assert( *arr[0].storage() == *arr.storage() ); // same memory resource
83 }
84 //]
85 }
86 //----------------------------------------------------------
87 {
88 //[doc_quick_look_6
89 {
90 monotonic_resource mr;
91 boost::container::pmr::vector< value > vv( &mr );
92 vv.resize( 3 );
93
94 // The memory resource of the container is propagated to each element
95 assert( *vv.get_allocator().resource() == *vv[0].storage() );
96 assert( *vv.get_allocator().resource() == *vv[1].storage() );
97 assert( *vv.get_allocator().resource() == *vv[2].storage() );
98 }
99 //]
100 }
101 //----------------------------------------------------------
102
103 } // set1()
104
105 //----------------------------------------------------------
106
107 //[doc_quick_look_7
108 value f()
109 {
110 // create a reference-counted memory resource
111 storage_ptr sp = make_shared_resource< monotonic_resource >();
112
113 // construct with shared ownership of the resource
114 value jv( sp );
115
116 // assign an array with 3 elements, the monotonic resource will be used
117 jv = { 1, 2, 3 };
118
119 // The caller receives the value, which still owns the resource
120 return jv;
121 }
122 //]
123 //----------------------------------------------------------
124
125 static void set2() {
126
127 //----------------------------------------------------------
128 {
129 //[doc_quick_look_8
130 value jv = parse( "[1, 2, 3]" );
131 //]
132 }
133 //----------------------------------------------------------
134 {
135 //[doc_quick_look_9
136 error_code ec;
137 value jv = parse( R"( "Hello, world!" )", ec );
138 //]
139 }
140 //----------------------------------------------------------
141 {
142 //[doc_quick_look_10
143 unsigned char buf[ 4096 ];
144 static_resource mr( buf );
145 parse_options opt;
146 opt.allow_comments = true;
147 opt.allow_trailing_commas = true;
148 value jv = parse( "[1, 2, 3, ] // array ", &mr, opt );
149 //]
150 }
151 //----------------------------------------------------------
152 {
153 //[doc_quick_look_11
154 stream_parser p;
155 error_code ec;
156 p.reset();
157 p.write( "[1, 2 ", ec );
158 if( ! ec )
159 p.write( ", 3]", ec );
160 if( ! ec )
161 p.finish( ec );
162 if( ec )
163 return;
164 value jv = p.release();
165 //]
166 }
167 //----------------------------------------------------------
168 {
169 //[doc_quick_look_12
170 value jv = { 1, 2, 3 };
171 std::string s = serialize( jv ); // produces "[1,2,3]"
172 //]
173 }
174 //----------------------------------------------------------
175 {
176 value jv;
177 //[doc_quick_look_13
178 serializer sr;
179 sr.reset( &jv ); // prepare to output `jv`
180 do
181 {
182 char buf[ 16 ];
183 std::cout << sr.read( buf );
184 }
185 while( ! sr.done() );
186 //]
187 }
188 //----------------------------------------------------------
189
190 } // set2()
191
192 //----------------------------------------------------------
193
194 //[doc_quick_look_14
195 namespace my_app {
196
197 struct customer
198 {
199 int id;
200 std::string name;
201 bool current;
202 };
203
204 } // namespace my_app
205 //]
206
207 //----------------------------------------------------------
208
209 //[doc_quick_look_15
210 namespace my_app {
211
212 void tag_invoke( value_from_tag, value& jv, customer const& c )
213 {
214 jv = {
215 { "id" , c.id },
216 { "name", c.name },
217 { "current", c.current }
218 };
219 }
220
221 } // namespace my_app
222 //]
223
224 static void set3() {
225
226 //----------------------------------------------------------
227 {
228 //[doc_quick_look_16
229 my_app::customer c{ 1001, "Boost", true };
230 std::cout << serialize( value_from( c ) );
231 //]
232 }
233 //----------------------------------------------------------
234 {
235 //[doc_quick_look_17
236 std::vector< my_app::customer > vc;
237 //...
238 value jv = value_from( vc );
239 //]
240 }
241 //----------------------------------------------------------
242
243 } // set3()
244
245 //----------------------------------------------------------
246
247 //[doc_quick_look_18
248 namespace my_app {
249
250 // This helper function deduces the type and assigns the value with the matching key
251 template<class T>
252 void extract( object const& obj, T& t, string_view key )
253 {
254 t = value_to<T>( obj.at( key ) );
255 }
256
257 customer tag_invoke( value_to_tag< customer >, value const& jv )
258 {
259 customer c;
260 object const& obj = jv.as_object();
261 extract( obj, c.id, "id" );
262 extract( obj, c.name, "name" );
263 extract( obj, c.current, "current" );
264 return c;
265 }
266
267 } // namespace my_app
268 //]
269
270 //----------------------------------------------------------
271
272 namespace my_app_2 {
273 namespace my_app { using boost::json::my_app::customer; }
274 //[doc_quick_look_19
275 namespace my_app {
276
277 customer tag_invoke( value_to_tag< customer >, value const& jv )
278 {
279 object const& obj = jv.as_object();
280 return customer {
281 value_to<int>( obj.at( "id" ) ),
282 value_to<std::string>( obj.at( "name" ) ),
283 value_to<bool>( obj.at( "current" ) )
284 };
285 }
286
287 } // namespace my_app
288 //]
289 } // my_app_2
290
291 //----------------------------------------------------------
292
293 static void set4() {
294 using namespace my_app;
295
296 //----------------------------------------------------------
297 {
298 //[doc_quick_look_20
299 json::value jv;
300 //...
301 customer c( value_to<customer>(jv) );
302 //]
303 }
304 //----------------------------------------------------------
305 {
306 json::value jv;
307 //[doc_quick_look_21
308 std::vector< customer > vc = value_to< std::vector< customer > >( jv );
309 //]
310 }
311 //----------------------------------------------------------
312
313 //----------------------------------------------------------
314
315 } // set4
316
317 class doc_quick_look_test
318 {
319 public:
320 void
321 run()
322 {
323 (void)&set1;
324 (void)&set2;
325 (void)&set3;
326 (void)&set4;
327 BOOST_TEST_PASS();
328 }
329 };
330
331 TEST_SUITE(doc_quick_look_test, "boost.json.doc_quick_look");
332
333 BOOST_JSON_NS_END