JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
jsonv::value_encoder Class Referencefinal

An encoder which builds a value from the tokens it is given: the sink for a document which is produced token by token through a writer but is wanted as a tree. More...

#include <jsonv/encode.hpp>

+ Inheritance diagram for jsonv::value_encoder:
+ Collaboration diagram for jsonv::value_encoder:

Public Member Functions

 value_encoder ()
 Create an instance holding nothing.
 
 value_encoder (const value_encoder &)=delete
 
value_encoder & operator= (const value_encoder &)=delete
 
 value_encoder (value_encoder &&)=delete
 
value_encoder & operator= (value_encoder &&)=delete
 
value take () &&
 Hand out the document written so far.
 
- Public Member Functions inherited from jsonv::encoder
void encode (const jsonv::value &source)
 Encode some source value into this encoder, through write_tree.
 

Protected Member Functions

virtual void write_null () override
 Write the null value.
 
virtual void write_object_begin () override
 Write the opening of an object value.
 
virtual void write_object_end () override
 Write the closing of an object value.
 
virtual void write_object_key (std::string_view key) override
 Keep key for the member whose value comes next.
 
virtual void write_object_delimiter () override
 Does nothing: a tree has no punctuation.
 
virtual void write_array_begin () override
 Write the opening of an array value.
 
virtual void write_array_end () override
 Write the closing of an array value.
 
virtual void write_array_delimiter () override
 Does nothing: a tree has no punctuation.
 
virtual void write_string (std::string_view value) override
 The string is copied into the tree as it is, valid UTF-8 or not.
 
virtual void write_integer (std::int64_t value) override
 Write an integer value.
 
virtual void write_decimal (double value) override
 Kept as given: a value can hold a NaN or an infinity, so nothing is substituted for one.
 
virtual void write_boolean (bool value) override
 Write a boolean value.
 
virtual void write_tree (jsonv::value &&source) override
 Takes source as it is.
 
virtual void write_tree (const jsonv::value &source) override
 Copies source in whole.
 

Detailed Description

An encoder which builds a value from the tokens it is given: the sink for a document which is produced token by token through a writer but is wanted as a tree.

It is the mirror of reader::from_value, which hands a tree out as tokens.

jsonv::writer to(sink);
to.object_begin()
.key("a").integer(1)
.key("b").array_begin().string("x").string("y").array_end()
.object_end();
jsonv::value built = std::move(sink).take(); // {"a":1,"b":["x","y"]}
An encoder which builds a value from the tokens it is given: the sink for a document which is produce...
Definition encode.hpp:310
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:113
A writer instance writes a JSON ast_node sequence to some form of sink: an encoder,...
Definition writer.hpp:68

An object is built as a value keeps one, so a key which repeats within it keeps the value written last, as parse keeps it by default; a document built from tokens and the same document parsed from text select the same data. The encoder holds one document at a time: a second root value written before take replaces the first, as a repeated key does.

See also
writer
reader::from_value

Definition at line 308 of file encode.hpp.

Member Function Documentation

◆ take()

value jsonv::value_encoder::take ( ) &&

Hand out the document written so far.

This instance is left holding nothing, as it was constructed, so another document may follow.

Exceptions
std::logic_errorif an object or array is still open, or if nothing has been written at all. A JSON document is never empty, so a writer which has produced nothing has not produced null.

◆ write_array_begin()

virtual void jsonv::value_encoder::write_array_begin ( )
overrideprotectedvirtual

Write the opening of an array value.

[

Implements jsonv::encoder.

◆ write_array_delimiter()

virtual void jsonv::value_encoder::write_array_delimiter ( )
overrideprotectedvirtual

Does nothing: a tree has no punctuation.

Implements jsonv::encoder.

◆ write_array_end()

virtual void jsonv::value_encoder::write_array_end ( )
overrideprotectedvirtual

Write the closing of an array value.

]

Implements jsonv::encoder.

◆ write_boolean()

virtual void jsonv::value_encoder::write_boolean ( bool  value)
overrideprotectedvirtual

Write a boolean value.

true

Implements jsonv::encoder.

◆ write_decimal()

virtual void jsonv::value_encoder::write_decimal ( double  value)
overrideprotectedvirtual

Kept as given: a value can hold a NaN or an infinity, so nothing is substituted for one.

Implements jsonv::encoder.

◆ write_integer()

virtual void jsonv::value_encoder::write_integer ( std::int64_t  value)
overrideprotectedvirtual

Write an integer value.

902

Implements jsonv::encoder.

◆ write_null()

virtual void jsonv::value_encoder::write_null ( )
overrideprotectedvirtual

Write the null value.

JSONV_PUBLIC const value null
An instance with kind::null.

Implements jsonv::encoder.

◆ write_object_begin()

virtual void jsonv::value_encoder::write_object_begin ( )
overrideprotectedvirtual

Write the opening of an object value.

{

Implements jsonv::encoder.

◆ write_object_delimiter()

virtual void jsonv::value_encoder::write_object_delimiter ( )
overrideprotectedvirtual

Does nothing: a tree has no punctuation.

Implements jsonv::encoder.

◆ write_object_end()

virtual void jsonv::value_encoder::write_object_end ( )
overrideprotectedvirtual

Write the closing of an object value.

}

Implements jsonv::encoder.

◆ write_object_key()

virtual void jsonv::value_encoder::write_object_key ( std::string_view  key)
overrideprotectedvirtual

Keep key for the member whose value comes next.

Implements jsonv::encoder.

◆ write_string()

virtual void jsonv::value_encoder::write_string ( std::string_view  value)
overrideprotectedvirtual

The string is copied into the tree as it is, valid UTF-8 or not.

Implements jsonv::encoder.

◆ write_tree() [1/2]

virtual void jsonv::value_encoder::write_tree ( const jsonv::value &  source)
overrideprotectedvirtual

Copies source in whole.

A tree written as an lvalue – to_json of a value, or of anything holding one – costs what copying it does rather than a rebuild through the hooks above, which inserts each member and grows each array one element at a time.

Reimplemented from jsonv::encoder.

◆ write_tree() [2/2]

virtual void jsonv::value_encoder::write_tree ( jsonv::value &&  source)
overrideprotectedvirtual

Takes source as it is.

A tree handed over whole is placed where the next value goes without being rebuilt, which is what keeps a serializer on the value bridge linear in the depth of what it serializes.

Reimplemented from jsonv::encoder.


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