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 item

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 is NULL.

Parameters
  • item[borrow]: A data item

  • buffer[out]: Buffer containing the result

  • buffer_size[out]: Size of the buffer

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 uint

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 negint

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 bytestring

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 string

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 array

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 map

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 tag

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer

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 ctrl

  • buffer: Buffer to serialize to

  • buffer_size: Size of the buffer