File: doc_parsing.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (340 lines) | stat: -rw-r--r-- 10,310 bytes parent folder | download | duplicates (3)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@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
//

#include <boost/json/monotonic_resource.hpp>
#include <boost/json/null_resource.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/parser.hpp>
#include <boost/json/static_resource.hpp>
#include <boost/json/stream_parser.hpp>

#include <iostream>
#include <string>

#include "test_suite.hpp"

namespace boost {
namespace json {

//----------------------------------------------------------

static void set1() {

//----------------------------------------------------------
{
// tag::doc_parsing_1[]
value jv = parse( "[1,2,3,4,5]" );
// end::doc_parsing_1[]
}
//----------------------------------------------------------
{
// tag::doc_parsing_2[]
boost::system::error_code ec;
value jv = parse( "[1,2,3,4,5]", ec );
if( ec )
    std::cout << "Parsing failed: " << ec.message() << "\n";
// end::doc_parsing_2[]
}
//----------------------------------------------------------
{
// tag::doc_parsing_3[]
try
{
    boost::system::error_code ec;
    value jv = parse( "[1,2,3,4,5]", ec );
    if( ec )
        std::cout << "Parsing failed: " << ec.message() << "\n";
}
catch( std::bad_alloc const& e)
{
    std::cout << "Parsing failed: " << e.what() << "\n";
}
// end::doc_parsing_3[]
}
//----------------------------------------------------------
{
// tag::doc_parsing_4[]
    monotonic_resource mr;
    value const jv = parse( "[1,2,3,4,5]", &mr );
// end::doc_parsing_4[]
}
//----------------------------------------------------------
{
// tag::doc_parsing_5[]
parse_options opt;                // all extensions default to off
opt.allow_comments = true;        // permit C and C++ style comments
                                  // to appear in whitespace
opt.allow_trailing_commas = true; // allow an additional trailing comma in
                                  // object and array element lists
opt.allow_invalid_utf8 = true;    // skip utf-8 validation of keys and strings
opt.allow_invalid_utf16 = true;   // replace invalid surrogate pair UTF-16 code point(s)
                                  // with the Unicode replacement character

value jv = parse( "[1,2,3,] // comment ", storage_ptr(), opt );
// end::doc_parsing_5[]
}
//----------------------------------------------------------
{
#if __cpp_designated_initializers >= 201707L
{
// tag::doc_parsing_6[]
value jv = parse(
    "[1,2,3,] // comment ",
    storage_ptr(),
    {
        .allow_comments = true,             // permit C and C++ style comments
                                            // to appear in whitespace
        .allow_trailing_commas = true,      // allow a trailing comma in object and array lists
        .allow_invalid_utf8 = true          // skip utf-8 validation of keys and strings
    });
// end::doc_parsing_6[]
}
{
// tag::doc_parsing_15[]
value jv = parse( "{\"command\":\"\\uDF3E\\uDEC2\"}", storage_ptr(),
    {
        .allow_invalid_utf16 = true       // replace illegal leading surrogate pair with ��
    });
// end::doc_parsing_15[]
}
#endif
}
//----------------------------------------------------------

} // set1

//----------------------------------------------------------
// tag::doc_parsing_7[]
class connection
{
    parser p_;                    // persistent data member

public:
    void do_read( string_view s ) // called for each complete message from the network
    {
        p_.reset();               // start parsing a new JSON using the default resource
        p_.write( s );            // parse the buffer, using exceptions to indicate error
        do_rpc( p_.release() );   // process the command
    }

