CborWriter
- class cometa.cbor.cbor_writer.CborWriter(ptr=None)[source]
Bases:
objectA simple writer for Concise Binary Object Representation (CBOR) encoded data.
This class facilitates encoding data into the CBOR format. It abstracts the complexities involved in CBOR encoding, providing a simple interface for creating CBOR data streams.
- __init__(ptr=None)[source]
Creates and initializes a new instance of a CBOR writer.
- Return type:
None
- property refcount: int
Returns the number of active references to the underlying C object.
- property encoded_size: int
Returns the current size of the encoded data in bytes.
- property last_error: str
Returns the last error message recorded for this writer.
- reset()[source]
Resets the writer, clearing all written data. The writer can be reused for new data without creating a new instance.
- Return type:
None
- write_int(value)[source]
Encodes and writes an integer value.
This method automatically selects the appropriate CBOR encoding: - Major type 0 for positive integers fitting in uint64. - Major type 1 for negative integers fitting in int64. - Major type 6 (Tag 2/3) for arbitrary precision integers (BigInts).
- Parameters:
value (int | BigInt) – The integer to write.
- Return type:
None
- write_bytes(data)[source]
Writes a byte string (major type 2).
- Parameters:
data (bytes) – The raw bytes to encode.
- Return type:
None
- write_str(text)[source]
Writes a UTF-8 text string (major type 3).
- Parameters:
text (str) – The string to encode.
- Return type:
None
- write_start_array(length=None)[source]
Initiates the writing of an array.
- Parameters:
length (int, optional) – The number of elements. If None, starts an indefinite-length array.
- Return type:
None
- write_start_map(length=None)[source]
Initiates the writing of a map.
- Parameters:
length (int, optional) – The number of key-value pairs. If None, starts an indefinite-length map.
- Return type:
None
- write_tag(tag)[source]
Assigns a semantic tag (major type 6) to the next data item.
- Parameters:
tag (int | CborTag) – The tag value.
- Return type:
None
- write_encoded(data)[source]
Writes pre-encoded CBOR data directly to the stream.
This is useful if you already have a valid CBOR byte fragment and want to embed it without re-encoding.
- Parameters:
data (bytes) – The pre-encoded CBOR bytes.
- Return type:
None