Hello there,
In the application I am currently working on, I am looking into the Arduino_JSON functionality to create a function to save and load application option settings to/from an SD card.
So I created the following test code to test on the ESP32:
#include <Arduino_JSON.h>
#include <SD.h>
const char *input = R"(
{
"code": "200",
"message": 0,
"list": [
{
"main": {
"temp": 3.23,
"pressure": 1014,
"humidity": 58
},
"weather": {
"main": "Clear",
"description": "clear sky"
},
"dt": "2020-02-12 09:00:00"
},
{
"main": {
"temp": 6.09,
"pressure": 1015,
"humidity": 48
},
"weather": {
"main": "Clear",
"description": "clear sky"
},
"dt": "2020-02-12 12:00:00"
}
],
"city": {
"name": "London",
"coord": {
"lat": 51.5085,
"lon": -0.1257
}
}
}
)";
void Test_hasOwnProperty(JSONVar &obj) {
Serial.println("===== Test_hasOwnProperty =====");
Serial.println(obj.hasOwnProperty("code")); // (1) 1 ... OK
Serial.println(obj.hasOwnProperty("main")); // (2) 0 ... OK
Serial.println(obj["list"][0].hasOwnProperty("main")); // (3) 1 ... OK
}
void Test_hasPropertyEqual(JSONVar &obj) {
JSONVar London = "London";
Serial.println("===== Test_hasPropertyEqual =====");
Serial.println(obj.hasPropertyEqual("code", "200")); // (4) 1 ... OK
Serial.println(obj["city"].hasPropertyEqual("name", "London")); // (5) 1 ... OK
Serial.println(obj["city"].hasPropertyEqual("name", London )); // (6) 1 ... OK
JSONVar num = 0;
//Serial.println(obj["city"].hasPropertyEqual("name", num)); // (7) Expect 0 (Q1) ... Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
//Serial.println(obj.hasPropertyEqual("message", num)); // (8) Expect 1 (Q1) ... Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
//Serial.println(obj.hasPropertyEqual("message", 0 )); // (9) Expect 1 (Q1) ... Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
}
void Test_filter(JSONVar &obj) {
Serial.println("===== Test_filter =====");
Serial.println(JSON.stringify(obj.filter("code", "200"))); // (10) OK
Serial.println(JSON.stringify(obj["city"].filter("name", "London"))); // (11) OK
JSONVar filter;
filter["name"] = "London";
Serial.println(JSON.stringify(obj.filter("city", filter))); // (12) null ... ? (Q2)
filter = 0;
Serial.println(JSON.stringify(obj.filter("message", filter))); // (13) null ... ? (Q3)
Serial.println(JSON.stringify(obj.filter("message", 0 ))); // (14) null ... ? (Q3)
}
void Test_printTo(JSONVar &obj) {
Serial.println("===== Test_printTo =====");
if (!SD.begin()) {
Serial.println("SD initialization failed.");
return;
}
File myFile = SD.open("/test.txt", FILE_WRITE);
if (!myFile) {
Serial.println("Error opening test.txt");
return;
}
size_t len = obj.printTo(myFile);
myFile.close();
char* buf = (char*)malloc(len + 1);
if (!buf) {
Serial.println("malloc failed.");
return;
}
myFile = SD.open("/test.txt", FILE_READ);
len = myFile.read((uint8_t*)buf, len);
buf[len] = '\0';
JSONVar tmp = JSON.parse(buf); // (15) OK
Serial.println(JSON.stringify(tmp));
free((void*)buf);
}
void setup() {
Serial.begin(115200);
while (millis() < 1500);
Serial.println("\nStart.");
JSONVar myObject = JSON.parse(input);
String type = JSON.typeof(myObject);
if (type == "undefined") {
Serial.println("Parsing input failed.");
return;
}
Test_hasOwnProperty (myObject);
Test_hasPropertyEqual (myObject);
Test_filter (myObject);
Test_printTo (myObject);
Serial.println("Done.");
}
void loop() {}
Q1
(7), (8), and (9) in Test_hasPropertyEqual() are supposed to verify whether "Property" has a specific numeric value. However, the results are as stated in the comments. Is my usage incorrect?
If I modify the line 516 of JSONVar.cpp as follows, the error will not occur...
return json != NULL && json->valuestring && strcmp(value, json->valuestring) == 0;
Q2
(12) in Test_filter() is code that expects to get the same result as (11). Is filter being used incorrectly?
Q3
(13) and (14) in Test_filter() are supposed to filter specific "key" and "value". Am I using them incorrectly?
Q4
I have a question for the Arduino team in particular: Arduino_JSON is listed as [BETA] in the library manager, but can we expect future updates?
Thank you.

