zserio.serialization module¶
The module provides help methods for serialization and deserialization of generated objects.
- zserio.serialization.deserialize(obj_class: Type[Any], bitbuffer: BitBuffer, *args) Any [source]¶
Deserializes bit buffer to the generated object.
Example:
import zserio bitbuffer = zserio.BitBuffer(b'\x00\x01\xBD\x5A', 31) obj = zserio.deserialize(SomeZserioObject, bitbuffer, 0xAB)
- Parameters:
obj_class – Class instance of the generated object to deserialize.
bitbuffer – Bit buffer which represents generated object in binary format.
args – Additional arguments needed for obj_class.from_reader method.
- Returns:
Generated object created from given bit buffer.
- Raises:
PythonRuntimeException – Throws in case of any error during deserialization.
- zserio.serialization.deserialize_from_bytes(obj_class: Type[Any], buffer: bytes, *args) Any [source]¶
Deserializes byte buffer to the generated object.
This method can potentially use all bits of the last byte even if not all of them were written during serialization (because there is no way how to specify exact number of bits). Thus, it could allow reading behind stream (possibly in case of damaged data).
Example:
import zserio buffer = b'\x00\x01\xBD\x5A' obj = zserio.deserialize_from_bytes(SomeZserioObject, buffer, 0xAB)
- Parameters:
obj_class – Class instance of the generated object to deserialize.
buffer – Byte buffer which represents generated object in binary format.
args – Additional arguments needed for obj_class.from_reader method.
- Returns:
Generated object created from given byte buffer.
- Raises:
PythonRuntimeException – Throws in case of any error during deserialization.
- zserio.serialization.deserialize_from_file(obj_class: Type[Any], filename: str, *args) Any [source]¶
Deserializes file to the generated object.
This is a convenient method for users to easily read given generated object from file.
Example:
import zserio obj = zserio.deserialize_from_file(SomeZserioObject, "file_name.bin", 0xAB)
- Parameters:
obj_class – Class instance of the generated object to deserialize.
filename – File which represents generated object in binary format.
args – Additional arguments needed for obj_class.from_reader method.
- Returns:
Generated object created from given file contents.
- Raises:
PythonRuntimeException – Throws in case of any error during deserialization.
- zserio.serialization.serialize(obj: Any) BitBuffer [source]¶
Serializes generated object to the bit buffer.
Before serialization, the method calls initialize_offsets() on the given zserio object.
Because serialization to the bit buffer does not have to be byte aligned (divisible by 8), it’s possible that not all bits of the last byte are used. In this case, only most significant bits of the corresponded size are used.
Example:
import zserio obj = SomeZserioObject() bitbuffer = zserio.serialize(obj)
- Parameters:
obj – Generated object to serialize.
- Returns:
Bit buffer which represents generated object in binary format.
- Raises:
PythonRuntimeException – Throws in case of any error during serialization.
- zserio.serialization.serialize_to_bytes(obj: Any) bytes [source]¶
Serializes generated object to the byte buffer.
Before serialization, the method calls initialize_offsets() on the given zserio object.
This is a convenient method for users which do not need exact number of bits to which the given object will be serialized.
However, it’s still possible that not all bits of the last byte are used. In this case, only most significant bits of the corresponding size are used.
Example:
import zserio obj = SomeZserioObject() buffer = zserio.serialize_to_bytes(obj)
- Parameters:
obj – Generated object to serialize.
- Returns:
Bytes which represents generated object in binary format.
- Raises:
PythonRuntimeException – Throws in case of any error during serialization.
- zserio.serialization.serialize_to_file(obj: Any, filename: str) None [source]¶
Serializes generated object to the file.
Before serialization, the method calls initialize_offsets() on the given zserio object.
This is a convenient method for users to easily write given generated object to file.
Example:
import zserio obj = SomeZserioObject() buffer = zserio.serialize_to_file(obj, "file_name.bin")
- Parameters:
obj – Generated object to serialize.
filename – File to write.
- Raises:
PythonRuntimeException – Throws in case of any error during serialization.