How to packed JSON object over serial port (Serialise JSON object)

please format code with </> button * homework policy * asking questions
I wish to set PC to communicate with my microcontroller over serial port.
The data format i use JSON.
The microcontroller to PC is working well but i can not send from the PC to the controller.
write function has no support for JSON object data type.

How can I achieve that?

<
JSONObject payLoad=null;
payLoad.setBoolean(“POWER”, true);
payLoad.setBoolean(“PUMP”, true);
payLoad.setInt(“VALUE”, 67);

// write(payLoad);  // This will work. 
 >
  how should i handle this?

I assume write function supports text. JSON is almost always transferred as text. JSON-objects are constructs of programming languages to help handling of JSON. Anyway payLoad.tostring() will give you your JSON-object as a string

I did as recommended. .
But I am left another challenge.
No error report but no data is transmitted.

I did it this way.

String str_payload=payload.toString();
write (str_payload);

One other thing i notice:
anytime I am packing of JSON object inside the function call to SerialPortWrite.
I get null pointer to the json object instance.

Now to solve it my own way.
I use String to manually pack the JSON struct and then forward it to the serial write.
Something like:
String payload ="{";
payload +=“key:”;
payload +=str(value);
payload +="}";

write(payload);

This truly is crook way but since the data size is small from the PC to my microcontroller. I dont mine.

I still open to learn should someone know better way to handle that.

Something is did not mention before.

A call to Serialport is handle by thread, something like thread (“SerialPortWrite”)
Anytime i need to send data to microcontroller will create the
thread(“SerialPortWrite”);
could this be the reason?

What does documentation tell you?

Quite often print() happens instantly where write() puts stuff to a buffer and you need another function call to to send or process the buffer.

If my understanding is is right.

"write " function send data to the serial port. It send data out immediately they are loaded in it

while “print/println” send data to the console.

These are truly different.