JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
Serialization

Serialization components are responsible for conversion between a C++ type and JSON, as a value or as a stream of tokens. More...

Classes

class  jsonv::reader
 A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON ast_node sequence. More...
 
class  jsonv::adapter
 An adapter is both a deserializer and a serializer. More...
 
class  jsonv::adapter_for< T >
 An adapter for the type T. More...
 
class  jsonv::value_adapter_for< T >
 A base for adapters written against the older value -based interface, in both directions. More...
 
class  jsonv::container_adapter< TContainer >
 An adapter for container types. More...
 
class  jsonv::context
 Provides extra information to routines used for deserialization and serialization. More...
 
class  jsonv::deserialization_error
 Exception thrown if there is any problem running deserialize. More...
 
class  jsonv::deserialize_options
 Configuration for various deserialization options. This becomes part of the deserialization_context. More...
 
class  jsonv::deserializer
 A deserializer holds the method for converting JSON source into an arbitrary C++ type. More...
 
class  jsonv::deserialization_context
 Provides extra information to routines used for deserialization, collects the problems they encounter, and tracks where in the document they are. More...
 
class  jsonv::deserializer_construction< T >
 A deserializer for a type T with a deserializing constructor. More...
 
class  jsonv::deserializer_for< T >
 A deserializer for type T. More...
 
class  jsonv::enum_adapter< TEnum, FEnumComp, FValueComp >
 An adapter for enumeration types. More...
 
class  jsonv::duplicate_type_error
 Exception thrown if an insertion of a deserializer or serializer into a formats is attempted, but there is already a deserializer or serializer for that type. More...
 
class  jsonv::no_deserializer
 Thrown when formats::deserialize does not have a deserializer for the provided type. More...
 
class  jsonv::no_serializer
 Thrown when formats::serialize does not have a serializer for the type it was asked to serialize. More...
 
class  jsonv::formats
 Simply put, this class is a collection of deserializer and serializer instances. More...
 
class  jsonv::function_adapter< T, FDeserialize, FSerialize >
 An adapter which calls one function to deserialize and another to serialize. More...
 
class  jsonv::function_deserializer< T, FDeserialize >
 A deserializer which calls a function to perform deserialization. More...
 
class  jsonv::function_serializer< T, FSerialize >
 A serializer which calls a function to perform serialization. More...
 
class  jsonv::optional_adapter< TOptional >
 An adapter for optional-like types. More...
 
class  jsonv::polymorphic_adapter< TPointer >
 An adapter which can create polymorphic types. More...
 
class  jsonv::serialization_error
 Exception thrown if there is any problem running serialize or to_json. More...
 
class  jsonv::serialization_context
 Provides extra information to routines used for serialization: the formats to find other serializers in, and the version and user data the caller asked for. More...
 
class  jsonv::serializer
 A serializer holds the method for converting an arbitrary C++ type into JSON, written token by token into a writer. More...
 
class  jsonv::serializer_for< T >
 A serializer for type T. More...
 
class  jsonv::value_serializer_for< T >
 A base for serializers written against the older value -based interface. More...
 
class  jsonv::wrapper_adapter< TWrapper >
 An adapter for "wrapper" types. More...
 
class  jsonv::writer
 A writer instance writes a JSON ast_node sequence to some form of sink: an encoder, which turns the tokens into text or into whatever else it builds. More...
 

Typedefs

using jsonv::demangle_function = std::function< std::string(std::string_view source)>
 Type of function used in setting a custom demangler.
 
template<typename TEnum , typename FEnumComp = std::less<TEnum>>
using jsonv::enum_adapter_icase = enum_adapter< TEnum, FEnumComp, value_less_icase >
 An adapter for enumeration types which ignores the case when deserializing from JSON.
 

Enumerations

enum class  jsonv::duplicate_type_action : unsigned char { duplicate_type_action::ignore , duplicate_type_action::replace , duplicate_type_action::exception }
 The action to take when an insertion of a deserializer or serializer into a formats is attempted, but there is alredy a deserializer or serializer for that type. More...
 
enum class  jsonv::keyed_subtype_action : unsigned char { keyed_subtype_action::none , keyed_subtype_action::check , keyed_subtype_action::insert }
 What to do when serializing a keyed subtype of a polymorphic_adapter. More...
 

Functions

