json utils updated

This commit is contained in:
Dmitry Borisenko
2021-10-09 18:48:02 +00:00
parent 7218411e34
commit bd6fa0fc37
4 changed files with 63 additions and 9 deletions

View File

@@ -5,7 +5,7 @@
#include "Global.h"
#include "Utils/FileUtils.h"
//=================================================================================
//depricated======================================================================
String jsonReadStr(String& json, String name) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
@@ -27,6 +27,51 @@ int jsonReadInt(String& json, String name) {
return doc[name].as<int>();
}
//new==============================================================================
bool jsonReadFromStr(String& json, String key, String& value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
ret = false;
}
value = doc[key].as<String>();
return ret;
}
bool jsonReadFromStr(String& json, String key, bool& value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
ret = false;
}
value = doc[key].as<bool>();
return ret;
}
bool jsonReadFromStr(String& json, String key, int& value) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json);
if (error) {
SerialPrint("EE", F("jsonRead"), error.f_str());
ret = false;
} else if (!doc.containsKey(key)) {
SerialPrint("EE", F("jsonRead"), key + " missing");
ret = false;
}
value = doc[key].as<int>();
return ret;
}
//=================================================================================
String jsonWriteStr(String& json, String name, String value) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE);