-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing JsonDocument by reference #2088
Comments
Hi @jgroszko, After making the following changes, I could successfully run your program on my ESP32. #include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "...";
const char* password = "...";
bool filteredJsonGet(String &url, JsonDocument &result, JsonDocument &filter) {
HTTPClient http;
http.useHTTP10(true);
http.begin(url);
Serial.println("About to get");
int httpCode = http.GET();
Serial.println("Got");
if(httpCode != 200) {
Serial.print("GET failed ");
Serial.print(httpCode);
Serial.print(" - ");
Serial.println(url);
http.end();
return false;
} else {
Serial.println("HTTP Success");
DeserializationError error = deserializeJson(result, http.getStream(), DeserializationOption::Filter(filter));
Serial.println("Deserialized json");
http.end();
if (error) {
Serial.print("GET failed ");
Serial.print(url);
Serial.print(" - deserializeJson() failed: ");
Serial.println(error.c_str());
return false;
}
if (result.overflowed()) {
Serial.print("GET failed ");
Serial.print(url);
Serial.println(" - Ran out of memory for JsonDocument");
return false;
}
Serial.println("No json errors");
}
+ return true;
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
String url = "http://worldtimeapi.org/api/timezone/America/Chicago";
JsonDocument filter;
filter["unixtime"] = true;
JsonDocument result;
if(filteredJsonGet(url, result, filter)) {
Serial.print("Success ");
- Serial.println(result["unixtime"].as<const char*>());
+ Serial.println(result["unixtime"].as<unsigned long>());
} else {
Serial.println("Fail");
}
}
void loop() {
// put your main code here, to run repeatedly:
} I tested with versions 2.0.14 and 2.0.16 of the Arduino Core for ESP32. I first thought that the crash was due to calling Best regards, |
PS: I just saw in your crash dump that you are using the Inkplate core. I looked at the source code and found the same protection against null pointers passed to |
Hey Benoit, Thanks for the quick response! It was definitely the missing return statement, glad it was something simple. I was afraid it'd be some pointer nonsense that I was missing. |
You're welcome, @jgroszko. |
Describe the issue
I'm trying to DRY up my code and create a helper function for fetching some JSON and filtering it, but it looks like my program is crashing when it returns from the helper function.
Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:
deserializeJson()
returnsOk
JsonDocument
String
doesn't solve the issuestrcmp()
, norstrcpy()
, notprintf()
Environment
Here is the environment that I'm using':
Reproduction
Here is a small snippet that demonstrate the problem.
Program output
If relevant, include the program output.
Expected output:
Actual output:
Thanks!
The text was updated successfully, but these errors were encountered: