A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
serialization.h File Reference

Type-and-mimetype indexed serialization of values to/from Chunks. More...

#include <any>
#include <cstddef>
#include <functional>
#include <string>
#include <string_view>
#include <typeindex>
#include <typeinfo>
#include <utility>
#include <vector>
#include <absl/base/nullability.h>
#include <absl/status/status.h>
#include <absl/status/status_macros.h>
#include <absl/status/statusor.h>
#include <absl/strings/str_cat.h>
#include "a11/data/types.h"
#include "a11/exception_guard.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  a11::data::TypedChunkObject< T >
 The local fast path: a value in a chunk, encoded only if needed. More...
 
class  a11::data::SerializationRegistry
 

Namespaces

namespace  a11
 
namespace  a11::data
 

Functions

template<typename T >
std::optional< T > a11::data::TryTakeObject (const Chunk &chunk, std::string_view tag)
 Takes the value out of chunk, if it is carrying one of type T.
 
SerializationRegistry & a11::data::GlobalSerializationRegistry ()
 Returns the process-wide registry (defaults pre-installed).
 
template<typename T >
Chunk a11::data::MakeChunkObject (T value, std::string tag, std::string mimetype, std::shared_ptr< const SerializationRegistry > registry)
 Builds a chunk carrying value, with no bytes produced.
 

Variables

constexpr std::string_view a11::data::kJsonMimetype = "application/json"
 Media type of the built-in JSON codec.
 
constexpr std::string_view a11::data::kMsgpackMimetype = "application/x-msgpack"
 Media type of the built-in MessagePack codec.
 
constexpr std::string_view a11::data::kTextMimetype = "text/plain"
 Media type of UTF-8 text carried as itself.
 
constexpr std::string_view a11::data::kBytesMimetype = "application/octet-stream"
 Media type of opaque bytes carried as themselves.
 

Detailed Description

Type-and-mimetype indexed serialization of values to/from Chunks.

a11::data::SerializationRegistry maps a C++ type together with a media type (its representation) to a serializer/deserializer pair, so arbitrary values can be converted to and from a11::data::Chunk.

A chunk's metadata is the only thing that says how to read its bytes. The media type gives the representation, and when the value is not one the format already describes, a type parameter names it ("application/json;type=a11.sdk.Interaction"). Nothing inside the payload repeats that. A bare "application/json" is therefore a complete description: it reads back as a nlohmann::json. Asking FromChunk for a particular T is a request the registry makes a best effort to satisfy, reporting the codec's own error when the data will not fit.

JSON and MessagePack codecs are available as defaults.