JSONV_PUBLIC std::string jsonv::demangle (std::string_view source)
 Convert the input source from a mangled type into a human-friendly version.
 
JSONV_PUBLIC void jsonv::set_demangle_function (demangle_function func)
 Sets the global demangle function.
 
JSONV_PUBLIC void jsonv::reset_demangle_function ()
 Resets the demangle function to the default.
 
JSONV_PUBLIC std::string jsonv::current_exception_type_name ()
 Get the demangled type name of the current exception.
 
JSONV_PUBLIC value jsonv::read_value (reader &from)
 Consume the JSON subtree under from starting at reader::current and return it as a fully materialised value tree.
 
JSONV_PUBLIC value jsonv::read_value (deserialization_context &context, reader &from)
 read_value for a deserializer.
 
template<typename T >
T jsonv::deserialize (const value &from, const formats &fmts)
 Deserialize a C++ value from from using the provided fmts.
 
template<typename T >
T jsonv::deserialize (const value &from, const formats &fmts, const deserialize_options &options)
 Deserialize a C++ value from from using the provided fmts and options.
 
template<typename T >
T jsonv::deserialize (const value &from)
 Deserialize a C++ value from from using jsonv::formats::global().
 
template<typename T >
T jsonv::deserialize (const value &from, const deserialize_options &options)
 Deserialize a C++ value from from using jsonv::formats::global() and the provided options.
 
template<typename FDeserialize , typename FSerialize >
auto jsonv::make_adapter (FDeserialize deserialize_, FSerialize serialize_) -> function_adapter< detail::deserialize_function_result_t< FDeserialize >, FDeserialize, FSerialize >
 Create an adapter from deserialize_ and serialize_, deducing the adapted type from what deserialize_ returns.
 
template<typename FDeserialize >
auto jsonv::make_deserializer (FDeserialize func) -> function_deserializer< detail::deserialize_function_result_t< FDeserialize >, FDeserialize >
 Create a deserializer from func, deducing what it deserializes from its return type.
 
template<typename T , typename FSerialize >
function_serializer< T, FSerialize > jsonv::make_serializer (FSerialize func)
 Create a serializer for T from func, which may take any of the shapes detail::invoke_serialize accepts.
 
template<typename T >
value jsonv::to_json (const T &from, const formats &fmts)
 Encode a JSON value from from using the provided fmts.
 
template<typename T >
value jsonv::to_json (const T &from)
 Encode a JSON value from from using jsonv::formats::global().
 
template<typename T >
std::string jsonv::serialize (const T &from, const formats &fmts=formats::global())
 Serialize from into JSON text using fmts (by default jsonv::formats::global()).
 
template<typename T >
std::string jsonv::serialize (const T &from, const serialization_context &context)
 Serialize from into JSON text through a context the caller built, exactly as the overload above does with one built from formats.
 
template<typename T >
void jsonv::serialize (const T &from, std::ostream &to, const formats &fmts=formats::global())
 Serialize from into to as JSON text using fmts (by default jsonv::formats::global()).
 
template<typename T >
void jsonv::serialize (const T &from, std::ostream &to, const serialization_context &context)
 Serialize from into to as JSON text through a context the caller built, exactly as the overload above does with one built from formats.
 
template<typename T >
void jsonv::serialize (const T &from, writer &to, const formats &fmts=formats::global())
 Serialize from into to using fmts (by default jsonv::formats::global()), as one value where the writer is: at the root, as the next element of an open array, or as the value of the key just written.
 
template<typename T >
void jsonv::serialize (const T &from, writer &to, const serialization_context &context)
 Serialize from into to through a context the caller built, as one value where the writer is, exactly as the overload above does with one built from formats.
 
template<typename T >
T jsonv::deserialization_context::deserialize (const value &from)
 Attempt to deserialize a T from the in-memory from using the formats associated with this context.
 
template<typename T >
T jsonv::deserialize (reader &from, const formats &fmts=formats::global(), const deserialize_options &options=deserialize_options())
 Deserialize a C++ value from a reader using fmts (by default jsonv::formats::global()) and options.
 
template<typename T >
T jsonv::deserialize (reader &from, const deserialize_options &options)
 Deserialize a C++ value from a reader using jsonv::formats::global() and the provided options.
 
