Save struct bytes in json object

is there a way to store the bytes of a struct into a json objet so i can send the whole struct as bytes instead of sending each variable 1 at a time?

currently i setup the json object like this,

void sendMessage()
{
  DynamicJsonDocument doc(2024);
  doc["ID"] = "NODEUV";
  doc["lightStatus"] = _uvStruct.lightsOn;
  doc["runTimes"] = _uvStruct.runTimes;
  doc["photoSensor"] = _uvStruct.photoSensor;
  doc["timeOn"] = _uvStruct.timeOn;
  doc["timeOff"] = _uvStruct.timeOff;
  doc["systemStatus"] = _uvStruct.systemStatus;
  doc["timerOn"] = _uvStruct.timerOn;
  doc["SecondsOn"] = _uvStruct.SecondsOn;
  doc["r1Status"] = _uvStruct.r1Status;
  doc["r2Status"] = _uvStruct.r2Status;
  doc["r3Status"] = _uvStruct.r3Status;
  doc["r4Status"] = _uvStruct.r4Status;
  String msg ;
  serializeJson(doc, msg);
  mesh.sendBroadcast( msg );
  Serial.println(msg);
  taskSendMessage.setInterval((TASK_SECOND * 1));
}

and i receive it like this, "just reading the "id"

void receivedCallback( const uint32_t &from, const String &msg ) {
  Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
  String json;
  DynamicJsonDocument doc(2024);
  json = msg.c_str();
  DeserializationError error = deserializeJson(doc, json);
  if (error)
  {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
  }
      String id = doc["ID"];
  if (strcmp(doc["ID"], "NODEUV") == 0) {
Serial.println("it equals the id");
    Serial.print(id);
  }
}

i would like to send it like this,

  DynamicJsonDocument doc(2024);
  doc["ID"] = "NODEUV";
  doc["struct"] = &_struct;
  String msg ;
  serializeJson(doc, msg);
  mesh.sendBroadcast( msg );
  Serial.println(msg);
  taskSendMessage.setInterval((TASK_SECOND * 1));
}

possibly read it like this

void receivedCallback( const uint32_t &from, const String &msg ) {
  Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
  String json;
  DynamicJsonDocument doc(2024);
  json = msg.c_str();
  DeserializationError error = deserializeJson(doc, json);
  if (error)
  {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
  }
      String id = doc["ID"];
  if (strcmp(doc["ID"], "NODEUV") == 0) {
_struct = doc["struct"];
Serial.println("it equals the id");
    Serial.print(id);
  }
}

can someone help me out here?

For a "serious" project, I'd recommend using Protocol Buffers to serialize your structs instead of JSON. It'll be faster and more efficient.

If you know that the packing and Endianness of your struct will be the same on both the sender and the receiver, you could send the bytes of the struct as a base64-encoded string.

Pieter

PieterP:
For a "serious" project, I'd recommend using Protocol Buffers to serialize your structs instead of JSON. It'll be faster and more efficient.

If you know that the packing and Endianness of your struct will be the same on both the sender and the receiver, you could send the bytes of the struct as a base64-encoded string.

Pieter

Maybe i will just get it working first and then work up to that. im just starting to use the mesh network not sure what to expect. encoding the bytes and decoding them sounds a little over my head

There's nothing complicated about base64 encoding.

You could also send the data as a binary network packet directly, but not if you're using JSON.

PieterP:
There's nothing complicated about base64 encoding.

You could also send the data as a binary network packet directly, but not if you're using JSON.

Sorry i hit a snag in the road. i was going to attempt this then https://forum.arduino.cc/index.php?topic=705803.new#new happened

That's a separate problem. Fix one thing at a time, get the communication working before complicating things by adding a display.