Answer the question
In order to leave comments, you need to log in
How to pass json buffer as function argument in ArduinoJson 6?
#include <Arduino.h>
#include <ArduinoJson.h> // @6.13.0
JsonDocument jsonParse(const String json, uint32_t buffer = 2048) {
StaticJsonDocument<buffer> jsonBuffer;
DeserializationError error = deserializeJson(jsonBuffer, json);
if (error) {
printToSerial(F("jsonParse error: "));
printlnToSerial(error.c_str());
}
return jsonBuffer;
}
void setup() {
const String json = F("{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}");
JsonDocument document = jsonParse(json, 512);
const String sensor = document[F("sensor")]; // "gps"
}
void loop() {}
StaticJsonDocument<buffer> jsonBuffer;
IDE (VSCode, PlatformIO) swears at buffer
- the expression must have a constant value -- the value of the "buffer" parameter (declared on line 4) cannot be used as a constant Answer the question
In order to leave comments, you need to log in
It seems that here the size should be known at the compilation stage. It is easier to pass to the function not the size of the buffer, but let's say the buffer itself, or make the function a template parameter with the size of the buffer.
Your option with a variable is not suitable, because its value is not known at compile time.
You can also try DynamicJsonDocument , its size can be specified at runtime, according to the documentation.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question