Оптимизируем работу с функциями чтения JSON

This commit is contained in:
2022-10-29 19:57:09 +03:00
parent 7dc21ee914
commit 4ddbd97999

View File

@@ -12,108 +12,82 @@ void jsonWriteStrDoc(DynamicJsonDocument& doc, String name, String value) {
// new============================================================================== // new==============================================================================
bool jsonRead(const String& json, String key, unsigned long& value, bool e) { bool jsonRead(const String& json, String key, unsigned long& value, bool e) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
if (e) {
SerialPrint("EE", F("jsonRead"), error.f_str()); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected(); jsonErrorDetected();
} return false;
ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
if (e) { if (e) {
SerialPrint("EE", F("jsonRead"), "json key '" + key + "' missing"); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected(); jsonErrorDetected();
} }
ret = false; return false;
} }
value = doc[key].as<unsigned long>(); value = doc[key].as<unsigned long>();
return ret; return true;
} }
bool jsonRead(const String& json, String key, float& value, bool e) { bool jsonRead(const String& json, String key, float& value, bool e) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
if (e) {
SerialPrint("EE", F("jsonRead"), error.f_str()); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected(); jsonErrorDetected();
} return false;
ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
if (e) { if (e) {
SerialPrint("EE", F("jsonRead"), key + " missing"); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected(); jsonErrorDetected();
} }
ret = false; return false;
} }
value = doc[key].as<float>(); value = doc[key].as<float>();
return ret; return true;
} }
bool jsonRead(const String& json, String key, String& value, bool e) { bool jsonRead(const String& json, String key, String& value, bool e) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
if (e) {
SerialPrint("EE", F("jsonRead"), error.f_str()); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected(); jsonErrorDetected();
} return false;
ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
if (e) { if (e) {
SerialPrint("EE", F("jsonRead"), key + " missing"); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected(); jsonErrorDetected();
} }
ret = false; return false;
} }
value = doc[key].as<String>(); value = doc[key].as<String>();
return ret; return true;
} }
bool jsonRead(const String& json, String key, bool& value, bool e) { bool jsonRead(const String& json, String key, bool& value, bool e) {
bool ret = true; int lvalue = value;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); bool ret = jsonRead(json, key, lvalue, e);
DeserializationError error = deserializeJson(doc, json); value = lvalue;
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>();
return ret; return ret;
} }
bool jsonRead(const String& json, String key, int& value, bool e) { bool jsonRead(const String& json, String key, int& value, bool e) {
bool ret = true;
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
DeserializationError error = deserializeJson(doc, json); DeserializationError error = deserializeJson(doc, json);
if (error) { if (error) {
if (e) {
SerialPrint("EE", F("jsonRead"), error.f_str()); SerialPrint("EE", F("jsonRead"), error.f_str());
jsonErrorDetected(); jsonErrorDetected();
} return false;
ret = false;
} else if (!doc.containsKey(key)) { } else if (!doc.containsKey(key)) {
if (e) { if (e) {
SerialPrint("EE", F("jsonRead"), key + " missing"); SerialPrint("EE", F("jsonRead"), key + " missing");
jsonErrorDetected(); jsonErrorDetected();
} }
ret = false; return false;
} }
value = doc[key].as<int>(); value = doc[key].as<int>();
return ret; return true;
} }
// new============================================================================== // new==============================================================================