xentara-utils v2.0.4
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::json::encoder::Document Class Referencefinal

A JSON document. More...

#include <xentara/utils/json/encoder/Document.hpp>

+ Inheritance diagram for xentara::utils::json::encoder::Document:

Public Types

enum class  Format { Regular , Compact }
 different formats More...
 

Public Member Functions

 Document (std::reference_wrapper< io::OutputStream< char > > stream, Format format=Format::Regular) noexcept
 Constructor.
 
 ~Document () noexcept(false)
 Destructor.
 
auto close () -> void
 Writes the termination of the document, if it has not already been written.
 
auto asObject () -> Object
 Adds a JSON object as the root element of the document.
 
auto asArray () -> Array
 Adds a JSON array as the root element of the document.
 
auto asValue () -> Value
 Adds a generic JSON value as the root element of the document.
 

Detailed Description

A JSON document.

Objects of this class can be moved, but not copied.

Member Enumeration Documentation

◆ Format

different formats

Enumerator
Regular 

The regular format that uses indentation and whitespace.

Compact 

A compact format without indentation or whitespace.

Constructor & Destructor Documentation

◆ Document()

xentara::utils::json::encoder::Document::Document ( std::reference_wrapper< io::OutputStream< char > >  stream,
Format  format = Format::Regular 
)
explicitnoexcept

Constructor.

Parameters
streamThe stream to write the document to
formatThe format to use

◆ ~Document()

xentara::utils::json::encoder::Document::~Document ( )

Destructor.

This destructor writes the termination, if it has not already been written.

This destructor will throw an exception if the termination cannot be written. To prevent termination of the program, no exceptions are thrown during stack windup, though. You can prevent the destructor from throwing any exceptions by closing the objects manually beforehand using close().

Exceptions
std::runtime_errorAn error occured writing to the stream

Member Function Documentation

◆ asArray()

auto xentara::utils::json::encoder::Document::asArray ( ) -> Array

Adds a JSON array as the root element of the document.

Returns
An object that can be used to add elements to the array
Exceptions
std::runtime_errorAn error occured writing to the stream

◆ asObject()

auto xentara::utils::json::encoder::Document::asObject ( ) -> Object

Adds a JSON object as the root element of the document.

Returns
An object that can be used to add members to the object
Exceptions
std::runtime_errorAn error occured writing to the stream

◆ asValue()

auto xentara::utils::json::encoder::Document::asValue ( ) -> Value

Adds a generic JSON value as the root element of the document.

Returns
An value that can be used to write the value
Exceptions
std::runtime_errorAn error occured writing to the stream

◆ close()

auto xentara::utils::json::encoder::Document::close ( ) -> void

Writes the termination of the document, if it has not already been written.

Note
This function can be safely called on objects that have been moved away using the move constructor or move assignment operator.
Exceptions
std::runtime_errorAn error occured writing to the stream