]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/doc_parsing.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/monotonic_resource.hpp>
11 #include <boost/json/null_resource.hpp>
12 #include <boost/json/parse.hpp>
13 #include <boost/json/parser.hpp>
14 #include <boost/json/static_resource.hpp>
15 #include <boost/json/stream_parser.hpp>
20 #include "test_suite.hpp"
24 //----------------------------------------------------------
28 //----------------------------------------------------------
31 value jv
= parse( "[1,2,3,4,5]" );
34 //----------------------------------------------------------
38 value jv
= parse( "[1,2,3,4,5]", ec
);
40 std::cout
<< "Parsing failed: " << ec
.message() << "\n";
43 //----------------------------------------------------------
49 value jv
= parse( "[1,2,3,4,5]", ec
);
51 std::cout
<< "Parsing failed: " << ec
.message() << "\n";
53 catch( std::bad_alloc
const& e
)
55 std::cout
<< "Parsing failed: " << e
.what() << "\n";
59 //----------------------------------------------------------
63 monotonic_resource mr
;
65 value
const jv
= parse( "[1,2,3,4,5]", &mr
);
69 //----------------------------------------------------------
72 parse_options opt
; // all extensions default to off
73 opt
.allow_comments
= true; // permit C and C++ style comments to appear in whitespace
74 opt
.allow_trailing_commas
= true; // allow an additional trailing comma in object and array element lists
75 opt
.allow_invalid_utf8
= true; // skip utf-8 validation of keys and strings
77 value jv
= parse( "[1,2,3,] // comment ", storage_ptr(), opt
);
80 //----------------------------------------------------------
82 #if __cpp_designated_initializers >= 201707L
84 value jv
= parse( "[1,2,3,] // comment ", storage_ptr(),
86 .allow_comments
= true, // permit C and C++ style comments to appear in whitespace
87 .allow_trailing_commas
= true, // allow a trailing comma in object and array lists
88 .allow_invalid_utf8
= true // skip utf-8 validation of keys and strings
93 //----------------------------------------------------------
97 //----------------------------------------------------------
101 parser p_
; // persistent data member
104 void do_read( string_view s
) // called for each complete message from the network
106 p_
.reset(); // start parsing a new JSON using the default resource
107 p_
.write( s
); // parse the buffer, using exceptions to indicate error
108 do_rpc( p_
.release() ); // process the command
111 void do_rpc( value jv
);
115 //----------------------------------------------------------
119 //----------------------------------------------------------
124 string_view s
= "[1,2,3] %HOME%";
125 std::size_t n
= p
.write_some( s
, ec
);
126 assert( ! ec
&& p
.done() && n
== 8 );
128 value jv
= p
.release();
129 assert( s
== "%HOME%" );
132 //----------------------------------------------------------
135 parse_options opt
; // All extensions default to off
136 opt
.allow_comments
= true; // Permit C and C++ style comments to appear in whitespace
137 opt
.allow_trailing_commas
= true; // Allow an additional trailing comma in object and array element lists
138 opt
.allow_invalid_utf8
= true; // Skip utf-8 validation of keys and strings
139 stream_parser
p( storage_ptr(), opt
); // The stream_parser will use the options
142 //----------------------------------------------------------
146 //----------------------------------------------------------
148 value
read_json( std::istream
& is
, error_code
& ec
)
152 while( std::getline( is
, line
) )
165 //----------------------------------------------------------
169 //----------------------------------------------------------
173 monotonic_resource mr
;
176 p
.reset( &mr
); // Use mr for the resulting value
177 p
.write( "[1,2,3,4,5]" ); // Parse the input JSON
178 value
const jv
= p
.release(); // Retrieve the result
179 assert( *jv
.storage() == mr
); // Same memory resource
183 //----------------------------------------------------------
186 unsigned char temp
[ 4096 ]; // Declare our buffer
188 storage_ptr(), // Default memory resource
189 parse_options
{}, // Default parse options (strict parsing)
190 temp
); // Use our buffer for temporary storage
193 //----------------------------------------------------------
197 //----------------------------------------------------------
200 /* Parse JSON and invoke the handler
202 This function parses the JSON specified in `s`
203 and invokes the handler, whose signature must
206 void( value const& jv );
208 The operation is guaranteed not to perform any
209 dynamic memory allocations. However, some
210 implementation-defined upper limits on the size
211 of the input JSON and the size of the resulting
214 Upon error, an exception is thrown.
216 template< class Handler
>
217 void do_rpc( string_view s
, Handler
&& handler
)
219 unsigned char temp
[ 4096 ]; // The parser will use this storage for its temporary needs
220 parser
p( // Construct a strict parser using the temp buffer and no dynamic memory
221 get_null_resource(), // The null resource never dynamically allocates memory
222 parse_options(), // Default constructed parse options allow only standard JSON
225 unsigned char buf
[ 16384 ]; // Now we need a buffer to hold the actual JSON values
226 static_resource
mr2( buf
); // The static resource is monotonic, using only a caller-provided buffer
227 p
.reset( &mr2
); // Use the static resource for producing the value
228 p
.write( s
); // Parse the entire string we received from the network client
230 // Retrieve the value and invoke the handler with it.
231 // The value will use `buf` for storage. The handler
232 // must not take ownership, since monotonic resources
233 // are inefficient with mutation.
234 handler( p
.release() );
238 //----------------------------------------------------------
240 class doc_parsing_test
252 TEST_SUITE(doc_parsing_test
, "boost.json.doc_parsing");