Encoding¶
The easiest way to encode data items is using the cbor_serialize()
or cbor_serialize_alloc()
functions:
-
size_t
cbor_serialize
(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶ Serialize the given item.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A data itembuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_alloc
(const cbor_item_t *item, cbor_mutable_data *buffer, size_t *buffer_size)¶ Serialize the given item, allocating buffers as needed.
Warning
It is your responsibility to free the buffer using an appropriate
free
implementation.- Return
- Length of the result. 0 on failure, in which case
buffer
isNULL
. - Parameters
item[borrow]
: A data itembuffer[out]
: Buffer containing the resultbuffer_size[out]
: Size of thebuffer
Type-specific serializers¶
In case you know the type of the item you want to serialize beforehand, you can use one of the type-specific serializers.
Note
Unless compiled in debug mode, these do not verify the type. Passing an incorrect item will result in an undefined behavior.
-
size_t
cbor_serialize_uint
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize an uint.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A uintbuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_negint
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize a negint.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A negingbuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_bytestring
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize a bytestring.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A bytestringbuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_string
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize a string.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A stringbuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_array
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize an array.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: An arraybuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_map
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize a map.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A mapbuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_tag
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize a tag.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A tagbuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer
-
size_t
cbor_serialize_float_ctrl
(const cbor_item_t *, cbor_mutable_data, size_t)¶ Serialize a.
- Return
- Length of the result. 0 on failure.
- Parameters
item[borrow]
: A float or ctrlbuffer
: Buffer to serialize tobuffer_size
: Size of thebuffer