slang::JsonWriter class

Contents

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)
void setPrettyPrint(bool enabled)
auto view() const -> string_view
void startObject()
void endObject()
void startArray()
void endArray()
void writeProperty(string_view name)
void writeValue(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").

Function documentation

void slang::JsonWriter::setIndentSize(int size)

Sets the number of spaces to indent whenever opening a new level of structure in the JSON.

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.

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(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.