Arduinojson: how big the capacity of DynamicJsonDocument can be for esp32

Created on 9 Jan 2020  路  4Comments  路  Source: bblanchon/ArduinoJson

i use an esp32-cam for my project, and i want to parse a big json file from the client response.
So, in order to calculate a possible capacity i took a big json response and duplicate it in arduinoJson assistance.
It gave me: const size_t capacity = JSON_ARRAY_SIZE(382) + 382 * JSON_OBJECT_SIZE(8) + 64440;

When i use it (even with much smaller json) i get "no memory" error.

When i change to the "real" capacity of my current json response to:
const size_t capacity = JSON_ARRAY_SIZE(30) + 30*JSON_OBJECT_SIZE(8) + 4880;

it works fine..

So my questions are:
1) why i get this error even i give so much space? is there any limit for capacity in esp32
2) i guess deserializeJson(doc, client) is much more efficient in speed and memory compared to get the payload String and do deserializeJson(doc, payload)..am i correct?

question

All 4 comments

Hi @ligantx,

If you try to allocate a JsonDocument that is too large to fit in memory, the allocation fails and deserializeJson() returns NoMemory.

The ESP32 has 520KB of RAM, but many boards extend this capacity with an external PSRAM chip. Assuming your board embeds such a chip, your program must use dedicated allocation functions to use it.

To use the external PSRAM with ArduinoJson, you must use a custom allocator. Here is an example from the documentation of BasicJsonDocument:

struct SpiRamAllocator {
  void* allocate(size_t size) {
    return heap_caps_malloc(size, MALLOC_CAP_SPIRAM);
  }
  void deallocate(void* pointer) {
    heap_caps_free(pointer);
  }
};

using SpiRamJsonDocument = BasicJsonDocument<SpiRamAllocator>;

SpiRamJsonDocument is similar to DynamicJsonDocument, except that it uses the external PSRAM.

Best regards,
Benoit

PS: I added How to use external RAM on an ESP32? to the documentation.

actually this is much better than a simple answer!

question: so the allocation that i tried const size_t capacity = JSON_ARRAY_SIZE(382) + 382 * JSON_OBJECT_SIZE(8) + 64440; was larger even than the 520KB of esp32 ram?


i tried your solution but get this error

no matching function for call to 'ArduinoJson6130_000001::BasicJsonDocument<SpiRamAllocator>::BasicJsonDocument()'

I don't know the value, you have to check with the assistant, but it's most likely larger than 520KB.

no matching function for call to 'BasicJsonDocument<SpiRamAllocator>::BasicJsonDocument()' means you forgot to pass the capacity. I'm sorry, I forgot to put it in the example:

SpiRamJsonDocument doc(1048576);
deserializeJson(doc, input);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

czuvich picture czuvich  路  7Comments

kylegordon picture kylegordon  路  6Comments

loewekordel picture loewekordel  路  4Comments

ghost picture ghost  路  7Comments

koffienl picture koffienl  路  3Comments