slang::JsonWriter class

A very lightweight JSON writer.

This class is simple and has few features; it's expected that you'll call its methods in the correct order to generate valid JSON. If not, it will happily spit out unparseable text.

Public functions

void setIndentSize(int size)
Sets the number of spaces to indent whenever opening a new level of structure in the JSON.
void setPrettyPrint(bool enabled)
Set whether pretty printing is enabled (off by default).
auto view() const -> std::string_view
void startObject()
Begins a new JSON object.
void endObject()
Ends the currently active object.
void startArray()
Begins a new JSON array.
void endArray()
Ends the currently active array.
void writeProperty(std::string_view name)
Writes an object property with the given name.
void writeValue(std::string_view value)
Writes an array or property string value.
void writeValue(int64_t value)
Writes an array or property signed integer value.
void writeValue(uint64_t value)
Writes an array or property unsigned integer value.
void writeValue(double value)
Writes an array or property floating point value.
void writeValue(bool value)
Writes an array or property boolean value ("true" or "false").
void writeNewLine()
Writes a newline character into the buffer.

Function documentation

void slang::JsonWriter::setPrettyPrint(bool enabled)

Set whether pretty printing is enabled (off by default).

When pretty printing is on, newlines, additional whitespace, and indentation are added to make the output more human friendly.

std::string_view slang::JsonWriter::view() const

Returns a view of the emitted JSON text so far.

void slang::JsonWriter::startObject()

Begins a new JSON object.

It's expected that you will write zero or more properties and then end the object.

void slang::JsonWriter::endObject()

Ends the currently active object.

Output will be messed up if there is no active object.

void slang::JsonWriter::startArray()

Begins a new JSON array.

It's expected that you will write zero or more values and then end the array.

void slang::JsonWriter::endArray()

Ends the currently active array.

Output will be messed up if there is no active object.

void slang::JsonWriter::writeProperty(std::string_view name)

Writes an object property with the given name.

It's expected that you will immediately write some kind of value for the property.