Type Support#

Here shows types supported by this library and the way to add support for other types.

Types Supported by This Library#

This library supports serialization of some builtin scalar types and some types in C++ standard library. Such support can be used by including corresponding headers.

Currently Supported Types in cpp-msgpack-light Library#

Types in C++

Type in MessagePack

Header to Include

bool

Boolean

msgpack_light/type_support/common.h

Integers in 8 bit, 16 bit, 32 bit and, 64 bit (char, int, uint32_t, …)

Integer

msgpack_light/type_support/common.h

float

Float

msgpack_light/type_support/common.h

double

Float

msgpack_light/type_support/common.h

std::nullptr_t (nullptr instance)

Nil

msgpack_light/type_support/common.h

std::string

String

msgpack_light/type_support/common.h

std::string_view

String

msgpack_light/type_support/common.h

std::vector

Array

msgpack_light/type_support/common.h

std::vector<unsigned char>

Binary

msgpack_light/type_support/common.h

std::array

Array

msgpack_light/type_support/array.h

std::deque

Array

msgpack_light/type_support/deque.h

std::forward_list

Array

msgpack_light/type_support/forward_list.h

std::list

Array

msgpack_light/type_support/list.h

std::set

Array

msgpack_light/type_support/set.h

std::multiset

Array

msgpack_light/type_support/set.h

std::unordered_set

Array

msgpack_light/type_support/unordered_set.h

std::unordered_multiset

Array

msgpack_light/type_support/unordered_set.h

std::map

Map

msgpack_light/type_support/map.h

std::multimap

Map

msgpack_light/type_support/map.h

std::unordered_map

Map

msgpack_light/type_support/unordered_map.h

std::unordered_multimap

Map

msgpack_light/type_support/unordered_map.h

std::pair

Array

msgpack_light/type_support/pair.h

std::tuple

Array

msgpack_light/type_support/tuple.h

std::optional

Nil or the type for the value

msgpack_light/type_support/optional.h

std::chrono::time_point

Extension

msgpack_light/type_support/chrono.h

std::timespec

Extension

msgpack_light/type_support/timespec.h

Attention

Strings are assumed to be encoded in UTF-8.

Hint

Allocators other than std::allocator can be used. For example, std::pmr::string can be serialized.

Add Support for Other Types#

Users can add support for other types by one of the following methods:

Specialization of serialization_traits#

For example, a class example::Example can be supported as follows:

// Following headers are required for implementation of serialization_traits.
#include "msgpack_light/serialization_buffer.h"
#include "msgpack_light/type_support/fwd.h"
// Other headers may be needed to serialize some existing types.

namespace msgpack_light::type_support {

/*!
 * \brief Class to serialize nullptr.
 */
template <>
struct serialization_traits<example::Example> {
public:
    static void serialize(serialization_buffer& buffer, const example::Example& value) {
        // Types already supported can be serialized using "serialize" function.
        buffer.serialize(value.get_parameter());
    }
};

}  // namespace msgpack_light::type_support

msgpack_light::type_support::serialization_traits has a template parameter to be used for SFINAE as the second template parameter. For example, classes derived from a class example::Base can be supported as follows:

// Following headers are required for implementation of serialization_traits.
#include "msgpack_light/serialization_buffer.h"
#include "msgpack_light/type_support/fwd.h"
// Other headers may be needed to serialize some existing types.

namespace msgpack_light::type_support {

/*!
 * \brief Class to serialize nullptr.
 */
template <typename T>
struct serialization_traits<T, std::enable_if_t<std::is_base_of<example::Base, T>>> {
public:
    static void serialize(serialization_buffer& buffer, const T& value) {
        // Types already supported can be serialized using "serialize" function.
        buffer.serialize(value.get_parameter());
    }
};

}  // namespace msgpack_light::type_support

Macros to Add Support of C++ struct#

This library provides two macros for support of C++ struct.

For example, a struct can be supported as follows:

namespace example {

struct Example {
    int param1;
    std::string param2;
};

}  // namespace example

// Add support to serialize into maps
MSGPACK_LIGHT_STRUCT_MAP(example::Example, param1, param2);
// or serialize into arrays.
MSGPACK_LIGHT_STRUCT_ARRAY(example::Example, param1, param2);

Reference#

MSGPACK_LIGHT_STRUCT_MAP(STRUCT, ...)#

Macro to generate class to support serialization of struct into maps.

Warning

This macro works for 1 to 15 parameters.

Parameters:
  • STRUCT – Type of the struct. Remaining arguments are treated as parameter names.

MSGPACK_LIGHT_STRUCT_ARRAY(STRUCT, ...)#

Macro to generate class to support serialization of struct into arrays.

Warning

This macro works for 1 to 15 parameters.

