ArduinoJson is not slow by itself, but it looks slow when used with unbuffered streams because it often reads and writes characters one at a time.

The following classes are impacted (not exhaustive): EthernetClient, WifiClient, File, PubSubClient.

Speed up serialization

As an example, we’ll take the WiFiClient class from the ESP8266 core. Because ArduinoJson writes bytes one by one, WiFiClient spends a lot of time sending small packets. To speed up your program, you need to insert a buffer between serializeJson() and WiFiClient. You can do that using the StreamUtils library.

StreamUtils's WriteBufferingStream

Suppose your program is currently like that:

serializeJson(doc, wifiClient);

To add buffering, replace the line above with the followings:

WriteBufferingStream bufferedWifiClient(wifiClient, 64);
serializeJson(doc, bufferedWifiClient);
bufferedWifiClient.flush();

The first line creates a new stream bufferedWifiClient that implements buffering on top of the original WiFiClient (this is the Decorator pattern).
The second line writes the JSON document to the WiFiClient through the buffer.
The last line flushes the buffer to make sure we send the end of the document.

Speed up deserialization

As an example, we’ll take the File class from the ESP8266 core. Because ArduinoJson reads bytes one by one, File spends a lot of time transmitting small packets over the SPI bus. To speed up your program, you must insert a buffer between deserializeJson() and File. You can do that using the StreamUtils library.

StreamUtils's ReadBufferingStream

Suppose your program is currently like so:

deserializeJson(doc, file);

To add buffering, replace the line above with the followings:

ReadBufferingStream bufferedFile(file, 64);
deserializeJson(doc, bufferedFile);

The first line creates a new stream bufferedFile that implements buffering on top of the original File (this is the Decorator pattern).
The second line reads the JSON document from the File through the buffer.

See also

Check out the README file of StreamUtils to see the other things you can do with this library.

StreamUtils is a powerful library that deserves more attention. Please give it a star to spread the word.