]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/doc_quick_look.cpp
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
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)
7 // Official repository: https://github.com/boostorg/json
10 #include <boost/json.hpp>
11 #include <boost/container/pmr/vector.hpp>
14 #include "test_suite.hpp"
20 //----------------------------------------------------------
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
33 //----------------------------------------------------------
40 { "nothing", nullptr },
42 { "everything", 42 } } },
45 { "currency", "USD" },
51 //----------------------------------------------------------
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
59 assert( arr
.empty() ); // moved-from arrays become empty
60 arr
= { nullptr, true, "boost" }; // fill in the array again
63 //----------------------------------------------------------
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`
74 //----------------------------------------------------------
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
86 //----------------------------------------------------------
90 monotonic_resource mr
;
91 boost::container::pmr::vector
< value
> vv( &mr
);
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() );
101 //----------------------------------------------------------
105 //----------------------------------------------------------
110 // create a reference-counted memory resource
111 storage_ptr sp
= make_shared_resource
< monotonic_resource
>();
113 // construct with shared ownership of the resource
116 // assign an array with 3 elements, the monotonic resource will be used
119 // The caller receives the value, which still owns the resource
123 //----------------------------------------------------------
127 //----------------------------------------------------------
130 value jv
= parse( "[1, 2, 3]" );
133 //----------------------------------------------------------
137 value jv
= parse( R
"( "Hello
, world
!" )", ec
);
140 //----------------------------------------------------------
143 unsigned char buf
[ 4096 ];
144 static_resource
mr( buf
);
146 opt
.allow_comments
= true;
147 opt
.allow_trailing_commas
= true;
148 value jv
= parse( "[1, 2, 3, ] // array ", &mr
, opt
);
151 //----------------------------------------------------------
157 p
.write( "[1, 2 ", ec
);
159 p
.write( ", 3]", ec
);
164 value jv
= p
.release();
167 //----------------------------------------------------------
170 value jv
= { 1, 2, 3 };
171 std::string s
= serialize( jv
); // produces "[1,2,3]"
174 //----------------------------------------------------------
179 sr
.reset( &jv
); // prepare to output `jv`
183 std::cout
<< sr
.read( buf
);
185 while( ! sr
.done() );
188 //----------------------------------------------------------
192 //----------------------------------------------------------
204 } // namespace my_app
207 //----------------------------------------------------------
212 void tag_invoke( value_from_tag
, value
& jv
, customer
const& c
)
217 { "current", c
.current
}
221 } // namespace my_app
226 //----------------------------------------------------------
229 my_app::customer c
{ 1001, "Boost", true };
230 std::cout
<< serialize( value_from( c
) );
233 //----------------------------------------------------------
236 std::vector
< my_app::customer
> vc
;
238 value jv
= value_from( vc
);
241 //----------------------------------------------------------
245 //----------------------------------------------------------
250 // This helper function deduces the type and assigns the value with the matching key
252 void extract( object
const& obj
, T
& t
, string_view key
)
254 t
= value_to
<T
>( obj
.at( key
) );
257 customer
tag_invoke( value_to_tag
< customer
>, value
const& jv
)
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" );
267 } // namespace my_app
270 //----------------------------------------------------------
273 namespace my_app
{ using boost::json::my_app::customer
; }
277 customer
tag_invoke( value_to_tag
< customer
>, value
const& jv
)
279 object
const& obj
= jv
.as_object();
281 value_to
<int>( obj
.at( "id" ) ),
282 value_to
<std::string
>( obj
.at( "name" ) ),
283 value_to
<bool>( obj
.at( "current" ) )
287 } // namespace my_app
291 //----------------------------------------------------------
294 using namespace my_app
;
296 //----------------------------------------------------------
301 customer
c( value_to
<customer
>(jv
) );
304 //----------------------------------------------------------
308 std::vector
< customer
> vc
= value_to
< std::vector
< customer
> >( jv
);
311 //----------------------------------------------------------
313 //----------------------------------------------------------
317 class doc_quick_look_test
331 TEST_SUITE(doc_quick_look_test
, "boost.json.doc_quick_look");