MsgPackBinary
Description
The MsgPackBinary class allows you to read or write MessagePack binary data.
This feature was added in ArduinoJson 7.1.0.
Internally, MsgPackBinary uses the same storage as serialized() values.
Constructors
MsgPackBinary(const void* data, size_t size);
Member functions
const void* data() const;
size_t size() const;
Example
Serialization
char buffer[] = {1, 2, 3};
JsonDocument doc;
doc['data'] = MsgPackBinary(buffer, sizeof(buffer));
serializeMsgPack(doc, output); // 81 A4 65 61 74 61 C4 03 01 02 03
MsgPackBinary 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);
MsgPackBinary data = doc['data'];
// or: auto data = doc['data'].as<MsgPackBinary>();
const void* buffer = data.data();
size_t size = data.size();
MsgPackBinary 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 MsgPackBinary 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 binary data is:
| CPU architecture | Max binary size |
|---|---|
| 8-bit | 253 |
| 32-bit | 65,632 |
| 64-bit | 65,632 |