template<typename T >
T jsonv::deserialize (reader &&from, const formats &fmts=formats::global(), const deserialize_options &options=deserialize_options())
 Deserialize a C++ value from a reader which may own its source, using fmts and options.
 
template<typename T >
T jsonv::deserialize (reader &&from, const deserialize_options &options)
 Deserialize a C++ value from a reader which may own its source, using jsonv::formats::global() and the provided options.
 
template<typename T >
T jsonv::deserialize (reader &from, deserialization_context &context)
 Deserialize a C++ value from a reader through a context the caller built, exactly as the overloads above do with one built from formats and deserialize_options.
 
template<typename T >
T jsonv::deserialize (reader &&from, deserialization_context &context)
 Deserialize a C++ value from a reader through a context the caller built, exactly as the overloads above do with one built from formats and deserialize_options.
 
template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize (TSource &&source, const formats &fmts=formats::global(), const deserialize_options &options=deserialize_options())
 Deserialize a C++ value directly from JSON source text, parsed with parse_opts, using fmts (by default jsonv::formats::global()) and options.
 
template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize (TSource &&source, const deserialize_options &options)
 Deserialize a C++ value from JSON source text using jsonv::formats::global() and the provided options.
 
template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize (TSource &&source, const parse_options &parse_opts, const formats &fmts=formats::global(), const deserialize_options &options=deserialize_options())
 Deserialize a C++ value from JSON source text parsed with parse_opts, using fmts and options.
 
template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize (TSource &&source, const parse_options &parse_opts, const deserialize_options &options)
 Deserialize a C++ value from JSON source text parsed with parse_opts, using jsonv::formats::global() and the provided options.
 
template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize (TSource &&source, deserialization_context &context)
 Deserialize a C++ value directly from JSON source text, parsed with parse_opts where they are given, through a context the caller built.
 
template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize (TSource &&source, const parse_options &parse_opts, deserialization_context &context)
 Deserialize a C++ value directly from JSON source text, parsed with parse_opts where they are given, through a context the caller built.
 
template<typename T >
T jsonv::deserialize (std::istream &source, const formats &fmts=formats::global(), const deserialize_options &options=deserialize_options())
 Deserialize a C++ value from the JSON in what is left of the source stream, parsed with parse_opts, using fmts (by default jsonv::formats::global()) and options.
 
template<typename T >
T jsonv::deserialize (std::istream &source, const deserialize_options &options)
 Deserialize a C++ value from the JSON in the source stream using jsonv::formats::global() and the provided options.
 
template<typename T >
T jsonv::deserialize (std::istream &source, const parse_options &parse_opts, const formats &fmts=formats::global(), const deserialize_options &options=deserialize_options())
 Deserialize a C++ value from the JSON in the source stream parsed with parse_opts, using fmts and options.
 
template<typename T >
T jsonv::deserialize (std::istream &source, const parse_options &parse_opts, const deserialize_options &options)
 Deserialize a C++ value from the JSON in the source stream parsed with parse_opts, using jsonv::formats::global() and the provided options.
 
template<typename T >
T jsonv::deserialize (std::istream &source, deserialization_context &context)
 Deserialize a C++ value from the JSON in what is left of the source stream, parsed with parse_opts where they are given, through a context the caller built.
 
template<typename T >
T jsonv::deserialize (std::istream &source, const parse_options &parse_opts, deserialization_context &context)
 Deserialize a C++ value from the JSON in what is left of the source stream, parsed with parse_opts where they are given, through a context the caller built.
 

Detailed Description

Serialization components are responsible for conversion between a C++ type and JSON, as a value or as a stream of tokens.

Typedef Documentation

◆ demangle_function

using jsonv::demangle_function = typedef std::function<std::string (std::string_view source)>

Type of function used in setting a custom demangler.

See also
demangle
set_demangle_function

Definition at line 39 of file demangle.hpp.

◆ enum_adapter_icase

template<typename TEnum , typename FEnumComp = std::less<TEnum>>
using jsonv::enum_adapter_icase = typedef enum_adapter<TEnum, FEnumComp, value_less_icase>

An adapter for enumeration types which ignores the case when deserializing from JSON.

See also
enum_adapter

Definition at line 291 of file enum_adapter.hpp.

Enumeration Type Documentation

◆ duplicate_type_action

enum class jsonv::duplicate_type_action : unsigned char
strong

