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.
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.
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: ");
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.