mirror of
https://github.com/esphome/esphome.git
synced 2025-10-08 12:53:45 +01:00
[json] Add parse_json
overload for const char *
(#11039)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
@@ -19,15 +19,19 @@ std::string build_json(const json_build_t &f) {
|
||||
|
||||
bool parse_json(const std::string &data, const json_parse_t &f) {
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||
JsonDocument doc = parse_json(data);
|
||||
JsonDocument doc = parse_json(reinterpret_cast<const uint8_t *>(data.c_str()), data.size());
|
||||
if (doc.overflowed() || doc.isNull())
|
||||
return false;
|
||||
return f(doc.as<JsonObject>());
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
}
|
||||
|
||||
JsonDocument parse_json(const std::string &data) {
|
||||
JsonDocument parse_json(const uint8_t *data, size_t len) {
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||
if (data == nullptr || len == 0) {
|
||||
ESP_LOGE(TAG, "No data to parse");
|
||||
return JsonObject(); // return unbound object
|
||||
}
|
||||
#ifdef USE_PSRAM
|
||||
auto doc_allocator = SpiRamAllocator();
|
||||
JsonDocument json_document(&doc_allocator);
|
||||
@@ -38,7 +42,7 @@ JsonDocument parse_json(const std::string &data) {
|
||||
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
||||
return JsonObject(); // return unbound object
|
||||
}
|
||||
DeserializationError err = deserializeJson(json_document, data);
|
||||
DeserializationError err = deserializeJson(json_document, data, len);
|
||||
|
||||
if (err == DeserializationError::Ok) {
|
||||
return json_document;
|
||||
|
@@ -50,8 +50,13 @@ std::string build_json(const json_build_t &f);
|
||||
|
||||
/// Parse a JSON string and run the provided json parse function if it's valid.
|
||||
bool parse_json(const std::string &data, const json_parse_t &f);
|
||||
|
||||
/// Parse a JSON string and return the root JsonDocument (or an unbound object on error)
|
||||
JsonDocument parse_json(const std::string &data);
|
||||
JsonDocument parse_json(const uint8_t *data, size_t len);
|
||||
/// Parse a JSON string and return the root JsonDocument (or an unbound object on error)
|
||||
inline JsonDocument parse_json(const std::string &data) {
|
||||
return parse_json(reinterpret_cast<const uint8_t *>(data.c_str()), data.size());
|
||||
}
|
||||
|
||||
/// Builder class for creating JSON documents without lambdas
|
||||
class JsonBuilder {
|
||||
|
Reference in New Issue
Block a user