Skip to content

Instantly share code, notes, and snippets.

@JayKickliter

JayKickliter/main.c

Created Feb 27, 2020
Embed
What would you like to do?
Serializing LoRa packets into JSON with parson
#include "parson.h"
#include <stdio.h>
/* EXPECTED OUTPUT */
/*
{
"rxpkt": [
{
"chan": 0,
"codr": "4\/5",
"data": "AB4AAAACAAAAAJsAAArKJQDqvgCLwY4=",
"datr": "SF10BW125",
"freq": 911.89999999999998,
"lsnr": 11.199999999999999,
"modu": "LORA",
"rfch": 0,
"rssi": -71,
"size": 23,
"stat": 1,
"time": "2020-02-27T18:16:22.142468Z",
"tmms": 1266862600142,
"tmst": 1384291068
},
{
"chan": 0,
"codr": "4\/5",
"data": "AB4AAAACAAAAAJsAAArKJQDqvgCLwY4=",
"datr": "SF10BW125",
"freq": 911.89999999999998,
"lsnr": 11.199999999999999,
"modu": "LORA",
"rfch": 0,
"rssi": -71,
"size": 23,
"stat": 1,
"time": "2020-02-27T18:16:22.142468Z",
"tmms": 1266862600142,
"tmst": 1384291068
}
]
}
*/
void
json_from_packet(JSON_Object * obj) {
json_object_set_number(obj, "chan", 0);
json_object_set_string(obj, "codr", "4/5");
json_object_set_string(obj, "data", "AB4AAAACAAAAAJsAAArKJQDqvgCLwY4=");
json_object_set_string(obj, "datr", "SF10BW125");
json_object_set_number(obj, "freq", 911.9);
json_object_set_number(obj, "lsnr", 11.2);
json_object_set_string(obj, "modu", "LORA");
json_object_set_number(obj, "rfch", 0);
json_object_set_number(obj, "rssi", -71);
json_object_set_number(obj, "size", 23);
json_object_set_number(obj, "stat", 1);
json_object_set_string(obj, "time", "2020-02-27T18:16:22.142468Z");
json_object_set_number(obj, "tmms", 1266862600142);
json_object_set_number(obj, "tmst", 1384291068);
}
int
main(int argc, char * argv[]) {
JSON_Value * root_val = json_value_init_object();
JSON_Object * root_obj = json_object(root_val);
JSON_Value * rx_pkts_val = json_value_init_array();
JSON_Array * rx_pkts_arr = json_array(rx_pkts_val);
json_object_set_value(root_obj, "rxpkt", rx_pkts_val);
for (int i = 0; i < 2; ++i) {
JSON_Value * pkt_val = json_value_init_object();
JSON_Object * pkt_obj = json_object(pkt_val);
json_from_packet(pkt_obj);
json_array_append_value(rx_pkts_arr, pkt_val);
}
char * packets_str = json_serialize_to_string_pretty(root_val);
puts(packets_str);
json_free_serialized_string(packets_str);
json_value_free(root_val);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.