C++ Actor Framework 0.19
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | List of all members
caf::json_writer Class Reference

Serializes an inspectable object to a JSON-formatted string. More...

#include <json_writer.hpp>

Inheritance diagram for caf::json_writer:
caf::serializer

Public Types

enum class  type : uint8_t {
  element ,
  object ,
  member ,
  key ,
  array ,
  string ,
  number ,
  boolean ,
  null
}
 Reflects the structure of JSON objects according to ECMA-404. More...
 
using super = serializer
 
- Public Types inherited from caf::serializer
using super = save_inspector_base< serializer >
 

Public Member Functions

 json_writer (actor_system &sys)
 
 json_writer (execution_unit *ctx)
 
std::string_view str () const noexcept
 Returns a string view into the internal buffer.
 
size_t indentation () const noexcept
 Returns the current indentation factor.
 
void indentation (size_t factor) noexcept
 Sets the indentation level.
 
bool compact () const noexcept
 Returns whether the writer generates compact JSON output without any spaces or newlines to separate values.
 
bool skip_empty_fields () const noexcept
 Returns whether the writer omits empty fields entirely (true) or renders empty fields as $field: null (false).
 
void skip_empty_fields (bool value) noexcept
 Configures whether the writer omits empty fields.
 
bool skip_object_type_annotation () const noexcept
 Returns whether the writer omits '@type' annotations for JSON objects.
 
void skip_object_type_annotation (bool value) noexcept
 Configures whether the writer omits '@type' annotations for JSON objects.
 
std::string_view field_type_suffix () const noexcept
 Returns the suffix for generating type annotation fields for variant fields.
 
void field_type_suffix (std::string_view suffix) noexcept
 Configures whether the writer omits empty fields.
 
const type_id_mappermapper () const noexcept
 Returns the type ID mapper used by the writer.
 
void mapper (const type_id_mapper *ptr) noexcept
 Changes the type ID mapper for the writer.
 
void reset ()
 Removes all characters from the buffer and restores the writer to its initial state.
 
bool begin_object (type_id_t type, std::string_view name) override
 Begins processing of an object.
 
bool end_object () override
 Ends processing of an object.
 
bool begin_field (std::string_view) override
 
bool begin_field (std::string_view name, bool is_present) override
 
bool begin_field (std::string_view name, span< const type_id_t > types, size_t index) override
 
bool begin_field (std::string_view name, bool is_present, span< const type_id_t > types, size_t index) override
 
bool end_field () override
 
bool begin_tuple (size_t size) override
 Begins processing of a tuple.
 
bool end_tuple () override
 Ends processing of a tuple.
 
bool begin_key_value_pair () override
 Begins processing of a tuple with two elements, whereas the first element represents the key in an associative array.
 
bool end_key_value_pair () override
 Ends processing of a key-value pair after both values were written.
 
bool begin_sequence (size_t size) override
 Begins processing of a sequence. Saves the size to the underlying storage.
 
bool end_sequence () override
 Ends processing of a sequence.
 
bool begin_associative_array (size_t size) override
 Begins processing of an associative array (map).
 
bool end_associative_array () override
 Ends processing of an associative array (map).
 
bool value (std::byte x) override
 Adds x to the output.
 
bool value (bool x) override
 Adds x to the output.
 
bool value (int8_t x) override
 Adds x to the output.
 
bool value (uint8_t x) override
 Adds x to the output.
 
bool value (int16_t x) override
 Adds x to the output.
 
bool value (uint16_t x) override
 Adds x to the output.
 
bool value (int32_t x) override
 Adds x to the output.
 
bool value (uint32_t x) override
 Adds x to the output.
 
bool value (int64_t x) override
 Adds x to the output.
 
bool value (uint64_t x) override
 Adds x to the output.
 
bool value (float x) override
 Adds x to the output.
 
bool value (double x) override
 Adds x to the output.
 
bool value (long double x) override
 Adds x to the output.
 
bool value (std::string_view x) override
 Adds x to the output.
 
bool value (const std::u16string &x) override
 Adds x to the output.
 
bool value (const std::u32string &x) override
 Adds x to the output.
 
bool value (span< const std::byte > x) override
 Adds x as raw byte block to the output.
 
- Public Member Functions inherited from caf::serializer
 serializer (actor_system &sys) noexcept
 
 serializer (execution_unit *ctx=nullptr) noexcept
 
auto context () const noexcept
 
bool has_human_readable_format () const noexcept
 
virtual bool begin_object (type_id_t type, std::string_view name)=0
 Begins processing of an object.
 
virtual bool end_object ()=0
 Ends processing of an object.
 
virtual bool begin_field (std::string_view)=0
 
virtual bool begin_field (std::string_view name, bool is_present)=0
 
virtual bool begin_field (std::string_view name, span< const type_id_t > types, size_t index)=0
 
virtual bool begin_field (std::string_view name, bool is_present, span< const type_id_t > types, size_t index)=0
 
virtual bool end_field ()=0
 
virtual bool begin_tuple (size_t size)=0
 Begins processing of a tuple.
 
virtual bool end_tuple ()=0
 Ends processing of a tuple.
 
virtual bool begin_key_value_pair ()
 Begins processing of a tuple with two elements, whereas the first element represents the key in an associative array.
 
virtual bool end_key_value_pair ()
 Ends processing of a key-value pair after both values were written.
 
virtual bool begin_sequence (size_t size)=0
 Begins processing of a sequence. Saves the size to the underlying storage.
 
virtual bool end_sequence ()=0
 Ends processing of a sequence.
 
virtual bool begin_associative_array (size_t size)
 Begins processing of an associative array (map).
 
virtual bool end_associative_array ()
 Ends processing of an associative array (map).
 
virtual bool value (std::byte x)=0
 Adds x to the output.
 
virtual bool value (bool x)=0
 Adds x to the output.
 
virtual bool value (int8_t x)=0
 Adds x to the output.
 
virtual bool value (uint8_t x)=0
 Adds x to the output.
 
virtual bool value (int16_t x)=0
 Adds x to the output.
 
virtual bool value (uint16_t x)=0
 Adds x to the output.
 
virtual bool value (int32_t x)=0
 Adds x to the output.
 
virtual bool value (uint32_t x)=0
 Adds x to the output.
 
virtual bool value (int64_t x)=0
 Adds x to the output.
 
virtual bool value (uint64_t x)=0
 Adds x to the output.
 
template<class T >
std::enable_if_t< std::is_integral_v< T >, bool > value (T x)
 Adds x to the output.
 
virtual bool value (float x)=0
 Adds x to the output.
 
virtual bool value (double x)=0
 Adds x to the output.
 
virtual bool value (long double x)=0
 Adds x to the output.
 
virtual bool value (std::string_view x)=0
 Adds x to the output.
 
virtual bool value (const std::u16string &x)=0
 Adds x to the output.
 
virtual bool value (const std::u32string &x)=0
 Adds x to the output.
 
virtual bool value (span< const std::byte > x)=0
 Adds x as raw byte block to the output.
 
virtual bool list (const std::vector< bool > &xs)
 Adds each boolean in xs to the output.
 

Static Public Attributes

static constexpr bool skip_empty_fields_default = true
 The default value for skip_empty_fields().
 
static constexpr bool skip_object_type_annotation_default = false
 The default value for skip_object_type_annotation().
 
static constexpr std::string_view field_type_suffix_default = "-type"
 The value value for field_type_suffix().
 

Additional Inherited Members

- Protected Attributes inherited from caf::serializer
execution_unitcontext_
 Provides access to the ::proxy_registry and to the ::actor_system.
 
bool has_human_readable_format_ = false
 Configures whether client code should assume human-readable output.
 

Detailed Description

Serializes an inspectable object to a JSON-formatted string.

Member Enumeration Documentation

◆ type

enum class caf::json_writer::type : uint8_t
strong

Reflects the structure of JSON objects according to ECMA-404.

This enum skips types such as members or value since they are not needed to generate JSON.

Enumerator
object 

Can morph into any other type except member.

member 

Contains any number of members.

key 

A single key-value pair.

array 

The key of a field.

string 

Contains any number of elements.

number 

A character sequence (terminal type).

boolean 

An integer or floating point (terminal type).

null 

Either "true" or "false" (terminal type).

Member Function Documentation

◆ begin_associative_array()

bool caf::json_writer::begin_associative_array ( size_t  size)
overridevirtual

Begins processing of an associative array (map).

Note
the default implementation calls begin_sequence(size).

Reimplemented from caf::serializer.

◆ begin_field() [1/4]

bool caf::json_writer::begin_field ( std::string_view  name,
bool  is_present 
)
overridevirtual

Implements caf::serializer.

◆ begin_field() [2/4]

bool caf::json_writer::begin_field ( std::string_view  name,
bool  is_present,
span< const type_id_t types,
size_t  index 
)
overridevirtual

Implements caf::serializer.

◆ begin_field() [3/4]

bool caf::json_writer::begin_field ( std::string_view  name,
span< const type_id_t types,
size_t  index 
)
overridevirtual

Implements caf::serializer.

◆ begin_field() [4/4]

bool caf::json_writer::begin_field ( std::string_view  name)
overridevirtual

Implements caf::serializer.

◆ begin_key_value_pair()

bool caf::json_writer::begin_key_value_pair ( )
overridevirtual

Begins processing of a tuple with two elements, whereas the first element represents the key in an associative array.

Note
the default implementation calls begin_tuple(2).

Reimplemented from caf::serializer.

◆ begin_object()

bool caf::json_writer::begin_object ( type_id_t  type,
std::string_view  name 
)
overridevirtual

Begins processing of an object.

May save the type information to the underlying storage to allow a deserializer to retrieve and check the type information for data formats that provide deserialization.

Implements caf::serializer.

◆ begin_sequence()

bool caf::json_writer::begin_sequence ( size_t  size)
overridevirtual

Begins processing of a sequence. Saves the size to the underlying storage.

Implements caf::serializer.

◆ begin_tuple()

bool caf::json_writer::begin_tuple ( size_t  size)
overridevirtual

Begins processing of a tuple.

Implements caf::serializer.

◆ end_associative_array()

bool caf::json_writer::end_associative_array ( )
overridevirtual

Ends processing of an associative array (map).

Note
the default implementation calls end_sequence().

Reimplemented from caf::serializer.

◆ end_field()

bool caf::json_writer::end_field ( )
overridevirtual

Implements caf::serializer.

◆ end_key_value_pair()

bool caf::json_writer::end_key_value_pair ( )
overridevirtual

Ends processing of a key-value pair after both values were written.

Note
the default implementation calls end_tuple().

Reimplemented from caf::serializer.

◆ end_object()

bool caf::json_writer::end_object ( )
overridevirtual

Ends processing of an object.

Implements caf::serializer.

◆ end_sequence()

bool caf::json_writer::end_sequence ( )
overridevirtual

Ends processing of a sequence.

Implements caf::serializer.

◆ end_tuple()

bool caf::json_writer::end_tuple ( )
overridevirtual

Ends processing of a tuple.

Implements caf::serializer.

◆ field_type_suffix()

std::string_view caf::json_writer::field_type_suffix ( ) const
noexcept

Returns the suffix for generating type annotation fields for variant fields.

For example, CAF inserts field called "@foo${field_type_suffix}" for a variant field called "foo".

◆ indentation()

void caf::json_writer::indentation ( size_t  factor)
noexcept

Sets the indentation level.

Parameters
factorThe number of spaces to add to each level of indentation. A value of 0 (the default) disables indentation, printing the entire JSON output into a single line.

◆ reset()

void caf::json_writer::reset ( )

Removes all characters from the buffer and restores the writer to its initial state.

Warning
Invalidates all string views into the buffer.

◆ str()

std::string_view caf::json_writer::str ( ) const
noexcept

Returns a string view into the internal buffer.

Warning
This view becomes invalid when calling any non-const member function on the writer object.

◆ value() [1/17]

bool caf::json_writer::value ( bool  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [2/17]

bool caf::json_writer::value ( const std::u16string &  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [3/17]

bool caf::json_writer::value ( const std::u32string &  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [4/17]

bool caf::json_writer::value ( double  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [5/17]

bool caf::json_writer::value ( float  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [6/17]

bool caf::json_writer::value ( int16_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [7/17]

bool caf::json_writer::value ( int32_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [8/17]

bool caf::json_writer::value ( int64_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [9/17]

bool caf::json_writer::value ( int8_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [10/17]

bool caf::json_writer::value ( long double  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [11/17]

bool caf::json_writer::value ( span< const std::byte >  x)
overridevirtual

Adds x as raw byte block to the output.

Parameters
xThe byte sequence.
Returns
A non-zero error code on failure, sec::success otherwise.

Implements caf::serializer.

◆ value() [12/17]

bool caf::json_writer::value ( std::byte  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [13/17]

bool caf::json_writer::value ( std::string_view  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [14/17]

bool caf::json_writer::value ( uint16_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [15/17]

bool caf::json_writer::value ( uint32_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [16/17]

bool caf::json_writer::value ( uint64_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.

◆ value() [17/17]

bool caf::json_writer::value ( uint8_t  x)
overridevirtual

Adds x to the output.

Parameters
xA value for a builtin type.
Returns
true on success, false otherwise.

Implements caf::serializer.


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