diff --git a/include/Utils/JsonUtils.h b/include/Utils/JsonUtils.h index 1fd43b58..51c73b34 100644 --- a/include/Utils/JsonUtils.h +++ b/include/Utils/JsonUtils.h @@ -3,21 +3,22 @@ #include String jsonReadStr(String& json, String name); - 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); - String jsonWriteFloat(String& json, String name, float value); - String jsonWriteBool(String& json, String name, boolean value); -void saveConfig(); +bool jsonRead(String& json, String key, String& value); +bool jsonRead(String& json, String key, bool& value); +bool jsonRead(String& json, String key, int& value); +bool jsonWrite(String& json, String name, String value); +bool jsonWrite(String& json, String name, bool value); +bool jsonWrite(String& json, String name, int value); +bool jsonWrite(String& json, String name, float value); + +void saveConfig(); void saveStore(); \ No newline at end of file diff --git a/src/Utils/JsonUtils.cpp b/src/Utils/JsonUtils.cpp index 577cc497..fd235119 100644 --- a/src/Utils/JsonUtils.cpp +++ b/src/Utils/JsonUtils.cpp @@ -28,7 +28,7 @@ int jsonReadInt(String& json, String name) { } //new============================================================================== -bool jsonReadFromStr(String& json, String key, String& value) { +bool jsonRead(String& json, String key, String& value) { bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); @@ -43,7 +43,7 @@ bool jsonReadFromStr(String& json, String key, String& value) { return ret; } -bool jsonReadFromStr(String& json, String key, bool& value) { +bool jsonRead(String& json, String key, bool& value) { bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); @@ -58,7 +58,7 @@ bool jsonReadFromStr(String& json, String key, bool& value) { return ret; } -bool jsonReadFromStr(String& json, String key, int& value) { +bool jsonRead(String& json, String key, int& value) { bool ret = true; DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); @@ -72,7 +72,7 @@ bool jsonReadFromStr(String& json, String key, int& value) { value = doc[key].as(); return ret; } -//================================================================================= +//depricated======================================================================== String jsonWriteStr(String& json, String name, String value) { DynamicJsonDocument doc(JSON_BUFFER_SIZE); DeserializationError error = deserializeJson(doc, json); @@ -113,6 +113,62 @@ String jsonWriteFloat(String& json, String name, float value) { return json; } +//new============================================================================== +bool jsonWrite(String& json, String name, String value) { + bool ret = true; + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DeserializationError error = deserializeJson(doc, json); + if (error) { + SerialPrint("EE", F("jsonWrite"), error.f_str()); + ret = false; + } + doc[name] = value; + json = ""; + serializeJson(doc, json); + return ret; +} + +bool jsonWrite(String& json, String name, bool value) { + bool ret = true; + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DeserializationError error = deserializeJson(doc, json); + if (error) { + SerialPrint("EE", F("jsonWrite"), error.f_str()); + ret = false; + } + doc[name] = value; + json = ""; + serializeJson(doc, json); + return ret; +} + +bool jsonWrite(String& json, String name, int value) { + bool ret = true; + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DeserializationError error = deserializeJson(doc, json); + if (error) { + SerialPrint("EE", F("jsonWrite"), error.f_str()); + ret = false; + } + doc[name] = value; + json = ""; + serializeJson(doc, json); + return ret; +} + +bool jsonWrite(String& json, String name, float value) { + bool ret = true; + DynamicJsonDocument doc(JSON_BUFFER_SIZE); + DeserializationError error = deserializeJson(doc, json); + if (error) { + SerialPrint("EE", F("jsonWrite"), error.f_str()); + ret = false; + } + doc[name] = value; + json = ""; + serializeJson(doc, json); + return ret; +} //================================================================================= void saveConfig() { writeFile(String("config.json"), configSetupJson); diff --git a/src/main.cpp b/src/main.cpp index 10d1f0d1..d86749a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -92,9 +92,26 @@ void setup() { Serial.println(configSetupJson.length()); Serial.println(F("--------------------------------------")); - String value; - if (jsonReadFromStr(configSetupJson, "name", value)) { - Serial.println(value); + if (jsonWrite(configSetupJson, "name", "test value")) { + Serial.println("write sucsess"); + } + + Serial.print("str test: "); + String valueStr; + if (jsonRead(configSetupJson, "name", valueStr)) { + Serial.println(valueStr); + } + + Serial.print("int test: "); + int valueInt; + if (jsonRead(configSetupJson, "mqttPort", valueInt)) { + Serial.println(valueInt); + } + + Serial.print("bool test: "); + bool valueBool; + if (jsonRead(configSetupJson, "telegonof", valueBool)) { + Serial.println(valueBool); } Serial.println(F("--------------------------------------"));