From 4ddbd97999113090a99b511c1c4d4cf441e1d291 Mon Sep 17 00:00:00 2001 From: biver Date: Sat, 29 Oct 2022 19:57:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B8=D1=80=D1=83=D0=B5=D0=BC=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D1=83=20=D1=81=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=D0=BC=D0=B8=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D1=8F=20JSON?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/JsonUtils.cpp | 74 +++++++++++++---------------------------- 1 file changed, 24 insertions(+), 50 deletions(-) diff --git a/src/utils/JsonUtils.cpp b/src/utils/JsonUtils.cpp index d35eb7d2..ab362ac3 100644 --- a/src/utils/JsonUtils.cpp +++ b/src/utils/JsonUtils.cpp @@ -12,108 +12,82 @@ void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value) { // new============================================================================== bool jsonRead(const String& json, String key, unsigned long& value, bool e) { - bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); if (error) { - if (e) { - SerialPrint("EE", F("jsonRead"), error.f_str()); - jsonErrorDetected(); - } - ret = false; + SerialPrint("EE", F("jsonRead"), error.f_str()); + jsonErrorDetected(); + return false; } else if (!doc.containsKey(key)) { if (e) { - SerialPrint("EE", F("jsonRead"), "json key '" + key + "' missing"); + SerialPrint("EE", F("jsonRead"), key + " missing"); jsonErrorDetected(); } - ret = false; + return false; } value = doc[key].as(); - return ret; + return true; } bool jsonRead(const String& json, String key, float& value, bool e) { - bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); if (error) { - if (e) { - SerialPrint("EE", F("jsonRead"), error.f_str()); - jsonErrorDetected(); - } - ret = false; + SerialPrint("EE", F("jsonRead"), error.f_str()); + jsonErrorDetected(); + return false; } else if (!doc.containsKey(key)) { if (e) { SerialPrint("EE", F("jsonRead"), key + " missing"); jsonErrorDetected(); } - ret = false; + return false; } value = doc[key].as(); - return ret; + return true; } bool jsonRead(const String& json, String key, String& value, bool e) { - bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); if (error) { - if (e) { - SerialPrint("EE", F("jsonRead"), error.f_str()); - jsonErrorDetected(); - } - ret = false; + SerialPrint("EE", F("jsonRead"), error.f_str()); + jsonErrorDetected(); + return false; } else if (!doc.containsKey(key)) { if (e) { SerialPrint("EE", F("jsonRead"), key + " missing"); jsonErrorDetected(); } - ret = false; + return false; } value = doc[key].as(); - return ret; + return true; } bool jsonRead(const String& json, String key, bool& value, bool e) { - bool ret = true; - DynamicJsonDocument doc(JSON_BUFFER_SIZE); - DeserializationError error = deserializeJson(doc, json); - if (error) { - if (e) { - SerialPrint("EE", F("jsonRead"), error.f_str()); - jsonErrorDetected(); - } - ret = false; - } else if (!doc.containsKey(key)) { - if (e) { - SerialPrint("EE", F("jsonRead"), key + " missing"); - jsonErrorDetected(); - } - ret = false; - } - value = doc[key].as(); + int lvalue = value; + bool ret = jsonRead(json, key, lvalue, e); + value = lvalue; return ret; } bool jsonRead(const String& json, String key, int& value, bool e) { - bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); if (error) { - if (e) { - SerialPrint("EE", F("jsonRead"), error.f_str()); - jsonErrorDetected(); - } - ret = false; + SerialPrint("EE", F("jsonRead"), error.f_str()); + jsonErrorDetected(); + return false; } else if (!doc.containsKey(key)) { if (e) { SerialPrint("EE", F("jsonRead"), key + " missing"); jsonErrorDetected(); } - ret = false; + return false; } value = doc[key].as(); - return ret; + return true; } // new==============================================================================