The <<ref_parse>> function offers a simple interface for converting a serialized JSON text to a <<ref_value>> in a single function call. This overload uses exceptions to indicate errors:
The <<ref_value>> returned in the preceding examples use the <<default_memory_resource,default memory resource>>. The following code uses a <<ref_monotonic_resource>>, which results in faster parsing. `jv` is marked `const` to prevent subsequent modification, because containers using a monotonic resource waste memory when mutated.
Unless otherwise specified, the parser in this library is strict. It recognizes only valid, standard JSON. The parser can be configured to allow certain non-standard extensions by filling in a <<ref_parse_options>> structure and passing it by value. By default all extensions are disabled:
When building with {cpp}20 or later, the use of https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers[designated initializers] with <<ref_parse_options>> is possible:
The default algorithm that the library uses to parse numbers is fast, but may result in slight precision loss. This may not be suitable for some applications, so there is an option to enable an alternative algorithm that doesn't have that flaw, but is somewhat slower. To do this, you also need to use <<ref_parse_options>> structure.
Note that full precision number parsing requires the algorithm to see the full number. This means, that when used with <<ref_stream_parser>>, additional memory allocations may be necessary to store the number parts which were so far accepted by the parser. The library does try its best to avoid such allocations.
The parser implementation uses temporary storage space to accumulate values during parsing. When using the <<ref_parse>> free functions, this storage is allocated and freed in each call. However, by declaring an instance of <<ref_parser>> or <<ref_stream_parser>>, this temporary storage can be reused when parsing more than one JSON text, reducing the total number of dynamic memory allocations.
To use the <<ref_parser>>, declare an instance. Then call <<ref_parser_write>> once with the buffer containing representing the input JSON. Finally, call <<ref_parser_release>> to take ownership of the resulting <<ref_value>> upon success. This example persists the parser instance in a class member to reuse across calls:
Sometimes a protocol may have a JSON text followed by data that is in a different format or specification. The JSON portion can still be parsed by using the function <<ref_parser_write_some>>. Upon success, the return value will indicate the number of characters consumed from the input, which will exclude the non-JSON characters:
The <<ref_stream_parser>> implements a https://en.wikipedia.org/wiki/Online_algorithm[__streaming algorithm__]; it allows incremental processing of large JSON inputs using one or more contiguous character buffers. The entire input JSON does not need to be loaded into memory at once. A network server can use the streaming interface to process incoming JSON in fixed-size amounts, providing these benefits:
To use the <<ref_stream_parser>>, declare an instance. Then call <<ref_stream_parser_write>> zero or more times with successive buffers representing the input JSON. When there are no more buffers, call <<ref_stream_parser_finish>>. The function <<ref_stream_parser_done>> returns `true` after a successful call to `write` or `finish` if parsing is complete.
In the following example a JSON text is parsed from standard input a line at a time. Error codes are used instead. The function <<ref_stream_parser_finish>> is used to indicate the end of the input:
After default construction, or after <<ref_stream_parser_reset>> is called with no arguments, the <<ref_value>> produced after a successful parse operation uses the default memory resource. To use a different memory resource, call `reset` with the resource to use. Here we use a <<ref_monotonic_resource>>, which is optimized for parsing but not subsequent modification: