2022-02-15 11:37:31 +01:00
|
|
|
|
#include "utils/JsonUtils.h"
|
|
|
|
|
|
#include "utils/FileUtils.h"
|
2021-12-22 23:33:47 +01:00
|
|
|
|
|
2021-12-25 00:19:58 +01:00
|
|
|
|
// new================================================================================
|
2023-10-06 01:26:16 +02:00
|
|
|
|
String jsonReadStrDoc(DynamicJsonDocument &doc, String name) {
|
2021-12-22 23:33:47 +01:00
|
|
|
|
return doc[name].as<String>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
void jsonWriteStrDoc(DynamicJsonDocument &doc, String name, String value) {
|
2021-12-24 22:49:06 +01:00
|
|
|
|
doc[name] = value;
|
2021-12-22 23:33:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// new==============================================================================
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonRead(const String &json, String key, long &value, bool e) {
|
2022-02-01 20:39:51 +03:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2023-10-06 01:26:16 +02:00
|
|
|
|
} else if (!doc.containsKey(key)) {
|
|
|
|
|
|
if (e) {
|
2023-02-08 09:37:20 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2022-02-01 20:39:51 +03:00
|
|
|
|
}
|
2022-11-04 16:42:30 +03:00
|
|
|
|
value = doc[key].as<long>();
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return true;
|
2022-02-01 20:39:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonRead(const String &json, String key, float &value, bool e) {
|
2022-02-01 20:28:17 +03:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2023-10-06 01:26:16 +02:00
|
|
|
|
} else if (!doc.containsKey(key)) {
|
|
|
|
|
|
if (e) {
|
2023-02-08 09:37:20 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2022-02-01 20:28:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
value = doc[key].as<float>();
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return true;
|
2022-02-01 20:28:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonRead(const String &json, String key, String &value, bool e) {
|
2021-12-22 23:33:47 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2023-10-06 01:26:16 +02:00
|
|
|
|
} else if (!doc.containsKey(key)) {
|
|
|
|
|
|
if (e) {
|
2023-02-08 09:37:20 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2021-12-22 23:33:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
value = doc[key].as<String>();
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return true;
|
2021-12-22 23:33:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonRead(const String &json, String key, bool &value, bool e) {
|
2022-10-29 19:57:09 +03:00
|
|
|
|
int lvalue = value;
|
|
|
|
|
|
bool ret = jsonRead(json, key, lvalue, e);
|
|
|
|
|
|
value = lvalue;
|
2021-12-22 23:33:47 +01:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonRead(const String &json, String key, int &value, bool e) {
|
2021-12-22 23:33:47 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2023-10-06 01:26:16 +02:00
|
|
|
|
} else if (!doc.containsKey(key)) {
|
|
|
|
|
|
if (e) {
|
2023-02-08 09:37:20 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), key + " missing in " + json);
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return false;
|
2021-12-22 23:33:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
value = doc[key].as<int>();
|
2022-10-29 19:57:09 +03:00
|
|
|
|
return true;
|
2021-12-22 23:33:47 +01:00
|
|
|
|
}
|
2021-12-24 22:49:06 +01:00
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonReadArray(const String &json, String key, std::vector<String> &jArray, bool e) {
|
2023-09-24 20:58:51 +03:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2023-09-24 20:58:51 +03:00
|
|
|
|
SerialPrint("E", F("jsonReadArray"), error.f_str());
|
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
2023-10-06 01:26:16 +02:00
|
|
|
|
} else if (!doc.containsKey(key)) {
|
|
|
|
|
|
if (e) {
|
2023-09-24 20:58:51 +03:00
|
|
|
|
SerialPrint("E", F("jsonReadArray"), key + " missing in " + json);
|
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2023-10-01 19:55:09 +03:00
|
|
|
|
// SerialPrint("E", F("jsonReadArray"), key + " doc " + doc[key].as<String>());
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (doc[key].is<JsonArray>()) {
|
2023-10-01 19:55:09 +03:00
|
|
|
|
for (int8_t i = 0; i < doc[key].size(); i++)
|
|
|
|
|
|
jArray.push_back(doc[key][i].as<String>());
|
|
|
|
|
|
// SerialPrint("E", F("jsonReadArray"), "isArray"+key + " doc " + doc[key].as<String>());
|
2023-10-06 01:26:16 +02:00
|
|
|
|
} else {
|
2023-10-01 19:55:09 +03:00
|
|
|
|
jArray.push_back(doc[key].as<String>());
|
|
|
|
|
|
// DynamicJsonDocument docArr(JSON_BUFFER_SIZE/5);
|
|
|
|
|
|
// jArray = doc[key].as<JsonArray>();
|
|
|
|
|
|
// String tmp = doc[key].as<String>();
|
|
|
|
|
|
// jArray.add("dsdsd");
|
|
|
|
|
|
// SerialPrint("E", F("jsonReadArray"), "notArray"+key + " doc " + doc[key].as<String>());
|
|
|
|
|
|
// SerialPrint("E", F("jsonReadArray"), "count: " + String(jArray.size()) +" key: " + key + " arr " + jArray[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
// SerialPrint("E", F("jsonReadArray"), "count: " + String(jArray.size()) +" key: " + key + " doc " + jArray[0].as<String>());
|
2023-09-24 20:58:51 +03:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-25 00:19:58 +01:00
|
|
|
|
// new==============================================================================
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonWriteStr_(String &json, const String &key, const String &value, bool e) {
|
2021-12-25 00:19:58 +01:00
|
|
|
|
bool ret = true;
|
|
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2021-12-25 00:19:58 +01:00
|
|
|
|
ret = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
doc[key] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonWriteBool_(String &json, const String &key, bool value, bool e) {
|
2021-12-25 00:19:58 +01:00
|
|
|
|
bool ret = true;
|
|
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2021-12-25 00:19:58 +01:00
|
|
|
|
ret = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
doc[key] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonWriteInt_(String &json, const String &key, int value, bool e) {
|
2021-12-25 00:19:58 +01:00
|
|
|
|
bool ret = true;
|
|
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2021-12-25 00:19:58 +01:00
|
|
|
|
ret = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
doc[key] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonWriteFloat_(String &json, const String &key, float value, bool e) {
|
2021-12-25 00:19:58 +01:00
|
|
|
|
bool ret = true;
|
|
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2021-12-25 00:19:58 +01:00
|
|
|
|
ret = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
doc[key] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
void writeUint8tValueToJsonString(uint8_t *payload, size_t length, size_t headerLenth, String &json) {
|
2022-02-09 21:55:17 +01:00
|
|
|
|
String payloadStr;
|
|
|
|
|
|
payloadStr.reserve(length + 1);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
for (size_t i = headerLenth; i < length; i++) {
|
2022-02-09 21:55:17 +01:00
|
|
|
|
payloadStr += (char)payload[i];
|
|
|
|
|
|
}
|
2022-02-13 16:13:51 +01:00
|
|
|
|
jsonMergeObjects(json, payloadStr);
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
bool jsonMergeObjects(String &json1, String &json2, bool e) {
|
2022-02-09 21:55:17 +01:00
|
|
|
|
bool ret = true;
|
|
|
|
|
|
DynamicJsonDocument doc1(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error1 = deserializeJson(doc1, json1);
|
|
|
|
|
|
DynamicJsonDocument doc2(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error2 = deserializeJson(doc2, json2);
|
|
|
|
|
|
jsonMergeDocs(doc1.as<JsonObject>(), doc2.as<JsonObject>());
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error1 || error2) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("json"), "jsonMergeObjects error");
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
ret = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
json1 = "";
|
|
|
|
|
|
serializeJson(doc1, json1);
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
void jsonMergeDocs(JsonObject dest, JsonObjectConst src) {
|
|
|
|
|
|
for (auto kvp : src) {
|
2022-02-09 21:55:17 +01:00
|
|
|
|
dest[kvp.key()] = kvp.value();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-24 22:49:06 +01:00
|
|
|
|
// depricated======================================================================
|
2023-10-06 01:26:16 +02:00
|
|
|
|
String jsonReadStr(const String &json, String name, bool e) {
|
2021-12-24 22:49:06 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
2021-12-24 22:49:06 +01:00
|
|
|
|
return doc[name].as<String>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
boolean jsonReadBool(const String &json, String name, bool e) {
|
2021-12-24 22:49:06 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
2021-12-24 22:49:06 +01:00
|
|
|
|
return doc[name].as<bool>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
int jsonReadInt(const String &json, String name, bool e) {
|
2021-12-24 22:49:06 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
2021-12-24 22:49:06 +01:00
|
|
|
|
return doc[name].as<int>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
long int jsonReadLInt(const String &json, String name, bool e) {
|
2023-06-25 18:41:46 +02:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2023-06-25 18:41:46 +02:00
|
|
|
|
SerialPrint("E", F("jsonRead"), error.f_str());
|
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return doc[name].as<long int>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-22 23:33:47 +01:00
|
|
|
|
// depricated========================================================================
|
2023-10-06 01:26:16 +02:00
|
|
|
|
String jsonWriteStr(String &json, String name, String value, bool e) {
|
2021-12-22 23:33:47 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
2021-12-22 23:33:47 +01:00
|
|
|
|
doc[name] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return json;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
String jsonWriteBool(String &json, String name, boolean value, bool e) {
|
2021-12-22 23:33:47 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
2021-12-22 23:33:47 +01:00
|
|
|
|
doc[name] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return json;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
String jsonWriteInt(String &json, String name, int value, bool e) {
|
2021-12-22 23:33:47 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
2021-12-22 23:33:47 +01:00
|
|
|
|
doc[name] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return json;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
String jsonWriteFloat(String &json, String name, float value, bool e) {
|
2021-12-22 23:33:47 +01:00
|
|
|
|
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, json);
|
2023-10-06 01:26:16 +02:00
|
|
|
|
if (error) {
|
|
|
|
|
|
if (e) {
|
2022-11-04 16:42:30 +03:00
|
|
|
|
SerialPrint("E", F("jsonWrite"), error.f_str());
|
2022-02-22 16:40:46 +01:00
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|
2021-12-22 23:33:47 +01:00
|
|
|
|
doc[name] = value;
|
|
|
|
|
|
json = "";
|
|
|
|
|
|
serializeJson(doc, json);
|
|
|
|
|
|
return json;
|
|
|
|
|
|
}
|
2022-02-09 21:55:17 +01:00
|
|
|
|
|
2023-10-06 01:26:16 +02:00
|
|
|
|
void jsonErrorDetected() {
|
2022-10-12 03:14:55 +02:00
|
|
|
|
// пример как отправить ошибку с количеством
|
|
|
|
|
|
// jsonWriteInt(errorsHeapJson, F("jse2"), 1);
|
|
|
|
|
|
// int number = jsonReadInt(errorsHeapJson, F("jse2n"));
|
|
|
|
|
|
// number++;
|
|
|
|
|
|
// jsonWriteInt(errorsHeapJson, F("jse2n"), number);
|
2022-02-09 21:55:17 +01:00
|
|
|
|
}
|