From fabe47bed43b0822beaa76310845962007ae56e5 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 20:39:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D0=B5=20Json=20?= =?UTF-8?q?=D0=B2=20unsigned=20long?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/utils/JsonUtils.h | 2 ++ src/utils/JsonUtils.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/utils/JsonUtils.h b/include/utils/JsonUtils.h index 9053ab41..c7ab1c61 100644 --- a/include/utils/JsonUtils.h +++ b/include/utils/JsonUtils.h @@ -10,6 +10,8 @@ extern String jsonWriteInt(String& json, String name, int value); extern String jsonWriteFloat(String& json, String name, float value); extern String jsonWriteBool(String& json, String name, boolean value); +extern bool jsonRead(String& json, String key, unsigned long& value); +extern bool jsonRead(String& json, String key, float& value); extern bool jsonRead(String& json, String key, String& value); extern bool jsonRead(String& json, String key, bool& value); extern bool jsonRead(String& json, String key, int& value); diff --git a/src/utils/JsonUtils.cpp b/src/utils/JsonUtils.cpp index 8e9e39a1..a02df298 100644 --- a/src/utils/JsonUtils.cpp +++ b/src/utils/JsonUtils.cpp @@ -12,6 +12,21 @@ void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value) { } // new============================================================================== +bool jsonRead(String& json, String key, unsigned long& 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 jsonRead(String& json, String key, float& value) { bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE);