Skip to content

Crash in serializeJson with Empty Values after Update (Arduino-ESP32 3.1.2) #10971

Closed
@hosseinaghaie

Description

@hosseinaghaie

Board

Board: ESP32-S3

Device Description

Description:
After updating to Arduino-ESP32 3.1.2, we observed frequent crashes when using serializeJson with DynamicJsonDocument. The issue occurs specifically when handling empty or minimal JSON objects like {} or when the serialized String is empty (""). This behavior was not present in previous versions and might be related to recent changes in String behavior (String::move fix). The crash generates a Guru Meditation Error (LoadProhibited).

Example Code:
DynamicJsonDocument doc(2048);
doc["data"] = "{}";
String json;
serializeJson(doc, json); // Causes crash

We also experience similar crashes when serializing responses with minimal data (doc["data"] = {} or []), which should be valid JSON structures.

By switching to std::string for handling serialization, the crash is avoided. The modified code is shown below:

#include // Use std::string

DynamicJsonDocument doc(2048);
doc["data"] = "{}";
std::string jsonString;
serializeJson(doc, jsonString); // No crash
This might suggest an underlying issue with String conversion in the recent core update.

Environment:
Board: ESP32-S3
Core Version: Arduino-ESP32 3.1.2
ArduinoJson Version: 7.3.0
The issue seems closely related to serializeJson combined with String. Let me know if you need additional debug logs or a full backtrace.

Hardware Configuration

no

Version

v3.1.2

IDE Name

Arduino 2.3.4

Operating System

11

Flash frequency

40m

PSRAM enabled

no

Upload speed

115200

Description

no

Sketch

//////////////////////////////////////////////////////////////////////////////////////////////
void processRawInput(String rawData, String source) {
   Serial.println("Raw data to be parsed: " + rawData);

  // Parse JSON from the raw data
  DynamicJsonDocument doc(2048);  // Adjusted for larger data
  DeserializationError error = deserializeJson(doc, rawData);

  if (error) {
    Serial.println("Invalid JSON received from source: " + source);
    return;
  }

  // Extract command details
  CommandData cmdData;
  cmdData.source = source;
  cmdData.sequenceId = doc["sequenceId"] | String(time(NULL));

  if (doc.containsKey("data")) {
    // Serialize the "data" object into a string and store it in cmdData.data
    //String jsonString;
   std::string jsonString;
    serializeJson(doc["data"], jsonString);
   // cmdData.data = jsonString;
    cmdData.data = String(jsonString.c_str());  // Convert safely back to String
  } else {
    Serial.println("Error: 'data' key is missing or invalid.");
    return;
  }

  String commandName = doc["command"] | "";
  if (commandName.isEmpty()) {
    Serial.println("Error: 'command' key is missing or invalid.");
    return;
  }

  // Process the command
  processCommand(commandName, cmdData);
  
}




### Debug Message

```plain
Decoding stack results
0x420405c0:  is in String::operator=(char const*) (C:\Users\Admin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.2\cores\esp32\WString.cpp:293).
0x42016ddf:  is in ArduinoJson::V730PB22::detail::serialize<ArduinoJson::V730PB22::detail::JsonSerializer, String>(ArduinoJson::V730PB22::JsonVariantConst, String&) (c:\Users\Admin\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Serialization/Writers/ArduinoStringWriter.hpp:19).
0x42016e69:  is in ArduinoJson::V730PB22::serializeJson<String, 0>(ArduinoJson::V730PB22::JsonVariantConst, String&) (c:\Users\Admin\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Json/JsonSerializer.hpp:137).

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Pending MergePull Request is ready to be mergedType: RegressionResult of unforeseen consequences of a previous changebug 🐞Inconsistencies or issues which will cause a problem for users or implementers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions