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.
- param item[borrow]:
A data item
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
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.- param item[borrow]:
A data item
- param buffer[out]:
Buffer containing the result
- param buffer_size[out]:
Size of the
buffer
- return:
Length of the result. 0 on failure, in which case
buffer
isNULL
.
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 *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize an uint.
- param item[borrow]:
A uint
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
size_t cbor_serialize_negint(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize a negint.
- param item[borrow]:
A neging
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
size_t cbor_serialize_bytestring(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize a bytestring.
- param item[borrow]:
A bytestring
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
size_t cbor_serialize_string(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize a string.
- param item[borrow]:
A string
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
size_t cbor_serialize_array(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize an array.
- param item[borrow]:
An array
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
size_t cbor_serialize_map(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize a map.
- param item[borrow]:
A map
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
size_t cbor_serialize_tag(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize a tag.
- param item[borrow]:
A tag
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.
-
size_t cbor_serialize_float_ctrl(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)¶
Serialize a.
- param item[borrow]:
A float or ctrl
- param buffer:
Buffer to serialize to
- param buffer_size:
Size of the
buffer
- return:
Length of the result. 0 on failure.