Bugs in convert bson_t* to uint8_t* with libbson in C?

76 views
Skip to first unread message

Zhaoyu Yang

unread,
Aug 9, 2018, 11:18:22 AM8/9/18
to BSON
Hello,

I am trying to send bson data via socket connection. I found an API dealing with this situation at bson api site. The API is as following:
 uint8_t bson_destroy_with_steal (bson_t *bson, bool steal, uint32_t *length) 

Here is part of my program code:
// bson_t copy_b
// ... append something into copy_b;
       
if(&copy_b) {
           
const bson_t* pb = &copy_b;
           
char* json_result = bson_as_canonical_extended_json(pb,NULL);




           write_json
(outfname, json_result);
           bson_free
(json_result);


           uint8_t
*  buf = NULL;


           buf
=  bson_destroy_with_steal(pb,true,&pb->len);

           write_bson
(outfname,buf);
           free
(buf);
           buf
= NULL;


I expected to get the content in uint8_t characters by using this API. I only resulted in getting a weird symbol "|" in buf. But if I use bson_as_canonical_extended_json, the content is appropriately converted to json.
part of the json_result
...
[ "2260", "10:22:06.144732", " recvmsg", [ "16", [ "msg_namelen=0" ], "0" ] ], "13407" : [ "2260", "10:22:06.144877", " recvmsg", [ "16", [ "msg_namelen=0" ], "0" ] ], "13408" : [ "2260", "10:22:06.145019", " recvmsg", [ "16", [ "msg_namelen=0" ], "0" ] ], "13409" : [ "2260", "10:22:06.162379", " recvmsg", [ "16", [ "msg_namelen=0" ], "0" ] ]
...

The content of the buf:
"|"

Could anyone tell me is there anything wrong with my code or this API? Any help is appreciated.

Regards,
Zhaoyu

A. Jesse Jiryu Davis

unread,
Aug 10, 2018, 10:26:28 AM8/10/18
to BSON
BSON is a binary format that has some similar uses as JSON, but BSON is not JSON. You can convert from BSON to JSON with functions like bson_as_canonical_extended_json.

bson_destroy_with_steal does not convert from BSON to JSON. It gives you the binary contents of your BSON document. For more information on the BSON format, please see bsonspec.org
Reply all
Reply to author
Forward
0 new messages