ArduinoJson is not slow by itself, but it can slow down unbuffered streams because it reads and writes characters one at a time.

Improve writing speed

As an example, we’ll take the WifiClient 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 ArduinoJson a WifiClient. You can do that using the StreamUtils library.

Suppose your program is currently like that:

serializeJson(doc, wifiClient);

To add buffering, you need to replace this line 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.

Improve reading speed

As an example, we’ll take the File class from SPIFFS. Because ArduinoJson reads bytes one by one, File spends a lot of time transmitting small packets over the SPI.

To speed up your program, you need to insert a buffer between ArduinoJson a File. You can do that using the StreamUtils library.

Suppose your program is currently like that:

deserializeJson(doc, file);

To add buffering, you need to replace this line with the followings:

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

The first creates a new stream bufferedFile that implements buffering on top of the original file (this is the Decorator pattern). The second line read 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.