9static const char *
const TAG =
"json";
21 JsonObject root = builder.
root();
29 JsonDocument doc =
parse_json(
reinterpret_cast<const uint8_t *
>(data.c_str()), data.size());
30 if (doc.overflowed() || doc.isNull())
32 return f(doc.as<JsonObject>());
38 if (data ==
nullptr ||
len == 0) {
39 ESP_LOGE(TAG,
"No data to parse");
43 JsonDocument json_document(&global_json_allocator);
45 JsonDocument json_document;
47 if (json_document.overflowed()) {
48 ESP_LOGE(TAG,
"Could not allocate memory for JSON document!");
51 DeserializationError err = deserializeJson(json_document, data,
len);
53 if (err == DeserializationError::Ok) {
55 }
else if (err == DeserializationError::NoMemory) {
56 ESP_LOGE(TAG,
"Can not allocate more memory for deserialization. Consider making source string smaller");
59 ESP_LOGE(TAG,
"Parse error: %s", err.c_str());
65 if (doc_.overflowed()) {
66 ESP_LOGE(TAG,
"JSON document overflow");
70 serializeJson(doc_, output);
Builder class for creating JSON documents without lambdas.
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
bool parse_json(const std::string &data, const json_parse_t &f)
Parse a JSON string and run the provided json parse function if it's valid.
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
std::function< bool(JsonObject)> json_parse_t
Callback function typedef for parsing JsonObjects.
Providing packet encoding functions for exchanging data with a remote host.