JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
jsonv::serialization_context Class Reference

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...

#include <jsonv/serialization/serialize.hpp>

+ Inheritance diagram for jsonv::serialization_context:
+ Collaboration diagram for jsonv::serialization_context:

Public Member Functions

 serialization_context ()
 Create a new instance using the default formats (formats::global).
 
 serialization_context (jsonv::formats fmt, std::optional< jsonv::version > ver=std::nullopt, const void *userdata=nullptr)
 Create a new instance using the given fmt, ver and userdata.
 
template<typename T >
void serialize (const T &from, writer &to) const
 Write from into to using the formats associated with this context.
 
void serialize (const std::type_info &type, const void *from, writer &to) const
 Write the object of the given type at from into to using the formats associated with this context.
 
template<typename T >
value to_json (const T &from) const
 Convenience function for converting a C++ object into a JSON value.
 
value to_json (const std::type_info &type, const void *from) const
 Dynamically convert a type into a JSON value.
 
- Public Member Functions inherited from jsonv::context
 context ()
 Create a new instance using the default formats (formats::global).
 
 context (jsonv::formats fmt, std::optional< jsonv::version > ver=std::nullopt, const void *userdata=nullptr)
 Create a new instance using the given fmt, ver and userdata.
 
const jsonv::formats & formats () const
 Get the formats object backing deserialization and encoding.
 
const std::optional< jsonv::version > & version () const
 Get the version this context was created with.
 
const void * user_data () const
 Get a pointer to arbitrary user data.
 

Detailed Description

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.

Unlike a deserialization_context, this is immutable, and one instance can serve any number of serializations at once. Nothing is recorded on it: a serializer's position in the document is its writer::current_path, and a failure is reported by throwing a serialization_error.

Definition at line 40 of file serialize.hpp.

Member Function Documentation

◆ serialize() [1/2]

void jsonv::serialization_context::serialize ( const std::type_info &  type,
const void *  from,
writer &  to 
) const

Write the object of the given type at from into to using the formats associated with this context.

This is what the overload above calls with the T it was asked for.

A serialization_error thrown by the serializer – a no_serializer included – is rethrown as it is, since it already says where it is and what was being serialized there. Anything else thrown is wrapped once in a serialization_error at to.current_path() naming type, with the original exception as its serialization_error::nested_ptr.

Exceptions
serialization_erroras above. The writer may have been given a prefix of the value by then: an entry point which owns its sink discards it, and one writing into a caller's writer leaves what was written where it is.

◆ serialize() [2/2]

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

Write from into to using the formats associated with this context.

This is the positioned primitive a composite calls for each of its parts: it writes 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 – and nothing else.

Exceptions
serialization_errorif the serializer for T cannot be found or throws. The writer may have been given a prefix of the value by then.

Definition at line 64 of file serialize.hpp.

+ Here is the call graph for this function:

◆ to_json() [1/2]

value jsonv::serialization_context::to_json ( const std::type_info &  type,
const void *  from 
) const

Dynamically convert a type into a JSON value.

This is what the overload above calls with the T it was asked for.

◆ to_json() [2/2]

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

Convenience function for converting a C++ object into a JSON value.

This runs the same pipeline as serialize, writing into a value_encoder and handing out what it built. It is how a serializer written against the older value -based interface serializes its parts, and what the free jsonv::to_json runs.

Exceptions
serialization_erroras serialize does, and also when the serializer wrote nothing or left a structure open, since a JSON document is never empty.

Definition at line 92 of file serialize.hpp.

+ Here is the call graph for this function:

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