mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 05:24:53 +01:00
Allow parse_json to return a boolean result (#6884)
* Allow parse_json to return a boolean result * Remove pass variable
This commit is contained in:
parent
51a8a7e875
commit
95e45dc12c
3 changed files with 14 additions and 12 deletions
|
@ -62,7 +62,7 @@ std::string build_json(const json_build_t &f) {
|
|||
}
|
||||
}
|
||||
|
||||
void parse_json(const std::string &data, const json_parse_t &f) {
|
||||
bool parse_json(const std::string &data, const json_parse_t &f) {
|
||||
// Here we are allocating 1.5 times the data size,
|
||||
// with the heap size minus 2kb to be safe if less than that
|
||||
// as we can not have a true dynamic sized document.
|
||||
|
@ -76,14 +76,13 @@ void parse_json(const std::string &data, const json_parse_t &f) {
|
|||
#elif defined(USE_LIBRETINY)
|
||||
const size_t free_heap = lt_heap_get_free();
|
||||
#endif
|
||||
bool pass = false;
|
||||
size_t request_size = std::min(free_heap, (size_t) (data.size() * 1.5));
|
||||
do {
|
||||
while (true) {
|
||||
DynamicJsonDocument json_document(request_size);
|
||||
if (json_document.capacity() == 0) {
|
||||
ESP_LOGE(TAG, "Could not allocate memory for JSON document! Requested %u bytes, free heap: %u", request_size,
|
||||
free_heap);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
DeserializationError err = deserializeJson(json_document, data);
|
||||
json_document.shrinkToFit();
|
||||
|
@ -91,21 +90,21 @@ void parse_json(const std::string &data, const json_parse_t &f) {
|
|||
JsonObject root = json_document.as<JsonObject>();
|
||||
|
||||
if (err == DeserializationError::Ok) {
|
||||
pass = true;
|
||||
f(root);
|
||||
return f(root);
|
||||
} else if (err == DeserializationError::NoMemory) {
|
||||
if (request_size * 2 >= free_heap) {
|
||||
ESP_LOGE(TAG, "Can not allocate more memory for deserialization. Consider making source string smaller");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
ESP_LOGV(TAG, "Increasing memory allocation.");
|
||||
request_size *= 2;
|
||||
continue;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "JSON parse error: %s", err.c_str());
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
} while (!pass);
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace esphome {
|
|||
namespace json {
|
||||
|
||||
/// Callback function typedef for parsing JsonObjects.
|
||||
using json_parse_t = std::function<void(JsonObject)>;
|
||||
using json_parse_t = std::function<bool(JsonObject)>;
|
||||
|
||||
/// Callback function typedef for building JsonObjects.
|
||||
using json_build_t = std::function<void(JsonObject)>;
|
||||
|
@ -23,7 +23,7 @@ using json_build_t = std::function<void(JsonObject)>;
|
|||
std::string build_json(const json_build_t &f);
|
||||
|
||||
/// Parse a JSON string and run the provided json parse function if it's valid.
|
||||
void parse_json(const std::string &data, const json_parse_t &f);
|
||||
bool parse_json(const std::string &data, const json_parse_t &f);
|
||||
|
||||
} // namespace json
|
||||
} // namespace esphome
|
||||
|
|
|
@ -410,7 +410,10 @@ void MQTTClientComponent::subscribe(const std::string &topic, mqtt_callback_t ca
|
|||
|
||||
void MQTTClientComponent::subscribe_json(const std::string &topic, const mqtt_json_callback_t &callback, uint8_t qos) {
|
||||
auto f = [callback](const std::string &topic, const std::string &payload) {
|
||||
json::parse_json(payload, [topic, callback](JsonObject root) { callback(topic, root); });
|
||||
json::parse_json(payload, [topic, callback](JsonObject root) -> bool {
|
||||
callback(topic, root);
|
||||
return true;
|
||||
});
|
||||
};
|
||||
MQTTSubscription subscription{
|
||||
.topic = topic,
|
||||
|
|
Loading…
Reference in a new issue