]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/json/stream_parser.hpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / boost / json / stream_parser.hpp
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 #ifndef BOOST_JSON_STREAM_PARSER_HPP
11 #define BOOST_JSON_STREAM_PARSER_HPP
12
13 #include <boost/json/detail/config.hpp>
14 #include <boost/json/basic_parser.hpp>
15 #include <boost/json/parse_options.hpp>
16 #include <boost/json/storage_ptr.hpp>
17 #include <boost/json/value.hpp>
18 #include <boost/json/detail/handler.hpp>
19 #include <type_traits>
20 #include <cstddef>
21
22 BOOST_JSON_NS_BEGIN
23
24 //----------------------------------------------------------
25
26 /** A DOM parser for JSON contained in multiple buffers.
27
28 This class is used to parse a JSON contained in a
29 series of one or more character buffers, into a
30 @ref value container. It implements a
31 <a href="https://en.wikipedia.org/wiki/Streaming_algorithm">
32 <em>streaming algorithm</em></a>, allowing these
33 parsing strategies:
34
35 @li Parse a JSON file a piece at a time.
36
37 @li Parse incoming JSON as it arrives,
38 one buffer at a time.
39
40 @li Parse with bounded resource consumption
41 per cycle.
42
43 @par Usage
44
45 To use the parser first construct it, then optionally
46 call @ref reset to specify a @ref storage_ptr to use
47 for the resulting @ref value. Then call @ref write
48 one or more times to parse a single, complete JSON.
49 Call @ref done to determine if the parse has completed.
50 To indicate there are no more buffers, call @ref finish.
51 If the parse is successful, call @ref release to take
52 ownership of the value:
53
54 @code
55 stream_parser p; // construct a parser
56 p.write( "[1,2" ); // parse some of a JSON
57 p.write( ",3,4]" ); // parse the rest of the JSON
58 assert( p.done() ); // we have a complete JSON
59 value jv = p.release(); // take ownership of the value
60 @endcode
61
62 @par Extra Data
63
64 When the character buffer provided as input contains
65 additional data that is not part of the complete
66 JSON, an error is returned. The @ref write_some
67 function is an alternative which allows the parse
68 to finish early, without consuming all the characters
69 in the buffer. This allows parsing of a buffer
70 containing multiple individual JSONs or containing
71 different protocol data:
72 @code
73 stream_parser p; // construct a parser
74 std::size_t n; // number of characters used
75 n = p.write_some( "[1,2" ); // parse some of a JSON
76 assert( n == 4 ); // all characters consumed
77 n = p.write_some( ",3,4] null" ); // parse the remainder of the JSON
78 assert( n == 6 ); // only some characters consumed
79 assert( p.done() ); // we have a complete JSON
80 value jv = p.release(); // take ownership of the value
81 @endcode
82
83 @par Temporary Storage
84
85 The parser may dynamically allocate temporary
86 storage as needed to accommodate the nesting level
87 of the JSON being parsed. Temporary storage is
88 first obtained from an optional, caller-owned
89 buffer specified upon construction. When that
90 is exhausted, the next allocation uses the
91 @ref memory_resource passed to the constructor; if
92 no such argument is specified, the default memory
93 resource is used. Temporary storage is freed only
94 when the parser is destroyed; The performance of
95 parsing multiple JSONs may be improved by reusing
96 the same parser instance.
97 \n
98 It is important to note that the @ref memory_resource
99 supplied upon construction is used for temporary
100 storage only, and not for allocating the elements
101 which make up the parsed value. That other memory
102 resource is optionally supplied in each call
103 to @ref reset.
104
105 @par Duplicate Keys
106
107 If there are object elements with duplicate keys;
108 that is, if multiple elements in an object have
109 keys that compare equal, only the last equivalent
110 element will be inserted.
111
112 @par Non-Standard JSON
113
114 The @ref parse_options structure optionally
115 provided upon construction is used to customize
116 some parameters of the parser, including which
117 non-standard JSON extensions should be allowed.
118 A default-constructed parse options allows only
119 standard JSON.
120
121 @par Thread Safety
122
123 Distinct instances may be accessed concurrently.
124 Non-const member functions of a shared instance
125 may not be called concurrently with any other
126 member functions of that instance.
127
128 @see
129 @ref parse,
130 @ref parser,
131 @ref parse_options,
132 */
133 class stream_parser
134 {
135 basic_parser<detail::handler> p_;
136
137 public:
138 /// Copy constructor (deleted)
139 stream_parser(
140 stream_parser const&) = delete;
141
142 /// Copy assignment (deleted)
143 stream_parser& operator=(
144 stream_parser const&) = delete;
145
146 /** Destructor.
147
148 All dynamically allocated memory, including
149 any incomplete parsing results, is freed.
150
151 @par Complexity
152 Linear in the size of partial results
153
154 @par Exception Safety
155 No-throw guarantee.
156 */
157 ~stream_parser() = default;
158
159 /** Constructor.
160
161 This constructs a new parser which first uses
162 the caller-owned storage pointed to by `buffer`
163 for temporary storage, falling back to the memory
164 resource `sp` if needed. The parser will use the
165 specified parsing options.
166 \n
167 The parsed value will use the default memory
168 resource for storage. To use a different resource,
169 call @ref reset after construction.
170
171 @par Complexity
172 Constant.
173
174 @par Exception Safety
175 No-throw guarantee.
176
177 @param sp The memory resource to use for
178 temporary storage after `buffer` is exhausted.
179
180 @param opt The parsing options to use.
181
182 @param buffer A pointer to valid memory of at least
183 `size` bytes for the parser to use for temporary storage.
184 Ownership is not transferred, the caller is responsible
185 for ensuring the lifetime of the memory pointed to by
186 `buffer` extends until the parser is destroyed.
187
188 @param size The number of valid bytes in `buffer`.
189 */
190 BOOST_JSON_DECL
191 stream_parser(
192 storage_ptr sp,
193 parse_options const& opt,
194 unsigned char* buffer,
195 std::size_t size) noexcept;
196
197 /** Constructor.
198
199 This constructs a new parser which uses the default
200 memory resource for temporary storage, and accepts
201 only strict JSON.
202 \n
203 The parsed value will use the default memory
204 resource for storage. To use a different resource,
205 call @ref reset after construction.
206
207 @par Complexity
208 Constant.
209
210 @par Exception Safety
211 No-throw guarantee.
212 */
213 stream_parser() noexcept
214 : stream_parser({}, {})
215 {
216 }
217
218 /** Constructor.
219
220 This constructs a new parser which uses the
221 specified memory resource for temporary storage,
222 and is configured to use the specified parsing
223 options.
224 \n
225 The parsed value will use the default memory
226 resource for storage. To use a different resource,
227 call @ref reset after construction.
228
229 @par Complexity
230 Constant.
231
232 @par Exception Safety
233 No-throw guarantee.
234
235 @param sp The memory resource to use for temporary storage.
236
237 @param opt The parsing options to use.
238 */
239 BOOST_JSON_DECL
240 stream_parser(
241 storage_ptr sp,
242 parse_options const& opt) noexcept;
243
244 /** Constructor.
245
246 This constructs a new parser which uses the
247 specified memory resource for temporary storage,
248 and accepts only strict JSON.
249 \n
250 The parsed value will use the default memory
251 resource for storage. To use a different resource,
252 call @ref reset after construction.
253
254 @par Complexity
255 Constant.
256
257 @par Exception Safety
258 No-throw guarantee.
259
260 @param sp The memory resource to use for temporary storage.
261 */
262 explicit
263 stream_parser(storage_ptr sp) noexcept
264 : stream_parser(std::move(sp), {})
265 {
266 }
267
268 /** Constructor.
269
270 This constructs a new parser which first uses the
271 caller-owned storage `buffer` for temporary storage,
272 falling back to the memory resource `sp` if needed.
273 The parser will use the specified parsing options.
274 \n
275 The parsed value will use the default memory
276 resource for storage. To use a different resource,
277 call @ref reset after construction.
278
279 @par Complexity
280 Constant.
281
282 @par Exception Safety
283 No-throw guarantee.
284
285 @param sp The memory resource to use for
286 temporary storage after `buffer` is exhausted.
287
288 @param opt The parsing options to use.
289
290 @param buffer A buffer for the parser to use for
291 temporary storage. Ownership is not transferred,
292 the caller is responsible for ensuring the lifetime
293 of `buffer` extends until the parser is destroyed.
294 */
295 template<std::size_t N>
296 stream_parser(
297 storage_ptr sp,
298 parse_options const& opt,
299 unsigned char(&buffer)[N]) noexcept
300 : stream_parser(std::move(sp),
301 opt, &buffer[0], N)
302 {
303 }
304
305 #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
306 /** Constructor.
307
308 This constructs a new parser which first uses
309 the caller-owned storage pointed to by `buffer`
310 for temporary storage, falling back to the memory
311 resource `sp` if needed. The parser will use the
312 specified parsing options.
313 \n
314 The parsed value will use the default memory
315 resource for storage. To use a different resource,
316 call @ref reset after construction.
317
318 @par Complexity
319 Constant.
320
321 @par Exception Safety
322 No-throw guarantee.
323
324 @param sp The memory resource to use for
325 temporary storage after `buffer` is exhausted.
326
327 @param opt The parsing options to use.
328
329 @param buffer A pointer to valid memory of at least
330 `size` bytes for the parser to use for temporary storage.
331 Ownership is not transferred, the caller is responsible
332 for ensuring the lifetime of the memory pointed to by
333 `buffer` extends until the parser is destroyed.
334
335 @param size The number of valid bytes in `buffer`.
336 */
337 stream_parser(
338 storage_ptr sp,
339 parse_options const& opt,
340 std::byte* buffer,
341 std::size_t size) noexcept
342 : stream_parser(sp, opt, reinterpret_cast<
343 unsigned char*>(buffer), size)
344 {
345 }
346
347 /** Constructor.
348
349 This constructs a new parser which first uses the
350 caller-owned storage `buffer` for temporary storage,
351 falling back to the memory resource `sp` if needed.
352 The parser will use the specified parsing options.
353 \n
354 The parsed value will use the default memory
355 resource for storage. To use a different resource,
356 call @ref reset after construction.
357
358 @par Complexity
359 Constant.
360
361 @par Exception Safety
362 No-throw guarantee.
363
364 @param sp The memory resource to use for
365 temporary storage after `buffer` is exhausted.
366
367 @param opt The parsing options to use.
368
369 @param buffer A buffer for the parser to use for
370 temporary storage. Ownership is not transferred,
371 the caller is responsible for ensuring the lifetime
372 of `buffer` extends until the parser is destroyed.
373 */
374 template<std::size_t N>
375 stream_parser(
376 storage_ptr sp,
377 parse_options const& opt,
378 std::byte(&buffer)[N]) noexcept
379 : stream_parser(std::move(sp),
380 opt, &buffer[0], N)
381 {
382 }
383 #endif
384
385 #ifndef BOOST_JSON_DOCS
386 // Safety net for accidental buffer overflows
387 template<std::size_t N>
388 stream_parser(
389 storage_ptr sp,
390 parse_options const& opt,
391 unsigned char(&buffer)[N],
392 std::size_t n) noexcept
393 : stream_parser(std::move(sp),
394 opt, &buffer[0], n)
395 {
396 // If this goes off, check your parameters
397 // closely, chances are you passed an array
398 // thinking it was a pointer.
399 BOOST_ASSERT(n <= N);
400 }
401
402 #ifdef __cpp_lib_byte
403 // Safety net for accidental buffer overflows
404 template<std::size_t N>
405 stream_parser(
406 storage_ptr sp,
407 parse_options const& opt,
408 std::byte(&buffer)[N], std::size_t n) noexcept
409 : stream_parser(std::move(sp),
410 opt, &buffer[0], n)
411 {
412 // If this goes off, check your parameters
413 // closely, chances are you passed an array
414 // thinking it was a pointer.
415 BOOST_ASSERT(n <= N);
416 }
417 #endif
418 #endif
419
420 /** Reset the parser for a new JSON.
421
422 This function is used to reset the parser to
423 prepare it for parsing a new complete JSON.
424 Any previous partial results are destroyed.
425
426 @par Complexity
427 Constant or linear in the size of any previous
428 partial parsing results.
429
430 @par Exception Safety
431 No-throw guarantee.
432
433 @param sp A pointer to the @ref memory_resource
434 to use for the resulting @ref value. The parser
435 will acquire shared ownership.
436 */
437 BOOST_JSON_DECL
438 void
439 reset(storage_ptr sp = {}) noexcept;
440
441 /** Return true if a complete JSON has been parsed.
442
443 This function returns `true` when all of these
444 conditions are met:
445
446 @li A complete serialized JSON has been
447 presented to the parser, and
448
449 @li No error has occurred since the parser
450 was constructed, or since the last call
451 to @ref reset,
452
453 @par Complexity
454 Constant.
455
456 @par Exception Safety
457 No-throw guarantee.
458 */
459 bool
460 done() const noexcept
461 {
462 return p_.done();
463 }
464
465 /** Parse a buffer containing all or part of a complete JSON.
466
467 This function parses JSON contained in the
468 specified character buffer. If parsing completes,
469 any additional characters past the end of the
470 complete JSON are ignored. The function returns the
471 actual number of characters parsed, which may be
472 less than the size of the input. This allows parsing
473 of a buffer containing multiple individual JSONs or
474 containing different protocol data.
475
476 @par Example
477 @code
478 stream_parser p; // construct a parser
479 std::size_t n; // number of characters used
480 n = p.write_some( "[1,2" ); // parse the first part of the JSON
481 assert( n == 4 ); // all characters consumed
482 n = p.write_some( "3,4] null" ); // parse the rest of the JSON
483 assert( n == 5 ); // only some characters consumed
484 value jv = p.release(); // take ownership of the value
485 @endcode
486
487 @note
488
489 To indicate there are no more character buffers,
490 such as when @ref done returns `false` after
491 writing, call @ref finish.
492
493 @par Complexity
494 Linear in `size`.
495
496 @par Exception Safety
497 Basic guarantee.
498 Calls to `memory_resource::allocate` may throw.
499 Upon error or exception, subsequent calls will
500 fail until @ref reset is called to parse a new JSON.
501
502 @return The number of characters consumed from
503 the buffer.
504
505 @param data A pointer to a buffer of `size`
506 characters to parse.
507
508 @param size The number of characters pointed to
509 by `data`.
510
511 @param ec Set to the error, if any occurred.
512 */
513 /** @{ */
514 BOOST_JSON_DECL
515 std::size_t
516 write_some(
517 char const* data,
518 std::size_t size,
519 error_code& ec);
520
521 BOOST_JSON_DECL
522 std::size_t
523 write_some(
524 char const* data,
525 std::size_t size,
526 std::error_code& ec);
527 /** @} */
528
529 /** Parse a buffer containing all or part of a complete JSON.
530
531 This function parses JSON contained in the
532 specified character buffer. If parsing completes,
533 any additional characters past the end of the
534 complete JSON are ignored. The function returns the
535 actual number of characters parsed, which may be
536 less than the size of the input. This allows parsing
537 of a buffer containing multiple individual JSONs or
538 containing different protocol data.
539
540 @par Example
541 @code
542 stream_parser p; // construct a parser
543 std::size_t n; // number of characters used
544 n = p.write_some( "[1,2" ); // parse the first part of the JSON
545 assert( n == 4 ); // all characters consumed
546 n = p.write_some( "3,4] null" ); // parse the rest of the JSON
547 assert( n == 5 ); // only some characters consumed
548 value jv = p.release(); // take ownership of the value
549 @endcode
550
551 @note
552
553 To indicate there are no more character buffers,
554 such as when @ref done returns `false` after
555 writing, call @ref finish.
556
557 @par Complexity
558 Linear in `size`.
559
560 @par Exception Safety
561 Basic guarantee.
562 Calls to `memory_resource::allocate` may throw.
563 Upon error or exception, subsequent calls will
564 fail until @ref reset is called to parse a new JSON.
565
566 @return The number of characters consumed from
567 the buffer.
568
569 @param data A pointer to a buffer of `size`
570 characters to parse.
571
572 @param size The number of characters pointed to
573 by `data`.
574
575 @throw system_error Thrown on error.
576 */
577 BOOST_JSON_DECL
578 std::size_t
579 write_some(
580 char const* data,
581 std::size_t size);
582
583 /** Parse a buffer containing all or part of a complete JSON.
584
585 This function parses JSON contained in the
586 specified character buffer. If parsing completes,
587 any additional characters past the end of the
588 complete JSON are ignored. The function returns the
589 actual number of characters parsed, which may be
590 less than the size of the input. This allows parsing
591 of a buffer containing multiple individual JSONs or
592 containing different protocol data.
593
594 @par Example
595 @code
596 stream_parser p; // construct a parser
597 std::size_t n; // number of characters used
598 n = p.write_some( "[1,2" ); // parse the first part of the JSON
599 assert( n == 4 ); // all characters consumed
600 n = p.write_some( "3,4] null" ); // parse the rest of the JSON
601 assert( n == 5 ); // only some characters consumed
602 value jv = p.release(); // take ownership of the value
603 @endcode
604
605 @note
606
607 To indicate there are no more character buffers,
608 such as when @ref done returns `false` after
609 writing, call @ref finish.
610
611 @par Complexity
612 Linear in `size`.
613
614 @par Exception Safety
615 Basic guarantee.
616 Calls to `memory_resource::allocate` may throw.
617 Upon error or exception, subsequent calls will
618 fail until @ref reset is called to parse a new JSON.
619
620 @return The number of characters consumed from
621 the buffer.
622
623 @param s The character string to parse.
624
625 @param ec Set to the error, if any occurred.
626 */
627 /** @{ */
628 std::size_t
629 write_some(
630 string_view s,
631 error_code& ec)
632 {
633 return write_some(
634 s.data(), s.size(), ec);
635 }
636
637 std::size_t
638 write_some(
639 string_view s,
640 std::error_code& ec)
641 {
642 return write_some(
643 s.data(), s.size(), ec);
644 }
645 /** @} */
646
647 /** Parse a buffer containing all or part of a complete JSON.
648
649 This function parses JSON contained in the
650 specified character buffer. If parsing completes,
651 any additional characters past the end of the
652 complete JSON are ignored. The function returns the
653 actual number of characters parsed, which may be
654 less than the size of the input. This allows parsing
655 of a buffer containing multiple individual JSONs or
656 containing different protocol data.
657
658 @par Example
659 @code
660 stream_parser p; // construct a parser
661 std::size_t n; // number of characters used
662 n = p.write_some( "[1,2" ); // parse the first part of the JSON
663 assert( n == 4 ); // all characters consumed
664 n = p.write_some( "3,4] null" ); // parse the rest of the JSON
665 assert( n == 5 ); // only some characters consumed
666 value jv = p.release(); // take ownership of the value
667 @endcode
668
669 @note
670
671 To indicate there are no more character buffers,
672 such as when @ref done returns `false` after
673 writing, call @ref finish.
674
675 @par Complexity
676 Linear in `size`.
677
678 @par Exception Safety
679 Basic guarantee.
680 Calls to `memory_resource::allocate` may throw.
681 Upon error or exception, subsequent calls will
682 fail until @ref reset is called to parse a new JSON.
683
684 @return The number of characters consumed from
685 the buffer.
686
687 @param s The character string to parse.
688
689 @throw system_error Thrown on error.
690 */
691 std::size_t
692 write_some(
693 string_view s)
694 {
695 return write_some(
696 s.data(), s.size());
697 }
698
699 /** Parse a buffer containing all or part of a complete JSON.
700
701 This function parses a all or part of a JSON
702 contained in the specified character buffer. The
703 entire buffer must be consumed; if there are
704 additional characters past the end of the complete
705 JSON, the parse fails and an error is returned.
706
707 @par Example
708 @code
709 stream_parser p; // construct a parser
710 std::size_t n; // number of characters used
711 n = p.write( "[1,2" ); // parse some of the JSON
712 assert( n == 4 ); // all characters consumed
713 n = p.write( "3,4]" ); // parse the rest of the JSON
714 assert( n == 4 ); // all characters consumed
715 value jv = p.release(); // take ownership of the value
716 @endcode
717
718 @note
719
720 To indicate there are no more character buffers,
721 such as when @ref done returns `false` after
722 writing, call @ref finish.
723
724 @par Complexity
725 Linear in `size`.
726
727 @par Exception Safety
728 Basic guarantee.
729 Calls to `memory_resource::allocate` may throw.
730 Upon error or exception, subsequent calls will
731 fail until @ref reset is called to parse a new JSON.
732
733 @return The number of characters consumed from
734 the buffer.
735
736 @param data A pointer to a buffer of `size`
737 characters to parse.
738
739 @param size The number of characters pointed to
740 by `data`.
741
742 @param ec Set to the error, if any occurred.
743 */
744 /** @{ */
745 BOOST_JSON_DECL
746 std::size_t
747 write(
748 char const* data,
749 std::size_t size,
750 error_code& ec);
751
752 BOOST_JSON_DECL
753 std::size_t
754 write(
755 char const* data,
756 std::size_t size,
757 std::error_code& ec);
758 /** @} */
759
760 /** Parse a buffer containing all or part of a complete JSON.
761
762 This function parses a all or part of a JSON
763 contained in the specified character buffer. The
764 entire buffer must be consumed; if there are
765 additional characters past the end of the complete
766 JSON, the parse fails and an error is returned.
767
768 @par Example
769 @code
770 stream_parser p; // construct a parser
771 std::size_t n; // number of characters used
772 n = p.write( "[1,2" ); // parse some of the JSON
773 assert( n == 4 ); // all characters consumed
774 n = p.write( "3,4]" ); // parse the rest of the JSON
775 assert( n == 4 ); // all characters consumed
776 value jv = p.release(); // take ownership of the value
777 @endcode
778
779 @note
780
781 To indicate there are no more character buffers,
782 such as when @ref done returns `false` after
783 writing, call @ref finish.
784
785 @par Complexity
786 Linear in `size`.
787
788 @par Exception Safety
789 Basic guarantee.
790 Calls to `memory_resource::allocate` may throw.
791 Upon error or exception, subsequent calls will
792 fail until @ref reset is called to parse a new JSON.
793
794 @return The number of characters consumed from
795 the buffer.
796
797 @param data A pointer to a buffer of `size`
798 characters to parse.
799
800 @param size The number of characters pointed to
801 by `data`.
802
803 @throw system_error Thrown on error.
804 */
805 BOOST_JSON_DECL
806 std::size_t
807 write(
808 char const* data,
809 std::size_t size);
810
811 /** Parse a buffer containing all or part of a complete JSON.
812
813 This function parses a all or part of a JSON
814 contained in the specified character buffer. The
815 entire buffer must be consumed; if there are
816 additional characters past the end of the complete
817 JSON, the parse fails and an error is returned.
818
819 @par Example
820 @code
821 stream_parser p; // construct a parser
822 std::size_t n; // number of characters used
823 n = p.write( "[1,2" ); // parse some of the JSON
824 assert( n == 4 ); // all characters consumed
825 n = p.write( "3,4]" ); // parse the rest of the JSON
826 assert( n == 4 ); // all characters consumed
827 value jv = p.release(); // take ownership of the value
828 @endcode
829
830 @note
831
832 To indicate there are no more character buffers,
833 such as when @ref done returns `false` after
834 writing, call @ref finish.
835
836 @par Complexity
837 Linear in `size`.
838
839 @par Exception Safety
840 Basic guarantee.
841 Calls to `memory_resource::allocate` may throw.
842 Upon error or exception, subsequent calls will
843 fail until @ref reset is called to parse a new JSON.
844
845 @return The number of characters consumed from
846 the buffer.
847
848 @param s The character string to parse.
849
850 @param ec Set to the error, if any occurred.
851 */
852 /** @{ */
853 std::size_t
854 write(
855 string_view s,
856 error_code& ec)
857 {
858 return write(
859 s.data(), s.size(), ec);
860 }
861
862 std::size_t
863 write(
864 string_view s,
865 std::error_code& ec)
866 {
867 return write(
868 s.data(), s.size(), ec);
869 }
870 /** @} */
871
872 /** Parse a buffer containing all or part of a complete JSON.
873
874 This function parses a all or part of a JSON
875 contained in the specified character buffer. The
876 entire buffer must be consumed; if there are
877 additional characters past the end of the complete
878 JSON, the parse fails and an error is returned.
879
880 @par Example
881 @code
882 stream_parser p; // construct a parser
883 std::size_t n; // number of characters used
884 n = p.write( "[1,2" ); // parse some of the JSON
885 assert( n == 4 ); // all characters consumed
886 n = p.write( "3,4]" ); // parse the rest of the JSON
887 assert( n == 4 ); // all characters consumed
888 value jv = p.release(); // take ownership of the value
889 @endcode
890
891 @note
892
893 To indicate there are no more character buffers,
894 such as when @ref done returns `false` after
895 writing, call @ref finish.
896
897 @par Complexity
898 Linear in `size`.
899
900 @par Exception Safety
901 Basic guarantee.
902 Calls to `memory_resource::allocate` may throw.
903 Upon error or exception, subsequent calls will
904 fail until @ref reset is called to parse a new JSON.
905
906 @return The number of characters consumed from
907 the buffer.
908
909 @param s The character string to parse.
910
911 @throw system_error Thrown on error.
912 */
913 std::size_t
914 write(
915 string_view s)
916 {
917 return write(
918 s.data(), s.size());
919 }
920
921 /** Indicate the end of JSON input.
922
923 This function is used to indicate that there
924 are no more character buffers in the current
925 JSON being parsed. If ther resulting JSON is
926 incomplete, the error is set to indicate a
927 parsing failure.
928
929 @par Example
930 In the code below, @ref finish is called to
931 indicate there are no more digits in the
932 resulting number:
933 @code
934 stream_parser p; // construct a parser
935 p.write( "3." ); // write the first part of the number
936 p.write( "14" ); // write the second part of the number
937 assert( ! p.done() ); // there could be more digits
938 p.finish(); // indicate the end of the JSON input
939 assert( p.done() ); // now we are finished
940 value jv = p.release(); // take ownership of the value
941 @endcode
942
943 @par Complexity
944 Constant.
945
946 @par Exception Safety
947 Basic guarantee.
948 Calls to `memory_resource::allocate` may throw.
949 Upon error or exception, subsequent calls will
950 fail until @ref reset is called to parse a new JSON.
951
952 @param ec Set to the error, if any occurred.
953 */
954 /** @{ */
955 BOOST_JSON_DECL
956 void
957 finish(error_code& ec);
958
959 BOOST_JSON_DECL
960 void
961 finish(std::error_code& ec);
962 /** @} */
963
964 /** Indicate the end of JSON input.
965
966 This function is used to indicate that there
967 are no more character buffers in the current
968 JSON being parsed. If ther resulting JSON is
969 incomplete, the error is set to indicate a
970 parsing failure.
971
972 @par Example
973 In the code below, @ref finish is called to
974 indicate there are no more digits in the
975 resulting number:
976 @code
977 stream_parser p; // construct a parser
978 p.write( "3." ); // write the first part of the number
979 p.write( "14" ); // write the second part of the number
980 assert( ! p.done() ); // there could be more digits
981 p.finish(); // indicate the end of the JSON input
982 assert( p.done() ); // now we are finished
983 value jv = p.release(); // take ownership of the value
984 @endcode
985
986 @par Complexity
987 Constant.
988
989 @par Exception Safety
990 Basic guarantee.
991 Calls to `memory_resource::allocate` may throw.
992 Upon error or exception, subsequent calls will
993 fail until @ref reset is called to parse a new JSON.
994
995 @throw system_error Thrown on error.
996 */
997 BOOST_JSON_DECL
998 void
999 finish();
1000
1001 /** Return the parsed JSON as a @ref value.
1002
1003 This returns the parsed value, or throws
1004 an exception if the parsing is incomplete or
1005 failed. It is necessary to call @ref reset
1006 after calling this function in order to parse
1007 another JSON.
1008
1009 @par Effects
1010 @code
1011 if( ! this->done() )
1012 this->finish();
1013 @endcode
1014 @note
1015
1016 @par Complexity
1017 Constant.
1018
1019 @return The parsed value. Ownership of this
1020 value is transferred to the caller.
1021
1022 @throw system_error Thrown on failure.
1023 */
1024 BOOST_JSON_DECL
1025 value
1026 release();
1027 };
1028
1029 BOOST_JSON_NS_END
1030
1031 #endif