bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.7k stars 1.12k forks source link

v7 equivalent of validateJson #2090

Closed higaski closed 4 months ago

higaski commented 4 months ago

The v6 documentation contains a small validateJson snippet which I have always found immensely useful:

bool validateJson(const char* input) {
  StaticJsonDocument<0> doc, filter;
  return deserializeJson(doc, input, DeserializationOption::Filter(filter)) == DeserializationError::Ok;
}

Now, the v7 documentation contains an equivalent snippet, but I've red that Static- and DynamicJsonDocument got merged into a single class which now always allocates in 1kB blocks. Does this mean that validateJson now allocates 2x1kB?

bool validateJson(const char* input) {
  JsonDocument doc, filter;
  return deserializeJson(doc, input, DeserializationOption::Filter(filter)) == DeserializationError::Ok;
}
bblanchon commented 4 months ago

Hi @higaski,

The current implementation allocates the memory pools lazily, so an empty JsonDocument doesn't require any heap allocation.

You can confirm that by replacing the allocator: https://wandbox.org/permlink/p07bfdAXL9johHby

Best regards, Benoit

higaski commented 4 months ago

Thanks for clearing that up!