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

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"files.associations": {
"functional": "cpp"
}
}

View File

@@ -8,6 +8,8 @@ int jsonReadInt(String& json, String name);
boolean jsonReadBool(String& json, String name); boolean jsonReadBool(String& json, String name);
bool jsonReadFromStr(String& json, String key, String& value);
String jsonWriteStr(String& json, String name, String value); String jsonWriteStr(String& json, String name, String value);
String jsonWriteInt(String& json, String name, int value); String jsonWriteInt(String& json, String name, int value);

View File

@@ -5,7 +5,7 @@
#include "Global.h" #include "Global.h"
#include "Utils/FileUtils.h" #include "Utils/FileUtils.h"
//================================================================================= //depricated======================================================================
String jsonReadStr(String& json, String name) { String jsonReadStr(String& json, String name) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
@@ -27,6 +27,51 @@ int jsonReadInt(String& json, String name) {
return doc[name].as<int>(); 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) { String jsonWriteStr(String& json, String name, String value) {
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);

View File

@@ -88,14 +88,16 @@ void setup() {
SerialPrint("I", F("Test"), String(capacity)); SerialPrint("I", F("Test"), String(capacity));
ts.add( ts.add(
MYTEST, 10000, [&](void*) { MYTEST, 5000, [&](void*) {
//Serial.println(configSetupJson.length()); Serial.println(configSetupJson.length());
//Serial.println(F("--------------------------------------")); Serial.println(F("--------------------------------------"));
//Serial.println(jsonReadStr(configSetupJson, "apssidx"));
//Serial.println(jsonReadBool(configSetupJson, "telegonof")); String value;
//Serial.println(jsonReadInt(configSetupJson, "mqttPort")); if (jsonReadFromStr(configSetupJson, "name", value)) {
//Serial.println(jsonReadInt(configSetupJson, "uartTX")); Serial.println(value);
//Serial.println(F("--------------------------------------")); }
Serial.println(F("--------------------------------------"));
}, },
nullptr, true); nullptr, true);