The action to take when an insertion of a deserializer or serializer into a formats is attempted, but there is alredy a deserializer or serializer for that type.

Enumerator
ignore 

The existing deserializer or serializer should be kept, but no exception should be thrown.

replace 

The new deserializer or serializer should be inserted, and no exception should be thrown.

exception 

A duplicate_type_error should be thrown.

Definition at line 34 of file formats.hpp.

◆ keyed_subtype_action

enum class jsonv::keyed_subtype_action : unsigned char
strong

What to do when serializing a keyed subtype of a polymorphic_adapter.

See polymorphic_adapter::add_subtype_keyed. Both check and insert build the subtype as a value before writing it, which polymorphic_adapter describes.

Enumerator
none 

Don't do any checking or insertion of the expected key/value pair.

check 

Ensure the correct key/value pair was inserted by serialization. Throws std::runtime_error if it wasn't.

insert 

Insert the correct key/value pair as part of serialization.

Throws std::runtime_error if the key is already present.

Definition at line 38 of file polymorphic_adapter.hpp.

Function Documentation

◆ current_exception_type_name()

JSONV_PUBLIC std::string jsonv::current_exception_type_name ( )

Get the demangled type name of the current exception.

This is meant to be called from catch (...) blocks to attempt to get more information about the exception type when it doesn't derive from a known entity.

Returns
The demangled type name of the current exception or "unknown" if this cannot be discovered. Failure to discover is not an error – this can happen if the exception is foreign to C++ (it does not have a std::type_info implementation) or if discovery is not known for this platform.

◆ demangle()

JSONV_PUBLIC std::string jsonv::demangle ( std::string_view  source)

Convert the input source from a mangled type into a human-friendly version.

This is used by formats (and the associated serialization functions) to give more user-friendly names in type errors.

See also
demangle_function
set_demangle_function

◆ deserialize() [1/23]

template<typename T >
T jsonv::deserialization_context::deserialize ( const value &  from)

Attempt to deserialize a T from the in-memory from using the formats associated with this context.

This runs the same pipeline as the reader overload by walking from through a reader::from_value, and reports failure by throwing rather than by returning. It is how an adapter written against the older value-based interface reaches the rest of the pipeline. To deserialize part of from, name the part – deserialize<T>(from.at("a")) – under a path_scope saying where it is.

Exceptions
deserialization_errorif anything goes wrong when attempting to deserialize a value.
See also
value_adapter_for

Definition at line 1346 of file deserialize.hpp.

◆ deserialize() [2/23]

template<typename T >
T jsonv::deserialize ( const value &  from)

Deserialize a C++ value from from using jsonv::formats::global().

Definition at line 1373 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [3/23]

template<typename T >
T jsonv::deserialize ( const value &  from,
const deserialize_options &  options 
)

Deserialize a C++ value from from using jsonv::formats::global() and the provided options.

Definition at line 1382 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [4/23]

template<typename T >
T jsonv::deserialize ( const value &  from,
const formats &  fmts 
)

Deserialize a C++ value from from using the provided fmts.

Definition at line 1355 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [5/23]

template<typename T >
T jsonv::deserialize ( const value &  from,
const formats &  fmts,
const deserialize_options &  options 
)

Deserialize a C++ value from from using the provided fmts and options.

Definition at line 1364 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [6/23]

template<typename T >
T jsonv::deserialize ( reader &&  from,
const deserialize_options &  options 
)

Deserialize a C++ value from a reader which may own its source, using jsonv::formats::global() and the provided options.

Definition at line 1447 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [7/23]

template<typename T >
T jsonv::deserialize ( reader &&  from,
const formats &  fmts = formats::global(),
const deserialize_options &  options = deserialize_options() 
)

Deserialize a C++ value from a reader which may own its source, using fmts and options.

Definition at line 1429 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [8/23]

template<typename T >
T jsonv::deserialize ( reader &&  from,
deserialization_context &  context 
)

Deserialize a C++ value from a reader through a context the caller built, exactly as the overloads above do with one built from formats and deserialize_options.

Everything context was created with applies: its formats and deserialize_options, and also what the overloads above have no way to be given – the version, user data and base path its deserializers see, and the deserialization_context::source_name its problems are reported in. A failed call throws the problems it recorded and takes them off context, which is left holding what it held before.

