GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::json::Writer Struct Reference

Stateful wrapper object of an output stream for JSON serialization. More...

#include <GREM/core/formats/json.hpp>

Public Member Functions

 Writer (std::ostream &stream, const SerializationOptions &options={})
 Construct a writer with an output stream as output.
void write (char ch)
 Write a single raw char to the output without any extra formatting.
void write (StringView raw)
 Write a raw sequence of chars to the output without any extra formatting.
void writeIndentation ()
 Write a sequence of indentation characters to the output.
void writeNewline ()
 Write a newline sequence to the output.
void writeNull ()
 Write a single JSON value of type Null to the output.
void writeBoolean (Boolean value)
 Write a single JSON value of type Boolean to the output.
void writeString (StringView value)
 Write a single JSON value of type String to the output from a raw string.
void writeString (UTF8StringView value)
 Write a single JSON value of type String to the output from a raw UTF8 string.
void writeString (const auto &value)
 Write a single JSON value of type String to the output from any string-view-like or JSON-serializable value.
void writeNumber (Number value)
 Write a single JSON value of type Number to the output.
void writeCustomObject (size_t propertyCountHint, auto callback)
 Write a single JSON object to the output, using a callback function for writing out the properties.
void writeCustomObject (auto callback)
 Write a single JSON object to the output, using a callback function for writing out the properties.
template<typename SerializerOverride = detail::NoSerializerOverride>
void writeObject (const auto &value, SerializerOverride serializerOverride={})
 Write a single JSON object to the output from any range of JSON-serializable key-value pairs.
void writeCustomArray (size_t itemCountHint, auto callback)
 Write a single JSON array to the output, using a callback function for writing out the items.
void writeCustomArray (auto callback)
 Write a single JSON array to the output, using a callback function for writing out the items.
template<typename SerializerOverride = detail::NoSerializerOverride>
void writeArray (const auto &value, SerializerOverride serializerOverride={})
 Write a single JSON array to the output from any range of JSON-serializable values.
template<typename SerializerOverride = detail::NoSerializerOverride>
void writeOptional (const auto &value, SerializerOverride serializerOverride={})
 Write a single JSON value to the output from any value that supports conversion to bool and the dereference operator.
template<typename Aggregate, typename SerializerOverride = detail::NoSerializerOverride>
void writeAggregateObject (const Aggregate &value, SerializerOverride serializerOverride={})
 Write a single JSON value to the output from any value of aggregate type.
template<typename Aggregate, typename SerializerOverride = detail::NoSerializerOverride>
void writeAggregateArray (const Aggregate &value, SerializerOverride serializerOverride={})
 Write a single JSON value to the output from any value of aggregate type.
template<typename T, typename SerializerOverride = detail::NoSerializerOverride>
void serialize (const T &value, SerializerOverride serializerOverride={})
 Write any JSON-serializable value to the output using its corresponding implementation of Serializer.

Public Attributes

SerializationOptions options
 The current options of the serialization process.

Detailed Description

Stateful wrapper object of an output stream for JSON serialization.

Constructor & Destructor Documentation

◆ Writer()

grem::json::Writer::Writer ( std::ostream & stream,
const SerializationOptions & options = {} )
inlineexplicit

Construct a writer with an output stream as output.

Parameters
streamoutput stream to write to.
optionsoutput options, see SerializationOptions.

Member Function Documentation

◆ write() [1/2]

void grem::json::Writer::write ( char ch)
inline

Write a single raw char to the output without any extra formatting.

Parameters
chvalue to write.
Exceptions
anyexception thrown by the underlying output stream.

◆ write() [2/2]

void grem::json::Writer::write ( StringView raw)
inline

Write a raw sequence of chars to the output without any extra formatting.

Parameters
rawvalues to write.
Exceptions
anyexception thrown by the underlying output stream.

◆ writeIndentation()

void grem::json::Writer::writeIndentation ( )
inline

Write a sequence of indentation characters to the output.