Parameters:
  • STRUCT – Type of the struct. Remaining arguments are treated as parameter names.

template<typename T, typename SFINAE = void>
struct serialization_traits#

Class to define functions to serialize data of various types.

Specializations of this template must implement a function with the following signature.

static void serialize(serialization_buffer& buffer, const T& value);

  • The first argument is the buffer to serialize to. For the usage of the buffer, see msgpack_light::serialization_buffer class.

  • The second argument is the serialized value. (Either const T& or T can be usable.)

Template Parameters:
  • T – Type of the data to serialize.

  • SFINAE – Template parameter to use for SFINAE.

class serialization_buffer#

Class of buffers to serialize data.

Note

For serialization of types already supported by msgpack_light::type_support::serialization_traits template, use serialize() function.

Initialization and finalization.

inline explicit serialization_buffer(output_stream &stream)#

Constructor.

Warning

This class hold the reference of the given stream.

Parameters:

stream[out] Stream to write output to.

serialization_buffer(const serialization_buffer&) = delete#
serialization_buffer(serialization_buffer&&) = delete#
serialization_buffer &operator=(const serialization_buffer&) = delete#
serialization_buffer &operator=(serialization_buffer&&) = delete#
inline ~serialization_buffer() noexcept#

Destructor.

Note

This will call flush() function.

inline void flush()#

Flush the internal buffer in this instance.

Warning

Data may not be written to streams without call of this function or destructor.

Serialization of Nil in MessagePack.

inline void serialize_nil()#

Serialize a nli value.

Serialization of Boolean in MessagePack.

inline void serialize_bool(bool value)#

Serialize a boolean value.

Parameters:

value[in] Value.

Serialization of Integer in MessagePack.

These functions implements serialization of integers in formats in MessagePack specification. To serialize integers with automatic selection of formats, use serialize() function.

inline void serialize_positive_fixint(std::uint8_t value)#

Serialize a value in positive fixint format.

Warning

This function assumes that the value is in the range of 0 to 0x7F.

Parameters:

value[in] Value.

inline void serialize_negative_fixint(std::int8_t value)#

Serialize a value in negative fixint format.

Warning

This function assumes that the value is in the range of -1 to 0xE0.

Parameters:

value[in] Value.

inline void serialize_uint8(std::uint8_t value)#

Serialize a value in uint 8 format.

Parameters:

value[in] Value.

inline void serialize_uint16(std::uint16_t value)#

Serialize a value in uint 16 format.

Parameters:

value[in] Value.

inline void serialize_uint32(std::uint32_t value)#

Serialize a value in uint 32 format.

Parameters:

value[in] Value.

inline void serialize_uint64(std::uint64_t value)#

Serialize a value in uint 64 format.

Parameters:

value[in] Value.

inline void serialize_int8(std::int8_t value)#

Serialize a value in int 8 format.

Parameters:

value[in] Value.

inline void serialize_int16(std::int16_t value)#

Serialize a value in int 16 format.

Parameters:

value[in] Value.

inline void serialize_int32(std::int32_t value)#

Serialize a value in int 32 format.

Parameters:

value[in] Value.

inline void serialize_int64(std::int64_t value)#

Serialize a value in int 64 format.

Parameters:

value[in] Value.

Serialization of Float in MessagePack.

inline void serialize_float32(float value)#

Serialize a value in float 32 format.

Parameters:

value[in] Value.

inline void serialize_float64(double value)#

Serialize a value in float 64 format.

Parameters:

value[in] Value.

Serialization of sizes in String in MessagePack.

To serialize sizes with automatic selection of formats, use serialize_str_size() function.

inline void serialize_fixstr_size(std::uint8_t size)#

Serialize a size of fixstr format.

Warning

This function assumes that the size is in the range of 0 to 0x1F.

Parameters:

size[in] Size.

inline void serialize_str8_size(std::uint8_t size)#

Serialize a size of str 8 format.

Parameters:

size[in] Size.

inline void serialize_str16_size(std::uint16_t size)#

Serialize a size of str 16 format.

Parameters:

size[in] Size.

inline void serialize_str32_size(std::uint32_t size)#

Serialize a size of str 32 format.

Parameters:

size[in] Size.

inline void serialize_str_size(std::size_t size)#

Serialize a size of a string.

Parameters:

size[in] Size.

Serialization of sizes in Binary in MessagePack.

To serialize sizes with automatic selection of formats, use serialize_bin_size() function.

inline void serialize_bin8_size(std::uint8_t size)#

Serialize a size of bin 8 format.

Parameters:

size[in] Size.

inline void serialize_bin16_size(std::uint16_t size)#

Serialize a size of bin 16 format.

Parameters:

size[in] Size.

