Description

The MsgPackExtension class allows you to read or write MessagePack extension data.

This feature was added in ArduinoJson 7.1.0.

Internally, MsgPackExtension uses the same storage as serialized() values.

Constructors

MsgPackExtension(int8_t type, const void* data, size_t size);

Member functions

int8_t type() const;
const void* data() const;
size_t size() const;

Example

Serialization

char buffer[] = {1, 2, 3};

JsonDocument doc;
doc['data'] = MsgPackExtension(4, buffer, sizeof(buffer));
serializeMsgPack(doc, output);   // 81 A4 65 61 74 61 C7 03 04 01 02 03

MsgPackExtension doesn’t hold a copy of the data; it only stores a pointer and a size. Therefore, you must ensure the buffer remains valid until you insert it into a JsonDocument (where it will be copied).

Deserialization

JsonDocument doc;
deserializeMsgPack(doc, input);

MsgPackExtension data = doc['data'];
// or: auto data = doc['data'].as<MsgPackExtension>();

int8_t type = data.type();
const void* buffer = data.data();
size_t size = data.size();

MsgPackExtension doesn’t hold a copy of the data; it only stores a pointer and a size. Therefore, you must ensure the JsonDocument remains valid as long as the MsgPackExtension object is in use.

Size restrictions

JsonDocument stores the size of the binary data in an integer whose width is determined by ARDUINOJSON_STRING_LENGTH_SIZE.

With the default settings, the maximum size of the extension data is:

CPU architecture Max extension size
8-bit 252
32-bit 65,631
64-bit 65,631

See also