freebsd-dev/contrib/libcbor/examples/create_items.c
Ed Maste 5d3e7166f6 libcbor: update to 0.10.2
Sponsored by:	The FreeBSD Foundation
2023-04-21 16:10:03 -04:00

35 lines
1.0 KiB
C

/*
* Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
*
* libcbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdio.h>
#include "cbor.h"
int main(void) {
/* Preallocate the map structure */
cbor_item_t* root = cbor_new_definite_map(2);
/* Add the content */
bool success = cbor_map_add(
root, (struct cbor_pair){
.key = cbor_move(cbor_build_string("Is CBOR awesome?")),
.value = cbor_move(cbor_build_bool(true))});
success &= cbor_map_add(
root, (struct cbor_pair){
.key = cbor_move(cbor_build_uint8(42)),
.value = cbor_move(cbor_build_string("Is the answer"))});
if (!success) return 1;
/* Output: `length` bytes of data in the `buffer` */
unsigned char* buffer;
size_t buffer_size;
cbor_serialize_alloc(root, &buffer, &buffer_size);
fwrite(buffer, 1, buffer_size, stdout);
free(buffer);
fflush(stdout);
cbor_decref(&root);
}