dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
dasmig::core::json_writer Class Reference

Builds one JSON document. More...

#include <json.hpp>

Public Member Functions

 json_writer ()
 An empty writer, with room for a typical document.
 
json_writer & begin_object ()
 Open an object.
 
json_writer & end_object ()
 Close the innermost object.
 
json_writer & begin_array ()
 Open an array.
 
json_writer & end_array ()
 Close the innermost array.
 
json_writer & key (std::string_view name)
 Write an object key; the next call writes its value.
 
json_writer & value (std::string_view text)
 Write a string value.
 
json_writer & value (const char *text)
 Write a string value (keeps string literals from binding to bool).
 
json_writer & value (bool b)
 Write a boolean.
 
json_writer & value (std::nullptr_t)
 Write null.
 
template<std::integral T>
requires (!std::same_as<T, bool> && !std::same_as<T, char> && !std::same_as<T, wchar_t> && !std::same_as<T, char8_t> && !std::same_as<T, char16_t> && !std::same_as<T, char32_t>)
json_writer & value (T n)
 Write an integer.
 
json_writer & value (double d)
 Write a number, in the shortest form that reads back exactly.
 
template<typename V >
json_writer & field (std::string_view name, const V &v)
 Write a key and its value.
 
bool complete () const noexcept
 Whether exactly one complete value has been written.
 
const std::string & str () const
 The document.
 

Static Public Member Functions

static void escape (std::string &out, std::string_view text)
 Append a JSON string literal for text to out.
 

Detailed Description

Builds one JSON document.

.field("name", "Ana")
.field("age", 34)
.key("tags").begin_array().value("calm").value("curious").end_array()
std::string text = w.str(); // {"name":"Ana","age":34,"tags":["calm","curious"]}
Builds one JSON document.
Definition json.hpp:49
json_writer & begin_object()
Open an object.
Definition json.hpp:61
json_writer & key(std::string_view name)
Write an object key; the next call writes its value.
Definition json.hpp:116
json_writer & field(std::string_view name, const V &v)
Write a key and its value.
Definition json.hpp:209
json_writer & begin_array()
Open an array.
Definition json.hpp:88
json_writer & value(std::string_view text)
Write a string value.
Definition json.hpp:133
json_writer & end_object()
Close the innermost object.
Definition json.hpp:73
const std::string & str() const
The document.
Definition json.hpp:222
json_writer & end_array()
Close the innermost array.
Definition json.hpp:99

Definition at line 48 of file json.hpp.

Constructor & Destructor Documentation

◆ json_writer()

dasmig::core::json_writer::json_writer ( )
inline

An empty writer, with room for a typical document.

Definition at line 52 of file json.hpp.

Member Function Documentation

◆ begin_array()

json_writer & dasmig::core::json_writer::begin_array ( )
inline

Open an array.

Returns
*this.
Exceptions
std::logic_errorWhere a value is not allowed.

Definition at line 88 of file json.hpp.

◆ begin_object()

json_writer & dasmig::core::json_writer::begin_object ( )
inline

Open an object.

Returns
*this.
Exceptions
std::logic_errorWhere a value is not allowed.

Definition at line 61 of file json.hpp.

◆ complete()

bool dasmig::core::json_writer::complete ( ) const
inlinenoexcept

Whether exactly one complete value has been written.

Returns
True when str() can be called.

Definition at line 217 of file json.hpp.

◆ end_array()

json_writer & dasmig::core::json_writer::end_array ( )
inline

Close the innermost array.

Returns
*this.
Exceptions
std::logic_errorIf the innermost container is not an array.

Definition at line 99 of file json.hpp.

◆ end_object()

json_writer & dasmig::core::json_writer::end_object ( )
inline

Close the innermost object.

Returns
*this.
Exceptions
std::logic_errorIf the innermost container is not an object, or a key has no value.

Definition at line 73 of file json.hpp.

◆ escape()

static void dasmig::core::json_writer::escape ( std::string &  out,
std::string_view  text 
)
inlinestatic

Append a JSON string literal for text to out.

Parameters
outDestination.
textUTF-8 text.
Exceptions
std::invalid_argumentIf text is not valid UTF-8.

Definition at line 235 of file json.hpp.

◆ field()

template<typename V >
json_writer & dasmig::core::json_writer::field ( std::string_view  name,
const V &  v 
)
inline

Write a key and its value.

Parameters
nameUTF-8 key.
vThe value (any type value() accepts).
Returns
*this.

Definition at line 209 of file json.hpp.

◆ key()

json_writer & dasmig::core::json_writer::key ( std::string_view  name)
inline

Write an object key; the next call writes its value.

Parameters
nameUTF-8 key.
Returns
*this.
Exceptions
std::logic_errorOutside an object or after another key.
std::invalid_argumentIf name is not valid UTF-8.

Definition at line 116 of file json.hpp.

◆ str()

const std::string & dasmig::core::json_writer::str ( ) const
inline

The document.

Returns
The JSON text.
Exceptions
std::logic_errorIf the document is incomplete.

Definition at line 222 of file json.hpp.

◆ value() [1/6]

json_writer & dasmig::core::json_writer::value ( bool  b)
inline

Write a boolean.

Parameters
bThe value.
Returns
*this.

Definition at line 149 of file json.hpp.

◆ value() [2/6]

json_writer & dasmig::core::json_writer::value ( const char *  text)
inline

Write a string value (keeps string literals from binding to bool).

Parameters
textNull-terminated UTF-8 text.
Returns
*this.

Definition at line 144 of file json.hpp.

◆ value() [3/6]

json_writer & dasmig::core::json_writer::value ( double  d)
inline

Write a number, in the shortest form that reads back exactly.

Parameters
dThe value.
Returns
*this.
Exceptions
std::invalid_argumentIf d is NaN or infinite (JSON has no representation for them).

Definition at line 190 of file json.hpp.

◆ value() [4/6]

json_writer & dasmig::core::json_writer::value ( std::nullptr_t  )
inline

Write null.

Returns
*this.

Definition at line 159 of file json.hpp.

◆ value() [5/6]

json_writer & dasmig::core::json_writer::value ( std::string_view  text)
inline

Write a string value.

Parameters
textUTF-8 text.
Returns
*this.
Exceptions
std::invalid_argumentIf text is not valid UTF-8.

Definition at line 133 of file json.hpp.

◆ value() [6/6]

template<std::integral T>
requires (!std::same_as<T, bool> && !std::same_as<T, char> && !std::same_as<T, wchar_t> && !std::same_as<T, char8_t> && !std::same_as<T, char16_t> && !std::same_as<T, char32_t>)
json_writer & dasmig::core::json_writer::value ( T  n)
inline

Write an integer.

Template Parameters
TIntegral type other than bool and the character types.
Parameters
nThe value.
Returns
*this.

Definition at line 175 of file json.hpp.


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