The length of the sequence matches the current indentation level specified in the options.

Exceptions
anyexception thrown by the underlying output stream.
See also
SerializationOptions::indentation
SerializationOptions::indentationCharacter

◆ writeNewline()

void grem::json::Writer::writeNewline ( )
inline

Write a newline sequence to the output.

Exceptions
anyexception thrown by the underlying output stream.

◆ writeNull()

void grem::json::Writer::writeNull ( )
inline

Write a single JSON value of type Null to the output.

Exceptions
anyexception thrown by the underlying output stream.

◆ writeBoolean()

void grem::json::Writer::writeBoolean ( Boolean value)
inline

Write a single JSON value of type Boolean to the output.

Parameters
valuevalue to write.
Exceptions
anyexception thrown by the underlying output stream.

◆ writeString() [1/3]

void grem::json::Writer::writeString ( StringView value)
inline

Write a single JSON value of type String to the output from a raw string.

Parameters
valueraw string value to write.
Exceptions
anyexception thrown by the underlying output stream.

◆ writeString() [2/3]

void grem::json::Writer::writeString ( UTF8StringView value)
inline

Write a single JSON value of type String to the output from a raw UTF8 string.

Parameters
valueraw string value to write.
Exceptions
anyexception thrown by the underlying output stream.

◆ writeString() [3/3]

void grem::json::Writer::writeString ( const auto & value)
inline

Write a single JSON value of type String to the output from any string-view-like or JSON-serializable value.

Parameters
valuevalue to write.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the underlying string view conversion or Serializer implementation of the given value type.

◆ writeNumber()

void grem::json::Writer::writeNumber ( Number value)
inline

Write a single JSON value of type Number to the output.

Parameters
valuevalue to write.
Exceptions
anyexception thrown by the underlying output stream.

◆ writeCustomObject() [1/2]

void grem::json::Writer::writeCustomObject ( size_t propertyCountHint,
auto callback )
inline

Write a single JSON object to the output, using a callback function for writing out the properties.

Parameters
propertyCountHintestimated number of properties contained by the object, which will be used to determine how to format the object when pretty printing is enabled.
callbackfunction object for writing the object contents, which should return void and accept the following generic parameter:
  • auto writeProperty: a function object for writing each property of the object, which accepts the following parameters:
    • const auto& key: property key, writable as string.
    • const auto& value: property value to write.
    • auto writeValue (optional): a function object that accepts the property value as an argument and writes its value to the writer. Defaults to calling writer.serialize(value) if not provided.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the callback function.

◆ writeCustomObject() [2/2]

void grem::json::Writer::writeCustomObject ( auto callback)
inline

Write a single JSON object to the output, using a callback function for writing out the properties.

Parameters
callbackfunction object for writing the object contents, which should return void and accept the following generic parameter:
  • auto writeProperty: a function object for writing each property of the object, which accepts the following parameters:
    • const auto& key: property key, writable as string.
    • const auto& value: property value to write.
    • auto writeValue (optional): a function object that accepts the property value as an argument and writes its value to the writer. Defaults to calling writer.serialize(value) if not provided.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the callback function.

◆ writeObject()

template<typename SerializerOverride = detail::NoSerializerOverride>
void grem::json::Writer::writeObject ( const auto & value,
SerializerOverride serializerOverride = {} )
inline

Write a single JSON object to the output from any range of JSON-serializable key-value pairs.

Parameters
valuerange to write as an object.
serializerOverrideoverloaded function object that overrides the default serialization behavior for nested values whenever a call to serializerOverride(writer, value) yields a valid matching overload.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer/SerializerOverride implementations of the given key/value types.

◆ writeCustomArray() [1/2]

void grem::json::Writer::writeCustomArray ( size_t itemCountHint,
auto callback )
inline

Write a single JSON array to the output, using a callback function for writing out the items.

