|
DAW JSON Link
|
daw::json::json_writer builds one JSON document incrementally. It is useful when the document structure is known while writing, but constructing an intermediate C++ object or container would be inconvenient.
Include the incremental writer header and create a writer over any supported writable output. This includes string-like containers, C++ output streams, and C FILE * outputs:
For example, a writer can write directly to standard output:
Values passed to write_value and write_key_value use the same serialization support as daw::json::to_json, including JSON data contracts.
Use write_key_value when a member's value is immediately available:
The result is:
Use add_key when the value will be written separately. This is particularly useful when the value is an array or object:
The result is:
Inside an object, write_value must follow add_key. Calling add_key again before writing the previous key's value writes null for the previous value.
An array can be the root document:
The result is:
write_array_values appends an initializer list, a container-like range, or a heterogeneous argument list to an open array:
The result is:
write_value also accepts an initializer list or multiple values and writes them as a complete JSON array. This is useful at the root or after add_key:
The result is:
Likewise, passing an initializer list or multiple values to write_key_value writes the member value as an array:
Objects and arrays can be nested to any depth supported by the writer's state stack:
The result is:
A scalar can be written as the complete document without opening a container:
Only one root value, object, or array can be written before the writer is reset.
Writer functions normally deduce the JSON representation from the C++ value. The following functions accept an optional JSON mapping type as their first template argument:
write_value<JsonClass>(value)write_number<JsonClass>(value)write_string<JsonClass>(value)write_key_value<JsonClass>(key, value)write_array_values<JsonClass>(range)write_array_values<JsonClass>({ values... })The initializer-list overloads of write_value and write_key_value also accept an explicit mapping and apply it to every array element.
For example, FPOutputFormat::Decimal can be used to preserve the decimal form of a floating-point value:
The mapping can be supplied to write_number:
or to the more general write_value:
Both examples produce:
The same mapping can be used when writing an object member:
The result is:
It can also be applied to a range or initializer list:
An explicit mapping cannot be supplied to the variadic write_array_values(value, values...) overload. Its potentially heterogeneous values are mapped individually using their deduced types. The variadic write_value and write_key_value forms use this behavior when producing their arrays.
The mapping supplied to write_number must have a number or Boolean underlying JSON type. The mapping supplied to write_string must have a number, Boolean, or string underlying JSON type. write_boolean and write_null have fixed representations and do not take a mapping type.
Serialization policy flags are template arguments to json_writer. For example, enable pretty output as follows:
The result is:
See Output Options for the available serialization policy flags.
Explicitly closing every object and array makes the intended structure clear, but the writer also closes open containers when it is destroyed:
If an object key has no value when another key is added, the object is closed, or the writer is destroyed, its value is written as null:
The result is:
Call finalize() to perform the same completion explicitly before the writer is destroyed. It writes null for a pending object key and closes all open objects and arrays:
Call reset() to finish the current document and reset the writer state so another root JSON value can be appended to the same output. reset() calls finalize(), so it writes null for a pending object key and closes every open object and array. It does not clear or otherwise modify output that has already been written.
Here, result is {"answer":42}true. Add any separator required by the surrounding output format before writing the next root value.
add_key and write_key_value are valid only inside an object.add_key.write_array_values is valid only inside an array.write_value or write_key_value writes those values as an array.close_object and close_array must match the currently open container.reset() and the next reset().finalize() completes the current document, and reset() finalizes it before resetting the writer state.