ESP32 ArduinoJson: Getting the length of the MessagePack serialization payload
DFRobot
Jul 23 2019261008
In this tutorial we will check how to obtain the length of the MessagePack payload that is obtained when we serialize data to this format, using the ArduinoJson library. The tests shown here were performed using an ESP32 board from DFRobot.
Introduction
In this tutorial we will check how to obtain the length of the MessagePack payload that is obtained when we serialize data to this format, using the ArduinoJson library. We will be using the Arduino core, running on the ESP32.
For an introductory tutorial on how to serialize content to the MessagePack format, please check this previous tutorial.
The tests shown here were performed using an ESP32 board from DFRobot, using version 6 of ArduinoJson.
The code
As usual, we will start the code by including the ArduinoJson library.
#include <ArduinoJson.h>
Then, moving on to the Arduino setup function, we will start by opening a serial connection, so we can output the result of our program.
Serial.begin(115200);
After this, we will declare an object of class StaticJsonDocument, to hold the memory representation of our data.
StaticJsonDocument<100> testDocument;
For this tutorial we will be using the same structure we have used in previous tutorials:
{
"sensorType": "temperature",
"value": 10
}
So, to add these elements to our StaticJsonDocument, we simply need to use the [] operator.
Then, to obtain the length of the payload produced by the MessagePack serialization, we simply need to call the measureMsgPack function. As input, we will pass our StaticJsonDocument. As output, the function will return the length, which we will print to the serial port.
int expectedBytesToWrite = measureMsgPack(testDocument);
Serial.print("Expected Bytes: ");
Serial.println(expectedBytesToWrite);
For comparison, we will now do the actual serialization of the data to the MessagePack format, like we have done in the previous tutorial.
So, we start by declaring a data buffer to hold the result and then we simply call the serializeMsgPack function to do the actual serialization.
Note that this function returns the number of bytes written to the output buffer, which corresponds to the length of the payload. Thus, we will also print this value to the serial port, to confirm it matches the value returned by the measureMsgPack function.
To test the code, simply compile it and upload it to your ESP32, using the Arduino IDE. After the procedure is finished, open the Arduino IDE serial monitor.
You should obtain a result similar to figure 1. As can be seen, the lengths printed to the serial match, as expected.
Figure 1 – Output of the program, showing the expected and the actual length of the MessagePack payload.
The landscape of K–12 education is rapidly evolving, moving from simple digital making to complex problem-solving with Artificial Intelligence. At DFRobot, we believe in equipping educators with the best tools and knowledge to navigate this shift. That is why we are thrilled to announce that DFRobot will be a featured presenter at the upcoming 2026 Fab Educators Summit on Friday, January 30th.
From smart wearables and robotic skins to industrial monitoring, Force Sensitive Resistors (FSR) and Thin Film Pressure Sensors are essential components for tactile feedback. DFRobot offers a comprehensive range of force sensors, but choosing the right one can be tricky.
We are excited to announce that DFRobot will be participating in Bett 2026. From Jan 21 - 23, 2026, join us at the ExCeL London to see how we are transforming classrooms with accessible AI and robotics.