Parameters
itemCountHintestimated number of items contained by the array, which will be used to determine how to format the array when pretty printing is enabled.
callbackfunction object for writing the array contents, which should return void and accept the following generic parameter:
  • auto writeItem: a function object for writing each item of the array, which accepts the following parameters:
    • const auto& value: item value to write.
    • auto writeValue (optional): a function object that accepts the item value as an argument and writes its value to the writer. Defaults to calling writer.serialize(value) if not provided.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the callback function.

◆ writeCustomArray() [2/2]

void grem::json::Writer::writeCustomArray ( auto callback)
inline

Write a single JSON array to the output, using a callback function for writing out the items.

Parameters
callbackfunction object for writing the array contents, which should return void and accept the following generic parameter:
  • auto writeItem: a function object for writing each item of the array, which accepts the following parameters:
    • const auto& value: item value to write.
    • auto writeValue (optional): a function object that accepts the item value as an argument and writes its value to the writer. Defaults to calling writer.serialize(value) if not provided.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the callback function.

◆ writeArray()

template<typename SerializerOverride = detail::NoSerializerOverride>
void grem::json::Writer::writeArray ( const auto & value,
SerializerOverride serializerOverride = {} )
inline

Write a single JSON array to the output from any range of JSON-serializable values.

Parameters
valuerange to write as an array.
serializerOverrideoverloaded function object that overrides the default serialization behavior for nested items whenever a call to serializerOverride(writer, value) yields a valid matching overload.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer/SerializerOverride implementation of the element type of the given value type.

◆ writeOptional()

template<typename SerializerOverride = detail::NoSerializerOverride>
void grem::json::Writer::writeOptional ( const auto & value,
SerializerOverride serializerOverride = {} )
inline

Write a single JSON value to the output from any value that supports conversion to bool and the dereference operator.

If the value evaluates to true when converted to bool, the value is dereferenced and serialized normally. Otherwise, null is written.

Parameters
valuevalue to write.
serializerOverrideoverloaded function object that overrides the default serialization behavior for the nested value whenever a call to serializerOverride(writer, value) yields a valid matching overload.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer implementation of the dereferenced type of the given value type.

◆ writeAggregateObject()

template<typename Aggregate, typename SerializerOverride = detail::NoSerializerOverride>
void grem::json::Writer::writeAggregateObject ( const Aggregate & value,
SerializerOverride serializerOverride = {} )
inline

Write a single JSON value to the output from any value of aggregate type.

The output JSON format is an object with properties whose keys match the field names of the aggregate.

Parameters
valueaggregate whose fields to write.
serializerOverrideoverloaded function object that overrides the default serialization behavior for nested fields whenever a call to serializerOverride(writer, value) yields a valid matching overload.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer/SerializerOverride implementations of the given field types.

◆ writeAggregateArray()

template<typename Aggregate, typename SerializerOverride = detail::NoSerializerOverride>
void grem::json::Writer::writeAggregateArray ( const Aggregate & value,
SerializerOverride serializerOverride = {} )
inline

Write a single JSON value to the output from any value of aggregate type.

The output JSON format is an array with all fields as items.

Parameters
valueaggregate whose fields to write.
serializerOverrideoverloaded function object that overrides the default serialization behavior for nested fields whenever a call to serializerOverride(writer, value) yields a valid matching overload.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer/SerializerOverride implementations of the given field types.

◆ serialize()

template<typename T, typename SerializerOverride>
void grem::json::Writer::serialize ( const T & value,
SerializerOverride serializerOverride = {} )
inline

Write any JSON-serializable value to the output using its corresponding implementation of Serializer.

Parameters
valuevalue to write.
serializerOverrideoverloaded function object that overrides the default serialization behavior for the value or its nested elements/fields whenever a call to serializerOverride(writer, value) yields a valid matching overload.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer/SerializerOverride implementation of T.

Member Data Documentation

◆ options

SerializationOptions grem::json::Writer::options

The current options of the serialization process.


The documentation for this struct was generated from the following file: