Path: blob/main/contrib/libcbor/examples/create_items.c
39586 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#include <stdio.h>8#include "cbor.h"910int main(void) {11/* Preallocate the map structure */12cbor_item_t* root = cbor_new_definite_map(2);13/* Add the content */14bool success = cbor_map_add(15root, (struct cbor_pair){16.key = cbor_move(cbor_build_string("Is CBOR awesome?")),17.value = cbor_move(cbor_build_bool(true))});18success &= cbor_map_add(19root, (struct cbor_pair){20.key = cbor_move(cbor_build_uint8(42)),21.value = cbor_move(cbor_build_string("Is the answer"))});22if (!success) return 1;23/* Output: `length` bytes of data in the `buffer` */24unsigned char* buffer;25size_t buffer_size;26cbor_serialize_alloc(root, &buffer, &buffer_size);2728fwrite(buffer, 1, buffer_size, stdout);29free(buffer);3031fflush(stdout);32cbor_decref(&root);33}343536