= Quick Look Here we highlight important features through example code to help convey the style of the interface. We begin by including the library header file which brings all the symbols into scope. Alternatively, individual headers may be included to obtain the declarations for specific types:
In order to link your program you will need to link with a built library. Alternatively you can use the header-only configuration simply by including this header file in any __one__ of your new or existing source files:
In this library the types <<ref_array>>, <<ref_object>>, and <<ref_string>> hold JSON arrays, objects, and strings respectively while the type <<ref_value>> is a special variant which can hold any JSON element. Here we construct an empty object and then insert the elements above:
While keys are strings, the mapped type of objects and the element type of arrays is the aforementioned type <<ref_value>> which can hold any JSON element, as seen in the previous assignments. Instead of building the JSON document using a series of function calls, we can build it in one statement using an initializer list:
When a <<ref_value>>, <<ref_array>>, or <<ref_object>> is assigned or constructed from an initializer list, the creation of the new value happens only once. This makes initializer lists equally efficient as using the other ways to create a value. The types in this library are first-class, supporting copy and move construction and assignment:
To permit custom memory allocation strategies, these containers all allow construction with a <<ref_storage_ptr>> which is a smart pointer to a {ref_memory_resource}. The constructor signatures have the same ordering as their `std` equivalents which use {req_Allocator} parameters. Once a container is constructed its memory resource can never change. Here we create an array without performing any dynamic allocations:
When a library type is used as the element type of a PMR container; that is, a container which uses a {ref_polymorphic_allocator}, the memory resource will automatically propagate to the type and all of its children:
Up until now we have shown how values may be constructed from a memory resource pointer, where ownership is not transferred. In this case the caller is responsible for ensuring that the lifetime of the resource is extended for the life of the container. Sometimes you want the container to acquire shared ownership of the resource. This is accomplished with <<ref_make_shared_resource>>:
JSON can be parsed into the value container in one step using a free function. In the following snippet, a parse error is indicated by a thrown exception:
By default, the parser is strict and only accepts JSON compliant with the standard. However this behavior can be relaxed by filling out an options structure enabling one or more extensions. Here we use a static buffer and enable two non-standard extensions:
The parser in this library implements a https://en.wikipedia.org/wiki/Online_algorithm[__streaming algorithm__]; it can process JSON piece-by-piece, without the requirement that the entire input is available from the start. The parser uses a temporary memory allocation to do its work. If you plan on parsing multiple JSONs, for example in a network server, keeping the same parser instance will allow re-use of this temporary storage, improving performance.
With strategic use of the right memory resources, parser instance, and calculated upper limits on buffer sizes, it is possible to parse and examine JSON without __any__ dynamic memory allocations. This is explored in more detail in a later section.