-
-
Save PJK/6f695279452f0a20fce1 to your computer and use it in GitHub Desktop.
libcbor example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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