A deserialization_context is single-use, so build one for each document and do not hand this the one a deserializer was given – whatever that deserialization has in progress would be applied to a document it knows nothing about.

Exceptions
deserialization_errorfor the same reasons as the overloads above.

Definition at line 1477 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [9/23]

template<typename T >
T jsonv::deserialize ( reader &  from,
const deserialize_options &  options 
)

Deserialize a C++ value from a reader using jsonv::formats::global() and the provided options.

Definition at line 1421 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [10/23]

template<typename T >
T jsonv::deserialize ( reader &  from,
const formats &  fmts = formats::global(),
const deserialize_options &  options = deserialize_options() 
)

Deserialize a C++ value from a reader using fmts (by default jsonv::formats::global()) and options.

A reader on ast_node_type::document_start – a freshly-created one – is read as a whole document. It is checked with reader::validate first, so a source which did not parse is reported as that rather than as whatever a deserializer made of the ast_node_type::error node it ran into; its document_start is stepped over, so neither the caller nor any deserializer has to; and the value read must be the whole document, so the reader is left on ast_node_type::document_end. A reader the caller has already positioned gets none of this: the value under its cursor is deserialized and the cursor left one past it, as reader::next_value would, whatever surrounds it. The exception is a reader on an ast_node_type::error node, which has no value to deserialize and is reported as the parse failure it is.

Anything deserialized as a view of the source – a std::string_view – views the reader's storage. Through the rvalue overloads, a reader which reader::owns_source dies with the call, so such views are refused; one over storage the caller owns, like reader(std::string_view), is viewed as usual.

Exceptions
deserialization_errorif the source did not parse, the value could not be deserialized or, for a whole document, something follows the value.

Definition at line 1409 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [11/23]

template<typename T >
T jsonv::deserialize ( reader &  from,
deserialization_context &  context 
)

Deserialize a C++ value from a reader through a context the caller built, exactly as the overloads above do with one built from formats and deserialize_options.

Everything context was created with applies: its formats and deserialize_options, and also what the overloads above have no way to be given – the version, user data and base path its deserializers see, and the deserialization_context::source_name its problems are reported in. A failed call throws the problems it recorded and takes them off context, which is left holding what it held before.

A deserialization_context is single-use, so build one for each document and do not hand this the one a deserializer was given – whatever that deserialization has in progress would be applied to a document it knows nothing about.

Exceptions
deserialization_errorfor the same reasons as the overloads above.

Definition at line 1470 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [12/23]

template<typename T >
T jsonv::deserialize ( std::istream &  source,
const deserialize_options &  options 
)

Deserialize a C++ value from the JSON in the source stream using jsonv::formats::global() and the provided options.

Definition at line 1618 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [13/23]

template<typename T >
T jsonv::deserialize ( std::istream &  source,
const formats &  fmts = formats::global(),
const deserialize_options &  options = deserialize_options() 
)

Deserialize a C++ value from the JSON in what is left of the source stream, parsed with parse_opts, using fmts (by default jsonv::formats::global()) and options.

For example, to deserialize a file:

std::ifstream file("file.json");
my_type out = jsonv::deserialize<my_type>(file);

The stream is read to its end, as parse(std::istream&) reads it, into a reader(std::istream&) which lives as long as this call. That text is then read exactly as the text overloads read a std::string rvalue: as a whole document, with views of it refused, since it is freed when this returns. A parse_error position counts from where source was when this was called.

Exceptions
deserialization_errorif what is left of source is not valid JSON, the value could not be deserialized, or something follows it.
std::invalid_argumentif parse_opts asks for a parse_options::max_structure_depth beyond the limit, as jsonv::parse does.

Definition at line 1606 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [14/23]

template<typename T >
T jsonv::deserialize ( std::istream &  source,
const parse_options &  parse_opts,
const deserialize_options &  options 
)

Deserialize a C++ value from the JSON in the source stream parsed with parse_opts, using jsonv::formats::global() and the provided options.

Definition at line 1640 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [15/23]

template<typename T >
T jsonv::deserialize ( std::istream &  source,
const parse_options &  parse_opts,
const formats &  fmts = formats::global(),
const deserialize_options &  options = deserialize_options() 
)

Deserialize a C++ value from the JSON in the source stream parsed with parse_opts, using fmts and options.