    void do_rpc( value jv );
};
// end::doc_parsing_7[]

//----------------------------------------------------------

static void set2() {

//----------------------------------------------------------
{
// tag::doc_parsing_8[]
stream_parser p;
boost::system::error_code ec;
string_view s = "[1,2,3] %HOME%";
std::size_t n = p.write_some( s, ec );
assert( ! ec && p.done() && n == 8 );
s = s.substr( n );
value jv = p.release();
assert( s == "%HOME%" );
// end::doc_parsing_8[]
}
//----------------------------------------------------------
{
// tag::doc_parsing_9[]
parse_options opt;                     // All extensions default to off
opt.allow_comments = true;             // Permit C and C++ style comments to appear in whitespace
opt.allow_trailing_commas = true;      // Allow an additional trailing comma in
                                       // object and array element lists
opt.allow_invalid_utf8 = true;         // Skip utf-8 validation of keys and strings
stream_parser p( storage_ptr(), opt ); // The stream_parser will use the options
// end::doc_parsing_9[]
}
//----------------------------------------------------------

} // set2

//----------------------------------------------------------
// tag::doc_parsing_10[]
value read_json( std::istream& is, boost::system::error_code& ec )
{
    stream_parser p;
    std::string line;
    while( std::getline( is, line ) )
    {
        p.write( line, ec );
        if( ec )
            return nullptr;
    }
    p.finish( ec );
    if( ec )
        return nullptr;
    return p.release();
}
// end::doc_parsing_10[]

// tag::doc_parsing_14[]
std::vector<value> read_jsons( std::istream& is, boost::system::error_code& ec )
{
    std::vector< value > jvs;
    stream_parser p;
    std::string line;
    std::size_t n = 0;
    while( true )
    {
        if( n == line.size() )
        {
            if( !std::getline( is, line ) )
                break;
            n = 0;
        }

        n += p.write_some( line.data() + n, line.size() - n, ec );

        if( p.done() )
        {
            jvs.push_back( p.release() );
            p.reset();
        }
    }
    if( !p.done() )   // this part handles the cases when the last JSON text in
    {                 // the input is either incomplete or doesn't have a marker
        p.finish(ec); // for end of the value (e.g. it is a number)
        if( ec.failed() )
            return jvs;
        jvs.push_back( p.release() );
    }

    return jvs;
}
// end::doc_parsing_14[]

//----------------------------------------------------------

static void set3() {

//----------------------------------------------------------
{
// tag::doc_parsing_11[]
    monotonic_resource mr;

    stream_parser p;
    p.reset( &mr );                // Use mr for the resulting value
    p.write( "[1,2,3,4,5]" );      // Parse the input JSON
    value const jv = p.release();  // Retrieve the result
    assert( *jv.storage() == mr ); // Same memory resource
// end::doc_parsing_11[]
}
//----------------------------------------------------------
{
// tag::doc_parsing_12[]
unsigned char temp[ 4096 ]; // Declare our buffer
stream_parser p(
    storage_ptr(),          // Default memory resource
    parse_options{},        // Default parse options (strict parsing)
    temp);                  // Use our buffer for temporary storage
// end::doc_parsing_12[]
}
//----------------------------------------------------------

} // set3

//----------------------------------------------------------

// tag::doc_parsing_13[]
/*  Parse JSON and invoke the handler

    This function parses the JSON specified in `s`
    and invokes the handler, whose signature must
    be equivalent to:

        void( value const& jv );

    The operation is guaranteed not to perform any
    dynamic memory allocations. However, some
    implementation-defined upper limits on the size
    of the input JSON and the size of the resulting
    value are imposed.

    Upon error, an exception is thrown.
*/
template< class Handler >
void do_rpc( string_view s, Handler&& handler )
{
    unsigned char temp[ 4096 ]; // The parser will use this storage for its temporary needs
    parser p(                   // Construct a strict parser using
                                // the temp buffer and no dynamic memory
        get_null_resource(),    // The null resource never dynamically allocates memory
        parse_options(),        // Default constructed parse options allow only standard JSON
        temp );

    unsigned char buf[ 16384 ]; // Now we need a buffer to hold the actual JSON values
    static_resource mr2( buf ); // The static resource is monotonic,
                                // using only a caller-provided buffer
    p.reset( &mr2 );            // Use the static resource for producing the value
    p.write( s );               // Parse the entire string we received from the network client

    // Retrieve the value and invoke the handler with it.
    // The value will use `buf` for storage. The handler
    // must not take ownership, since monotonic resources
    // are inefficient with mutation.
    handler( p.release() );
}
// end::doc_parsing_13[]

//----------------------------------------------------------

void
testPrecise()
{
    // tag::doc_parsing_precise[]
    parse_options opt;
    opt.numbers = number_precision::precise;
    value jv = parse( "1002.9111801605201", storage_ptr(), opt );
    // end::doc_parsing_precise[]
    (void)jv;
    assert( jv == 1002.9111801605201 );
}

//----------------------------------------------------------

class doc_parsing_test
{
public:
    void
    run()
    {
        (void)&set1;
        (void)&set2;
        (void)&set3;
        {
            std::stringstream ss( "[1,2,3\n"
                                  ",4]nul\n"
                                  "l12345\n"
                                  "6\"!\n"
                                  "\"[2]3" );
            system::error_code ec;
            auto jvs = read_jsons( ss, ec );
            assert( !ec.failed() );
            assert( jvs.size() == 6 );
            assert(( jvs[0] == array{ 1, 2, 3, 4} ));
            assert(( jvs[1] == value() ));
            assert(( jvs[2] == 123456 ));
            assert(( jvs[3] == "!" ));
            assert(( jvs[4] == array{2} ));
            assert(( jvs[5] == 3 ));
        }

        testPrecise();
    }
};

TEST_SUITE(doc_parsing_test, "boost.json.doc_parsing");

} // namespace json
} // namespace boost