Writer
Writer is an incremental generator that outputs C data types in a JSON format.
The output is generated piece by piece as C data types are inserted.
The writer keeps track of the context and inserts the appropriate
separators between values where needed.
Writer member function |
Description |
|
Returns the current level of nested containers. |
|
Returns the current error code. |
|
Write a literal value directly into the JSON output without formatting it. Returns the number of characters written. Returns zero if an error occurred. |
|
Write a formatted tag into the JSON output. Returns the number of characters written. Returns zero if an error occurred. |
|
Write a formatted value into the JSON output. Returns the number of characters written. Returns zero if an error occurred. |
Values are properly formatted and written into the JSON output with writer::value(T).
The parameter T can be a boolean, a number, or a string.
Writing a nullable value or the opening and closing brackets of containers is
done by passing special tags as the parameter to the parameter-less version
writer::value<T>().
Literal values can also be inserted unconverted into the JSON output with
writer::literal().
These can be useful useful for adding whitespaces, but special care should be
exerted to not violate the JSON format.
As writer has been designed for wire protocols, it does not insert whitespaces
into the output.[1].
|
The following examples assume that you have included the following header files:
|
Boolean
Boolean values are output via writer::value(bool).
std::ostringstream result;
json::writer writer(result);
writer.value(true); // Write boolean value
assert(result.str() == "true");
String
Strings are written by passing an std::string or a string literal to
writer::value(T).
All strings will be quoted in the JSON output, and special characters will be
escaped.
Strings must be UTF-8 encoded.
std::ostringstream result;
json::writer writer(result);
writer.value("alpha"); // Write string
assert(result.str() == "\"alpha\"");
Null
Nullable value are output with writer::value<token::null>().
std::ostringstream result;
json::writer writer(result);
writer.value<json::token::null>(); // Write nullable value
assert(result.str() == "null");
Array
An array is initiated by passing the json::begin_array tag to writer::value(T),
and terminated by passing the json::end_array. These tags must be properly balanced,
otherwise an error will be raised.
Value separators are automatically inserted between values.
std::ostringstream result;
json::writer writer(result);
writer.value(json::begin_array); // Write beginning of array
assert(result.str() == "[");
writer.value(42);
assert(result.str() == "[42"); // Write number
writer.value(43);
assert(result.str() == "[42,43"); // Write number
writer.value(json::end_array);
assert(result.str() == "[42,43]"); // Write ending of array
Associative array
Name separators are automatically inserted between the key and the value, and value separators are automatically inserted between key-value pairs.
example/json/pretty_printer for an example of how to produce an indented output.