ArduinoJSON how to correctly serialise data

  arduino, arduinojson, c++, json

I’m using ArduinoJSON library to send JSON payload to Broker via MQTT library PubSub in my ESP8266. What is the correct way to use the following method found in the official website of the library:

struct SensorData {
   char name[32];
   int time;
   float value;
};

void writeSensorData(const SensorData& data, Stream& output)
{
  const size_t capacity = JSON_OBJECT_SIZE(3);
  StaticJsonDocument<capacity> doc;

  doc["name"] = data.name;
  doc["time"] = data.time;
  doc["value"] = data.value;

  serializeJson(data, output);
}

Unfortunately the example is incomplete, and I cannot figure out what is the Stream& output parameter. I thought of replacing it with a temporary buffer e.g char buffer[256], but I’m curious to know what is the Stream& output required here.

Here’s where I found the method: https://arduinojson.org/v6/how-to/reuse-a-json-document/

Source: Windows Questions C++

LEAVE A COMMENT