bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.63k stars 1.1k forks source link

JSON output is truncated. #2100

Closed the-maldridge closed 1 month ago

the-maldridge commented 1 month ago

Description I assemble data into a JsonDocument prior to serializing it into an MQTT Topic. This used to work fine, but I'm now observing that the output is truncated in the last key. It is always the last key, and by a variable amount each time I reset the board.

Troubleshooter's report

  1. The program uses ArduinoJson 7
  2. The issue happens at run time
  3. The issue concerns serialization
  4. Output is incomplete
  5. JsonDocument::overflowed() returns false
  6. The empty/missing value is neither an array, object, nor string

Environment

Reproduction code

void statusReport() {
  JsonDocument posting;
  posting["ControlFrameAge"] = millis() - controlFrameAge;
  posting["ControlFramesReceived"] = boardState.FramesReceived;
  posting["WifiReconnects"] = boardState.WifiReconnects;
  posting["VBat"] = boardState.VBat;
  posting["VBatM"] = int(pinStatusPwrSupplyTuneM * 100000);
  posting["VBatB"] = int(pinStatusPwrSupplyTuneB * 100000);
  posting["WatchdogRemaining"] = boardState.WatchdogRemaining;
  posting["RSSI"] = boardState.RSSI;
  posting["PwrBoard"] = boardState.PwrBoard;
  posting["PwrPico"] = boardState.PwrPico;
  posting["PwrGPIO"] = boardState.PwrGPIO;
  posting["PwrServo"] = boardState.PwrServo;
  posting["PwrMainA"] = boardState.PwrMainA;
  posting["PwrMainB"] = boardState.PwrMainB;
  posting["PwrPixels"] = boardState.PwrPixels;
  posting["WatchdogOK"] = boardState.WatchdogOK;

  // Push to the FMS                                                                                                                                                                                                                          
  mqtt.beginMessage(cfg.mqttTopicStats);
  serializeJson(posting, mqtt);
  mqtt.endMessage();
}

Remarks The issue manifested after I added VBatM and VBatB to the posting.

the-maldridge commented 1 month ago

Manual reading is an artform best not practiced in the wee hours of the morning. After revisiting and rereading the docs, I determined that my structure had passed the magic 256 byte size and it was necessary to specify the size of the buffers directly.