Definition at line 1627 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [16/23]

template<typename T >
T jsonv::deserialize ( std::istream &  source,
const parse_options &  parse_opts,
deserialization_context &  context 
)

Deserialize a C++ value from the JSON in what is left of the source stream, parsed with parse_opts where they are given, through a context the caller built.

source is read exactly as the overloads above read it, and context is used as the reader overload taking a deserialization_context uses it: everything it was created with applies, and the problems a failed call throws are taken off it. Build one for each document, and name the file it is read from as its deserialization_context::source_name to have the problems say which file they are in.

Exceptions
deserialization_errorif what is left of source is not valid JSON, the value could not be deserialized, or something follows it.
std::invalid_argumentif parse_opts asks for a parse_options::max_structure_depth beyond the limit, as jsonv::parse does.

Definition at line 1669 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [17/23]

template<typename T >
T jsonv::deserialize ( std::istream &  source,
deserialization_context &  context 
)

Deserialize a C++ value from the JSON in what is left of the source stream, parsed with parse_opts where they are given, through a context the caller built.

source is read exactly as the overloads above read it, and context is used as the reader overload taking a deserialization_context uses it: everything it was created with applies, and the problems a failed call throws are taken off it. Build one for each document, and name the file it is read from as its deserialization_context::source_name to have the problems say which file they are in.

Exceptions
deserialization_errorif what is left of source is not valid JSON, the value could not be deserialized, or something follows it.
std::invalid_argumentif parse_opts asks for a parse_options::max_structure_depth beyond the limit, as jsonv::parse does.

Definition at line 1662 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [18/23]

template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize ( TSource &&  source,
const deserialize_options &  options 
)

Deserialize a C++ value from JSON source text using jsonv::formats::global() and the provided options.

Definition at line 1522 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [19/23]

template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize ( TSource &&  source,
const formats &  fmts = formats::global(),
const deserialize_options &  options = deserialize_options() 
)

Deserialize a C++ value directly from JSON source text, parsed with parse_opts, using fmts (by default jsonv::formats::global()) and options.

source is anything which converts to std::string_view: a string literal, a std::string, a std::string_view. Note what that means for a C++ string: it is JSON text to be parsed, not a JSON string, so deserialize<std::string>(R"("fire")") is "fire" and deserialize<std::string>("fire") is a parse failure. Wrap it in a value – deserialize<std::string>(value("fire")) – to mean the string.

A std::string rvalue is taken over for the call and freed when it returns, so views of it are refused, as for a tree materialised during deserialization (see deserialization_context::source_is_temporary). Every other source is read where it is, without copying, and a std::string_view deserialized from it points into it.

This reads the whole document, exactly as the reader overloads do given a fresh reader.

Exceptions
deserialization_errorif source is not valid JSON, the value could not be deserialized, or something follows it.
std::invalid_argumentif parse_opts asks for a parse_options::max_structure_depth beyond the limit, as jsonv::parse does.

Definition at line 1510 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [20/23]

template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize ( TSource &&  source,
const parse_options &  parse_opts,
const deserialize_options &  options 
)

Deserialize a C++ value from JSON source text parsed with parse_opts, using jsonv::formats::global() and the provided options.

Definition at line 1549 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [21/23]

template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize ( TSource &&  source,
const parse_options &  parse_opts,
const formats &  fmts = formats::global(),
const deserialize_options &  options = deserialize_options() 
)

Deserialize a C++ value from JSON source text parsed with parse_opts, using fmts and options.

Definition at line 1535 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [22/23]

template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize ( TSource &&  source,
const parse_options &  parse_opts,
deserialization_context &  context 
)

Deserialize a C++ value directly from JSON source text, parsed with parse_opts where they are given, through a context the caller built.

source is read exactly as the overloads above read it, a std::string rvalue included, and context is used as the reader overload taking a deserialization_context uses it: everything it was created with applies, and the problems a failed call throws are taken off it. Build one for each document.

Exceptions
deserialization_errorif source is not valid JSON, the value could not be deserialized, or something follows it.
std::invalid_argumentif parse_opts asks for a parse_options::max_structure_depth beyond the limit, as jsonv::parse does.

Definition at line 1579 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ deserialize() [23/23]

