A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
a11::data::SerializationRegistry Class Reference

#include <cpp/a11/data/serialization.h>

Classes

struct  DeserializerRegistration
 
struct  Impl
 
struct  SerializerRegistration
 

Public Types

template<typename T >
using Serializer = std::function< absl::StatusOr< Chunk >(const T &)>
 Callable turning a T into a Chunk.
 
template<typename T >
using Deserializer = std::function< absl::StatusOr< T >(const Chunk &)>
 Callable reconstructing a T from a Chunk.
 

Public Member Functions

 SerializationRegistry (bool register_defaults=false)
 Constructs a registry.
 
 ~SerializationRegistry ()
 
 SerializationRegistry (const SerializationRegistry &)=delete
 
SerializationRegistry & operator= (const SerializationRegistry &)=delete
 
template<typename T >
absl::Status RegisterSerializer (std::string type_name, const std::string &mimetype, Serializer< T > serializer)
 Registers a serializer for type T and an exact media type.
 
template<typename T >
absl::Status RegisterDeserializer (std::string type_name, const std::string &mimetype, Deserializer< T > deserializer)
 Registers a deserializer for type T and an exact media type.
 
template<typename T >
absl::Status Register (const std::string &type_name, const std::string &mimetype, Serializer< T > serializer, Deserializer< T > deserializer)
 Atomically registers a serializer/deserializer pair for T.
 
template<typename T >
absl::StatusOr< Chunk > ToChunk (const T &value, std::string_view mimetype={}) const
 Serializes value into a Chunk.
 
template<typename T >
absl::StatusOr< T > FromChunk (const Chunk &chunk, const std::vector< std::string > &mimetype_patterns={}) const
 Deserializes chunk into a value of type T.
 
absl::Status RegisterDefaults ()
 Installs the standard JSON and MessagePack codecs.
 
size_t serializer_count () const
 Number of registered serializers.
 
size_t deserializer_count () const
 Number of registered deserializers.
 

Member Typedef Documentation

◆ Deserializer

template<typename T >
using a11::data::SerializationRegistry::Deserializer = std::function<absl::StatusOr<T>(const Chunk&)>

Callable reconstructing a T from a Chunk.

◆ Serializer

template<typename T >
using a11::data::SerializationRegistry::Serializer = std::function<absl::StatusOr<Chunk>(const T&)>

Callable turning a T into a Chunk.

Constructor & Destructor Documentation

◆ SerializationRegistry() [1/2]

a11::data::SerializationRegistry::SerializationRegistry ( bool  register_defaults = false)
explicit

Constructs a registry.

Parameters
register_defaultsWhen true, install the JSON and MessagePack codecs; otherwise start empty.

◆ ~SerializationRegistry()

a11::data::SerializationRegistry::~SerializationRegistry ( )

◆ SerializationRegistry() [2/2]

a11::data::SerializationRegistry::SerializationRegistry ( const SerializationRegistry &  )
delete

Member Function Documentation

◆ deserializer_count()

size_t a11::data::SerializationRegistry::deserializer_count ( ) const

Number of registered deserializers.

◆ FromChunk()

template<typename T >
absl::StatusOr< T > a11::data::SerializationRegistry::FromChunk ( const Chunk &  chunk,
const std::vector< std::string > &  mimetype_patterns = {} 
) const
inline

Deserializes chunk into a value of type T.

Parameters
chunkChunk to decode.
mimetype_patternsOptional ordered media-type selectors (may use wildcards); the first that matches the chunk is used. When empty, the chunk's own mimetype selects the codec. A selector chooses the representation only – T is what decides the result type, so the chunk's own type parameter does not have to agree with it.
Returns
The decoded value, or an error when no codec matches or the data cannot be read as a T.

◆ operator=()

SerializationRegistry & a11::data::SerializationRegistry::operator= ( const SerializationRegistry &  )
delete

◆ Register()

template<typename T >
absl::Status a11::data::SerializationRegistry::Register ( const std::string &  type_name,
const std::string &  mimetype,
Serializer< T >  serializer,
Deserializer< T >  deserializer 
)
inline

Atomically registers a serializer/deserializer pair for T.

On failure to register the deserializer, the serializer added by this call is rolled back.

Parameters
type_nameWire tag identifying T in serialized chunks. Tags a format already describes (json, object, string, ...) are not written; the media type alone says as much.
mimetypeExact media type for both codecs.
serializerCallable converting a T to a Chunk.
deserializerCallable reconstructing a T from a Chunk.
Returns
OK on success, or the first error encountered.

◆ RegisterDefaults()

absl::Status a11::data::SerializationRegistry::RegisterDefaults ( )

Installs the standard JSON and MessagePack codecs.

◆ RegisterDeserializer()

template<typename T >
absl::Status a11::data::SerializationRegistry::RegisterDeserializer ( std::string  type_name,
const std::string &  mimetype,
Deserializer< T >  deserializer 
)
inline

Registers a deserializer for type T and an exact media type.

Parameters
type_nameWire tag identifying T in serialized chunks. Tags a format already describes (json, object, string, ...) are not written; the media type alone says as much.
mimetypeExact media type accepted by deserializer.
deserializerCallable reconstructing a T from a Chunk.
Returns
OK on success, or an error (e.g. when deserializer is empty).

◆ RegisterSerializer()

template<typename T >
absl::Status a11::data::SerializationRegistry::RegisterSerializer ( std::string  type_name,
const std::string &  mimetype,
Serializer< T >  serializer 
)
inline

Registers a serializer for type T and an exact media type.

Parameters
type_nameWire tag identifying T in serialized chunks. Tags a format already describes (json, object, string, ...) are not written; the media type alone says as much.
mimetypeExact media type produced by serializer.
serializerCallable converting a T to a Chunk.
Returns
OK on success, or an error (e.g. when serializer is empty).

◆ serializer_count()

size_t a11::data::SerializationRegistry::serializer_count ( ) const

Number of registered serializers.

◆ ToChunk()

template<typename T >
absl::StatusOr< Chunk > a11::data::SerializationRegistry::ToChunk ( const T &  value,
std::string_view  mimetype = {} 
) const
inline

Serializes value into a Chunk.

Parameters
valueValue to serialize.
mimetypeOptional media type to select a representation; when empty the type's registered default (by registration order) is used.
Returns
A chunk with an exact mimetype and type tag, or an error when no matching serializer is registered.

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