Char arrays and ArduinoJson

Hello,

This is a general question about serializing a JSON doc into a char array that gets passed into a function by pointer or by reference.

For example, let’s say I have a function similar to this:

void Sensor::measure(char* dbuf) 
{
  // Take some measurements...
  StaticJsonDocument<200> doc;
  // Store the measurements...

  // Serialize the result
  serializeJson(doc, dbuf);
}

After creating the sensor object, I call the function:

char data_buf[1024];
void loop() {
    sensor.measure(data_buf);
    Serial.println(data_buf);
    delay(1000);
}

But I get a long error message saying (in part):

ArduinoJson/Serialization/Writer.hpp:18:26: error: request for member ‘write’ in ‘((ArduinoJson6151_0000010::Writer<char, void>)this)->ArduinoJson6151_0000010::Writer<char, void>::_dest’, which is of non-class type ‘char*’
return _dest->write(c);

How do I pass by pointer or reference properly to get this to work? (I don’t want to use an Arduino String because I want to send the data via MQTT as a char array).

Many thanks!

So in your loop() method, you define a char array and pass that in by value to measure(), but measure is looking for a pointer to a char.

Would you not want to define your input parameter for measure() as a pass by value to a char array like so
measure (char dbuf[]) ?

davie_gravy:
Would you not want to define your input parameter for measure() as a pass by value reference to a char array like so

measure (char dbuf[])

?

Or, you could do:

sensor.measure(&data_buf);