Reputation: 203
I am new to JSON and jansson . I am trying to create a message in JSON using jansson library and send using UDP. Which requires in byte array. Buffer and length of message in bytes. I tried with json_object_size(). But it returns only number of elements in object.Please suggest me a way forward.
json_t *messagebody = json_object();
json_object_set_new(messagebody, "request_id", request_id);
json_object_set_new(messagebody, "process_id", json_string(process_id));
json_object_set_new(messagebody, "process_server_id", json_string(process_server_id));
json_object_set_new(messagebody, "ip_address", json_string(my_ip_address));
json_object_set_new(messagebody, "action", action);
Upvotes: 1
Views: 1646
Reputation: 969
It seems you just call char *json_dumps(const json_t *json, size_t flags)
.
That will give you a char * to a null terminated string representing the encoded json data. You have to free it after you are finished with it. To get the length in bytes you should simply be able to use strlen() on the result.
The flags are explained in API reference under 'encoding'.
Upvotes: 2