]> git.proxmox.com Git - ceph.git/blame - 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
CommitLineData
20effc67
TL
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>
20effc67 11#include <boost/container/pmr/vector.hpp>
20effc67
TL
12#include <iostream>
13
14#include "test_suite.hpp"
15
16BOOST_JSON_NS_BEGIN
17
18static void set1() {
19
20//----------------------------------------------------------
21{
22//[doc_quick_look_1
23object obj; // construct an empty object
24obj[ "pi" ] = 3.141; // insert a double
25obj[ "happy" ] = true; // insert a bool
26obj[ "name" ] = "Boost"; // insert a string
27obj[ "nothing" ] = nullptr; // insert a null
28obj[ "answer" ].emplace_object()["everything"] = 42; // insert an object with 1 element
29obj[ "list" ] = { 1, 0, 2 }; // insert an array with 3 elements
30obj[ "object" ] = { {"currency", "USD"}, {"value", 42.99} }; // insert an object with 2 elements
31//]
32}
33//----------------------------------------------------------
34{
35//[doc_quick_look_2
36value 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
54array arr; // construct an empty array
55arr = { 1, 2, 3 }; // replace the contents with 3 elements
56value jv1( arr ); // this makes a copy of the array
57value jv2( std::move(arr) ); // this performs a move-construction
58
59assert( arr.empty() ); // moved-from arrays become empty
60arr = { 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{
20effc67
TL
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//]
20effc67
TL
100}
101//----------------------------------------------------------
102
103} // set1()
104
105//----------------------------------------------------------
106
107//[doc_quick_look_7
108value 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
125static void set2() {
126
127//----------------------------------------------------------
128{
129//[doc_quick_look_8
130value jv = parse( "[1, 2, 3]" );
131//]
132}
133//----------------------------------------------------------
134{
135//[doc_quick_look_9
136error_code ec;
137value jv = parse( R"( "Hello, world!" )", ec );
138//]
139}
140//----------------------------------------------------------
141{
142//[doc_quick_look_10
143unsigned char buf[ 4096 ];
144static_resource mr( buf );
145parse_options opt;
146opt.allow_comments = true;
147opt.allow_trailing_commas = true;
148value jv = parse( "[1, 2, 3, ] // array ", &mr, opt );
149//]
150}
151//----------------------------------------------------------
152{
153//[doc_quick_look_11
154stream_parser p;
155error_code ec;
156p.reset();
157p.write( "[1, 2 ", ec );
158if( ! ec )
159 p.write( ", 3]", ec );
160if( ! ec )
161 p.finish( ec );
162if( ec )
163 return;
164value jv = p.release();
165//]
166}
167//----------------------------------------------------------
168{
169//[doc_quick_look_12
170value jv = { 1, 2, 3 };
171std::string s = serialize( jv ); // produces "[1,2,3]"
172//]
173}
174//----------------------------------------------------------
175{
176value jv;
177//[doc_quick_look_13
178serializer sr;
179sr.reset( &jv ); // prepare to output `jv`
180do
181{
182 char buf[ 16 ];
183 std::cout << sr.read( buf );
184}
185while( ! sr.done() );
186//]
187}
188//----------------------------------------------------------
189
190} // set2()
191
192//----------------------------------------------------------
193
194//[doc_quick_look_14
195namespace my_app {
196
197struct 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
210namespace my_app {
211
212void 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
224static void set3() {
225
226//----------------------------------------------------------
227{
228//[doc_quick_look_16
229my_app::customer c{ 1001, "Boost", true };
230std::cout << serialize( value_from( c ) );
231//]
232}
233//----------------------------------------------------------
234{
235//[doc_quick_look_17
236std::vector< my_app::customer > vc;
237//...
238value jv = value_from( vc );
239//]
240}
241//----------------------------------------------------------
242
243} // set3()
244
245//----------------------------------------------------------
246
247//[doc_quick_look_18
248namespace my_app {
249
250// This helper function deduces the type and assigns the value with the matching key
251template<class T>
252void extract( object const& obj, T& t, string_view key )
253{
254 t = value_to<T>( obj.at( key ) );
255}
256
257customer 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
272namespace my_app_2 {
273namespace my_app { using boost::json::my_app::customer; }
274//[doc_quick_look_19
275namespace my_app {
276
277customer 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
293static void set4() {
294using namespace my_app;
295
296//----------------------------------------------------------
297{
298//[doc_quick_look_20
299json::value jv;
300//...
301customer c( value_to<customer>(jv) );
302//]
303}
304//----------------------------------------------------------
305{
306json::value jv;
307//[doc_quick_look_21
308std::vector< customer > vc = value_to< std::vector< customer > >( jv );
309//]
310}
311//----------------------------------------------------------
312
313//----------------------------------------------------------
314
315} // set4
316
317class doc_quick_look_test
318{
319public:
320 void
321 run()
322 {
323 (void)&set1;
324 (void)&set2;
325 (void)&set3;
326 (void)&set4;
327 BOOST_TEST_PASS();
328 }
329};
330
331TEST_SUITE(doc_quick_look_test, "boost.json.doc_quick_look");
332
333BOOST_JSON_NS_END