template<typename T , typename TSource >
requires std::convertible_to<TSource, std::string_view>
T jsonv::deserialize ( TSource &&  source,
deserialization_context &  context 
)

Deserialize a C++ value directly from JSON source text, parsed with parse_opts where they are given, through a context the caller built.

source is read exactly as the overloads above read it, a std::string rvalue included, and context is used as the reader overload taking a deserialization_context uses it: everything it was created with applies, and the problems a failed call throws are taken off it. Build one for each document.

Exceptions
deserialization_errorif source is not valid JSON, the value could not be deserialized, or something follows it.
std::invalid_argumentif parse_opts asks for a parse_options::max_structure_depth beyond the limit, as jsonv::parse does.

Definition at line 1571 of file deserialize.hpp.

+ Here is the call graph for this function:

◆ make_adapter()

template<typename FDeserialize , typename FSerialize >
auto jsonv::make_adapter ( FDeserialize  deserialize_,
FSerialize  serialize_ 
) -> function_adapter<detail::deserialize_function_result_t<FDeserialize>, FDeserialize, FSerialize>

Create an adapter from deserialize_ and serialize_, deducing the adapted type from what deserialize_ returns.

Definition at line 63 of file function_adapter.hpp.

◆ make_deserializer()

template<typename FDeserialize >
auto jsonv::make_deserializer ( FDeserialize  func) -> function_deserializer<detail::deserialize_function_result_t<FDeserialize>, FDeserialize>

Create a deserializer from func, deducing what it deserializes from its return type.

Definition at line 59 of file function_deserializer.hpp.

◆ make_serializer()

template<typename T , typename FSerialize >
function_serializer< T, FSerialize > jsonv::make_serializer ( FSerialize  func)

Create a serializer for T from func, which may take any of the shapes detail::invoke_serialize accepts.

Definition at line 58 of file function_serializer.hpp.

◆ read_value() [1/2]

JSONV_PUBLIC value jsonv::read_value ( deserialization_context &  context,
reader &  from 
)

read_value for a deserializer.

When a structure fails, which leaves the cursor past it, this also says so to context with deserialization_context::note_value_consumed – which is what lets a composite recovering from the failure resume at the next sibling rather than one sibling too far.

A repeated key is settled by context's deserialize_options::on_duplicate_key, as parse settles it for the same options: replace keeps the last value, ignore the first, and exception refuses the object.

A failure is placed where context says, as any other problem is: under its base path or a live deserialization_context::path_scope when it has one, followed by whatever lies below where the read began. With neither, the reader's own path stands – and a reader which has run out has none to give.

Exceptions
deserialization_errorfor a repeated key under deserialize_options::duplicate_key_action::exception, as well as for everything read_value throws it for.
+ Here is the call graph for this function:

◆ read_value() [2/2]

JSONV_PUBLIC value jsonv::read_value ( reader &  from)

Consume the JSON subtree under from starting at reader::current and return it as a fully materialised value tree.

On return from has advanced one position past the consumed subtree, exactly as reader::next_value would have left it for the same input. Every adapter reading a subtree has to agree on this, or subtrees get consumed twice or not at all. A leading ast_node_type::document_start is stepped over first, so this works on a freshly-created reader as well as on one positioned mid-document.

This is the bridge which lets adapters written against the older value -based interface keep working while the surrounding pipeline runs against a streaming reader.

A structure which fails part-way through is still stepped over, so from is left one past it just as success would have left it; a scalar which fails leaves from on it. A deserializer which lets the failure of a structure out has to say that the value is behind the cursor, or a composite recovering from it steps over the following sibling as well – which is what the overload taking a deserialization_context is for.

An object which repeats a key keeps the last of its values, which is deserialize_options::duplicate_key_action's default. The overload taking a deserialization_context does what its options say instead.

Exceptions
deserialization_errorif from is not positioned on a value or the document ends part-way through one.
std::invalid_argumentif a number has no finite double to round to, such as 1e400.
parse_errorif a string holds an escape which does not decode, such as an unpaired \uD800.
See also
value_adapter_for

◆ reset_demangle_function()

JSONV_PUBLIC void jsonv::reset_demangle_function ( )

Resets the demangle function to the default.

See also
set_demangle_function

◆ serialize() [1/6]

template<typename T >
std::string jsonv::serialize ( const T &  from,
const formats &  fmts = formats::global() 
)

Serialize from into JSON text using fmts (by default jsonv::formats::global()).

The text is one whole document, written compactly with ostream_encoder::ensure_ascii on, as to_string writes a value. It is the text of to_string(to_json(from, fmts)) with one difference: an object's members are in the order its serializer wrote them – declaration order, for a type described with the serialization builder DSL – where a value keeps them sorted by key. Nothing is built in between, so a container of a million elements is written without a million-node tree. To pretty-print, or to write well-formed UTF-8 as it is, construct the encoder yourself and serialize into a writer over it.

from is serialized as the type it is, as for to_json: a string literal is an array of char, which has no serializer, so pass a std::string_view.

Exceptions
serialization_errorif a serializer could not be found or failed, as serialization_context::serialize throws it; or if the serializer for T wrote anything but one whole value – nothing, a structure left open, or a second value after the first – since a JSON document is exactly one value.

Definition at line 206 of file serialize.hpp.

◆ serialize() [2/6]

template<typename T >
std::string jsonv::serialize ( const T &  from,
const serialization_context &  context 
)

Serialize from into JSON text through a context the caller built, exactly as the overload above does with one built from formats.

Everything context was created with applies: its formats, and also what the overload above has no way to be given – the version and user data its serializers see. The serialization builder DSL's since and until ask for that version. A serialization_context holds nothing about any one serialization, so one may serve any number of them.

Exceptions
serialization_errorfor the same reasons as the overload above.

Definition at line 222 of file serialize.hpp.

◆ serialize() [3/6]

template<typename T >
void jsonv::serialize ( const T &  from,
std::ostream &  to,
const formats &  fmts = formats::global() 
)

Serialize from into to as JSON text using fmts (by default jsonv::formats::global()).

What is written is exactly the text the overload returning a std::string returns: one whole document, with nothing before or after it. Nothing separates it from a document written into the same stream next, so write a separator yourself if the stream is to be read back.

Exceptions
serialization_errorfor the same reasons as the overload returning a std::string. What was written before the failure stays in to.

Definition at line 236 of file serialize.hpp.

◆ serialize() [4/6]

template<typename T >
void jsonv::serialize ( const T &  from,
std::ostream &  to,
const serialization_context &  context 
)

Serialize from into to as JSON text through a context the caller built, exactly as the overload above does with one built from formats.

Everything context was created with applies, as for the overload returning a std::string which takes one.

Exceptions
serialization_errorfor the same reasons as the overload above. What was written before the failure stays in to.

Definition at line 248 of file serialize.hpp.

◆ serialize() [5/6]

template<typename T >
void jsonv::serialize ( const T &  from,
writer &  to,
const formats &  fmts = formats::global() 
)

Serialize from into to using fmts (by default jsonv::formats::global()), as one value where the writer is: at the root, as the next element of an open array, or as the value of the key just written.

This is the mirror of deserializing from a reader the caller has positioned. What surrounds the value is the caller's, so open an array once and serialize a million items into it. Unlike the overloads which own their writer, this does not check that to ends up holding a whole document.

Exceptions
serialization_erroras serialization_context::serialize throws it. What was written before the failure stays in to.

Definition at line 263 of file serialize.hpp.

◆ serialize() [6/6]

template<typename T >
void jsonv::serialize ( const T &  from,
writer &  to,
const serialization_context &  context 
)

Serialize from into to through a context the caller built, as one value where the writer is, exactly as the overload above does with one built from formats.

This is context.serialize(from, to), which is the spelling a serializer uses for its parts.

Exceptions
serialization_erroras serialization_context::serialize throws it. What was written before the failure stays in to.

Definition at line 276 of file serialize.hpp.

◆ set_demangle_function()

JSONV_PUBLIC void jsonv::set_demangle_function ( demangle_function  func)

Sets the global demangle function.

This controls the behavior of demangle – the provided func will be called by demangle.

See also
demangle

◆ to_json() [1/2]

template<typename T >
value jsonv::to_json ( const T &  from)

Encode a JSON value from from using jsonv::formats::global().

Definition at line 182 of file serialize.hpp.

◆ to_json() [2/2]

template<typename T >
value jsonv::to_json ( const T &  from,
const formats &  fmts 
)

Encode a JSON value from from using the provided fmts.

Definition at line 173 of file serialize.hpp.