From bd6fa0fc3723be3ad0f9e423704e99b548af069a Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko Date: Sat, 9 Oct 2021 18:48:02 +0000 Subject: [PATCH] json utils updated --- .vscode/settings.json | 5 +++++ include/Utils/JsonUtils.h | 2 ++ src/Utils/JsonUtils.cpp | 47 ++++++++++++++++++++++++++++++++++++++- src/main.cpp | 18 ++++++++------- 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..1507357b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "functional": "cpp" + } +} \ No newline at end of file diff --git a/include/Utils/JsonUtils.h b/include/Utils/JsonUtils.h index 2b19dcc5..1fd43b58 100644 --- a/include/Utils/JsonUtils.h +++ b/include/Utils/JsonUtils.h @@ -8,6 +8,8 @@ int jsonReadInt(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 jsonWriteInt(String& json, String name, int value); diff --git a/src/Utils/JsonUtils.cpp b/src/Utils/JsonUtils.cpp index cb6500bc..577cc497 100644 --- a/src/Utils/JsonUtils.cpp +++ b/src/Utils/JsonUtils.cpp @@ -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(); } +//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(); + 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(); + 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(); + return ret; +} //================================================================================= String jsonWriteStr(String& json, String name, String value) { DynamicJsonDocument doc(JSON_BUFFER_SIZE); diff --git a/src/main.cpp b/src/main.cpp index d180fc11..10d1f0d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,14 +88,16 @@ void setup() { SerialPrint("I", F("Test"), String(capacity)); ts.add( - MYTEST, 10000, [&](void*) { - //Serial.println(configSetupJson.length()); - //Serial.println(F("--------------------------------------")); - //Serial.println(jsonReadStr(configSetupJson, "apssidx")); - //Serial.println(jsonReadBool(configSetupJson, "telegonof")); - //Serial.println(jsonReadInt(configSetupJson, "mqttPort")); - //Serial.println(jsonReadInt(configSetupJson, "uartTX")); - //Serial.println(F("--------------------------------------")); + MYTEST, 5000, [&](void*) { + Serial.println(configSetupJson.length()); + Serial.println(F("--------------------------------------")); + + String value; + if (jsonReadFromStr(configSetupJson, "name", value)) { + Serial.println(value); + } + + Serial.println(F("--------------------------------------")); }, nullptr, true);