Skip to content

Commit 8667b5c

Browse files
committed
serialize the json to bytes
Signed-off-by: faradaym <rcheyenne.truss@gmail.com>
1 parent a572798 commit 8667b5c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/DuckNet.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "include/DuckNet.h"
22
#include <cstring>
3+
#include <ArduinoJson.h>
34

45
DuckNet::DuckNet(BloomFilter *filter): bloomFilter(filter) {
56
}
@@ -407,7 +408,28 @@ uint8_t* DuckNet::serializeAtakHistoryToBytes(CircularBuffer* buffer, size_t* to
407408
while(tail != buffer->getHead()){
408409
CdpPacket packet = buffer->getMessage(tail);
409410
packetArr.insert(packetArr.end(), packet.muid.begin(), packet.muid.end());
410-
packetArr.insert(packetArr.end(), packet.data.begin(), packet.data.end());
411+
// packetArr.insert(packetArr.end(), packet.data.begin(), packet.data.end());
412+
413+
// Decode JSON from packet.data
414+
DynamicJsonDocument doc(256);
415+
DeserializationError err = deserializeJson(doc, packet.data.data(), packet.data.size());
416+
if (err) {
417+
logerr_ln("JSON parse error in packet.data");
418+
} else {
419+
// Extract fields
420+
const char* body = doc["body"] | "";
421+
const char* username = doc["username"] | "";
422+
423+
// Manually encode: [len][data]
424+
uint8_t bodyLen = strlen(body);
425+
packetArr.push_back(bodyLen);
426+
packetArr.insert(packetArr.end(), body, body + bodyLen);
427+
428+
uint8_t usernameLen = strlen(username);
429+
packetArr.push_back(usernameLen);
430+
packetArr.insert(packetArr.end(), username, username + usernameLen);
431+
}
432+
411433
tail++;
412434
if(tail == buffer->getBufferEnd()){
413435
tail = 0;

0 commit comments

Comments
 (0)