Skip to content

Instantly share code, notes, and snippets.

@PJK
Last active August 29, 2015 14:21
Show Gist options
  • Save PJK/6f695279452f0a20fce1 to your computer and use it in GitHub Desktop.
Save PJK/6f695279452f0a20fce1 to your computer and use it in GitHub Desktop.
libcbor example
#include "cbor.h"
int main(int argc, char * argv[])
{
/* Preallocate the map structure */
cbor_item_t * root = cbor_new_definite_map(2);
/* Add the content */
cbor_map_add(root, (struct cbor_pair) {
.key = cbor_move(cbor_build_string("Is CBOR awesome?")),
.value = cbor_move(cbor_build_bool(true))
});
cbor_map_add(root, (struct cbor_pair) {
.key = cbor_move(cbor_build_uint8(42)),
.value = cbor_move(cbor_build_string("Is the answer"))
});
/* Output: `length` bytes of data in the `buffer` */
unsigned char * buffer = malloc(1024);
size_t length = cbor_serialize(root, buffer, 1024);
fwrite(buffer, 1, length, stdout);
fflush(stdout);
cbor_decref(&root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment