mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-13 13:01:55 +03:00
Updated Example to use ArduinoJson6 (#6203)
* Updated Example to use ArduinoJson6 * Updated save method to Serialize and not Deserialize * Updated References to ArduinoJson 6.11.0 * Style Fix * another line missed * Added the file extension to the new version
This commit is contained in:
@ -31,16 +31,15 @@ bool loadConfig() {
|
||||
// use configFile.readString instead.
|
||||
configFile.readBytes(buf.get(), size);
|
||||
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
JsonObject& json = jsonBuffer.parseObject(buf.get());
|
||||
|
||||
if (!json.success()) {
|
||||
StaticJsonDocument<200> doc;
|
||||
auto error = deserializeJson(doc, buf.get());
|
||||
if (error) {
|
||||
Serial.println("Failed to parse config file");
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* serverName = json["serverName"];
|
||||
const char* accessToken = json["accessToken"];
|
||||
const char* serverName = doc["serverName"];
|
||||
const char* accessToken = doc["accessToken"];
|
||||
|
||||
// Real world application would store these values in some variables for
|
||||
// later use.
|
||||
@ -53,10 +52,9 @@ bool loadConfig() {
|
||||
}
|
||||
|
||||
bool saveConfig() {
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
JsonObject& json = jsonBuffer.createObject();
|
||||
json["serverName"] = "api.example.com";
|
||||
json["accessToken"] = "128du9as8du12eoue8da98h123ueh9h98";
|
||||
StaticJsonDocument<200> doc;
|
||||
doc["serverName"] = "api.example.com";
|
||||
doc["accessToken"] = "128du9as8du12eoue8da98h123ueh9h98";
|
||||
|
||||
File configFile = SPIFFS.open("/config.json", "w");
|
||||
if (!configFile) {
|
||||
@ -64,7 +62,7 @@ bool saveConfig() {
|
||||
return false;
|
||||
}
|
||||
|
||||
json.printTo(configFile);
|
||||
serializeJson(doc, configFile);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user