inline void serialize_bin32_size(std::uint32_t size)#

Serialize a size of bin 32 format.

Parameters:

size[in] Size.

inline void serialize_bin_size(std::size_t size)#

Serialize a size of a binary.

Parameters:

size[in] Size.

Serialization of sizes in Array in MessagePack.

To serialize sizes with automatic selection of formats, use serialize_array_size() function.

inline void serialize_fixarray_size(std::uint8_t size)#

Serialize a size of fixarray format.

Warning

This function assumes that the size is in the range of 0 to 15.

Parameters:

size[in] Size.

inline void serialize_array16_size(std::uint16_t size)#

Serialize a size of array 16 format.

Parameters:

size[in] Size.

inline void serialize_array32_size(std::uint32_t size)#

Serialize a size of array 32 format.

Parameters:

size[in] Size.

inline void serialize_array_size(std::size_t size)#

Serialize a size of an array.

Parameters:

size[in] Size.

Serialization of sizes in Map in MessagePack.

To serialize sizes with automatic selection of formats, use serialize_map_size() function.

inline void serialize_fixmap_size(std::uint8_t size)#

Serialize a size of fixmap format.

Warning

This function assumes that the size is in the range of 0 to 15.

Parameters:

size[in] Size.

inline void serialize_map16_size(std::uint16_t size)#

Serialize a size of map 16 format.

Parameters:

size[in] Size.

inline void serialize_map32_size(std::uint32_t size)#

Serialize a size of map 32 format.

Parameters:

size[in] Size.

inline void serialize_map_size(std::size_t size)#

Serialize a size of a map.

Parameters:

size[in] Size.

Serialization of sizes and types in Extension in MessagePack.

To serialize sizes with automatic selection of formats, use serialize_ext_header() function.

inline void serialize_fixext1_header(std::int8_t ext_type)#

Serialize the size and type of an extension value in fixext 1 format.

Note

Serialize data of the value after call of this function.

Parameters:

ext_type[in] Extension type.

inline void serialize_fixext2_header(std::int8_t ext_type)#

Serialize the size and type of an extension value in fixext 2 format.

Note

Serialize data of the value after call of this function.

Parameters:

ext_type[in] Extension type.

inline void serialize_fixext4_header(std::int8_t ext_type)#

Serialize the size and type of an extension value in fixext 4 format.

Note

Serialize data of the value after call of this function.

Parameters:

ext_type[in] Extension type.

inline void serialize_fixext8_header(std::int8_t ext_type)#

Serialize the size and type of an extension value in fixext 8 format.

Note

Serialize data of the value after call of this function.

Parameters:

ext_type[in] Extension type.

inline void serialize_fixext16_header(std::int8_t ext_type)#

Serialize the size and type of an extension value in fixext 16 format.

Note

Serialize data of the value after call of this function.

Parameters:

ext_type[in] Extension type.

inline void serialize_ext8_header(std::int8_t ext_type, std::uint8_t data_size)#

Serialize the size and type of an extension value in ext 8 format.

Note

Serialize data of the value after call of this function.

Parameters:
  • ext_type[in] Extension type.

  • data_size[in] Size of the data.

inline void serialize_ext16_header(std::int8_t ext_type, std::uint16_t data_size)#

Serialize the size and type of an extension value in ext 16 format.

Note

Serialize data of the value after call of this function.

Parameters:
  • ext_type[in] Extension type.

  • data_size[in] Size of the data.

inline void serialize_ext32_header(std::int8_t ext_type, std::uint32_t data_size)#

Serialize the size and type of an extension value in ext 32 format.

Note

Serialize data of the value after call of this function.

Parameters:
  • ext_type[in] Extension type.

  • data_size[in] Size of the data.

inline void serialize_ext_header(std::int8_t ext_type, std::size_t data_size)#

Serialize the size and type of an extension value.

Note

Serialize data of the value after call of this function.

Parameters:
  • ext_type[in] Extension type.

  • data_size[in] Size of the data.

Serialization of general types.

template<typename T>
inline void serialize(const T &data)#

Serialize data.

Note

This function can be usable for types using msgpack_light::type_support::serialization_traits class.

Template Parameters:

T – Type of data.

Parameters:

data[in] Data.

Functions to write data directly.

inline void write(const unsigned char *data, std::size_t size)#

Write data.

Note

Use this function to write data of strings, binaries, arrays, maps, and extension types.

Parameters:
  • data[in] Pointer to the data.

  • size[in] Size of the data.

inline void put(unsigned char data)#

Write a byte of data.

Parameters:

data[in] Data.

template<typename ...T>
inline void write_in_big_endian(T... values)#

Write values in big endian.

Template Parameters:

T – Types of the values.

Parameters:

values[in] Values.