libdonut  2.3.2
Application framework for cross-platform game development in C++20
Public Member Functions | Public Attributes | List of all members
donut::json::Writer Struct Reference

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

#include <donut/json.hpp>

Public Member Functions

 Writer (std::ostream &stream, const SerializationOptions &options={})
 Construct a writer with an output stream as output. More...
 
void write (char byte)
 Write a single raw byte to the output without any extra formatting. More...
 
void write (std::string_view bytes)
 Write a raw sequence of bytes to the output without any extra formatting. More...
 
void writeIndentation ()
 Write a sequence of indentation characters to the output. More...
 
void writeNewline ()
 Write a newline sequence to the output. More...
 
void writeNull ()
 Write a single JSON value of type Null to the output. More...
 
void writeBoolean (Boolean value)
 Write a single JSON value of type Boolean to the output. More...
 
void writeString (std::string_view bytes)
 Write a single JSON value of type String to the output from a raw byte string. More...
 
template<typename CharT , typename Traits >
void writeString (std::basic_string_view< CharT, Traits > value) requires(!std
 Write a single JSON value of type String to the output from a raw string, interpreted as raw bytes. More...
 
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. More...
 
void writeNumber (Number value)
 Write a single JSON value of type Number to the output. More...
 
template<typename PropertyFilter = detail::AlwaysTrue, typename GetKey = detail::GetFirst, typename GetValue = detail::GetSecond>
void writeObject (const auto &value, PropertyFilter propertyFilter={}, GetKey getKey={}, GetValue getValue={})
 Write a single JSON object to the output from any range of JSON-serializable key-value pairs. More...
 
template<typename ItemFilter = detail::AlwaysTrue, typename GetValue = detail::Get>
void writeArray (const auto &value, ItemFilter itemFilter={}, GetValue getValue={})
 Write a single JSON array to the output from any range of JSON-serializable values. More...
 
void writeOptional (const auto &value)
 Write a single JSON value to the output from any value that supports conversion to bool and the dereference operator. More...
 
template<typename T >
void writeAggregate (const T &value)
 Write a single JSON value to the output from any value of aggregate type. More...
 
template<typename T >
void serialize (const T &value)
 Write any JSON-serializable value to the output using its corresponding implementation of Serializer. More...
 

Public Attributes

SerializationOptions options
 The current options of the serialization process. More...
 

Detailed Description

Stateful wrapper object of an output stream for JSON serialization.

Constructor & Destructor Documentation

◆ Writer()

donut::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 donut::json::Writer::write ( char  byte)
inline

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

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

◆ write() [2/2]

void donut::json::Writer::write ( std::string_view  bytes)
inline

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

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

◆ writeIndentation()

void donut::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 donut::json::Writer::writeNewline ( )
inline

Write a newline sequence to the output.

Exceptions
anyexception thrown by the underlying output stream.

◆ writeNull()

void donut::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 donut::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 donut::json::Writer::writeString ( std::string_view  bytes)
inline

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

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

◆ writeString() [2/3]

template<typename CharT , typename Traits >
void donut::json::Writer::writeString ( std::basic_string_view< CharT, Traits >  value)
inline

Write a single JSON value of type String to the output from a raw string, interpreted as raw bytes.

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

◆ writeString() [3/3]

void donut::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 donut::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.

◆ writeObject()

template<typename PropertyFilter = detail::AlwaysTrue, typename GetKey = detail::GetFirst, typename GetValue = detail::GetSecond>
void donut::json::Writer::writeObject ( const auto &  value,
PropertyFilter  propertyFilter = {},
GetKey  getKey = {},
GetValue  getValue = {} 
)
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.
propertyFilterpredicate that each key-value pair from the range must pass in order to be included in the output. Defaults to always pass.
getKeyfunction for getting the key from each key-value pair. Defaults to returning pair.first.
getValuefunction for getting the value from each key-value pair. Defaults to returning pair.second.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer implementations of the given key/value types.

◆ writeArray()

template<typename ItemFilter = detail::AlwaysTrue, typename GetValue = detail::Get>
void donut::json::Writer::writeArray ( const auto &  value,
ItemFilter  itemFilter = {},
GetValue  getValue = {} 
)
inline

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

Parameters
valuerange to write as an array.
itemFilterpredicate that each value from the range must pass in order to be included in the output. Defaults to always pass.
getValuefunction for getting the value from each value of the range. Defaults to returning each value without modification.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer implementation of the element type of the given value type.

◆ writeOptional()

void donut::json::Writer::writeOptional ( const auto &  value)
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.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer implementation of the dereferenced type of the given value type.

◆ writeAggregate()

template<typename T >
void donut::json::Writer::writeAggregate ( const T &  value)
inline

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

Parameters
valueaggregate whose fields to write.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer implementations of the given field types.

◆ serialize()

template<typename T >
void donut::json::Writer::serialize ( const T &  value)
inline

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

Parameters
valuevalue to write.
Exceptions
anyexception thrown by the underlying output stream.
anyexception thrown by the Serializer implementation of T.

Member Data Documentation

◆ options

SerializationOptions donut::json::Writer::options

The current options of the serialization process.


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