Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/src/zserio/serialization.py: 100%
27 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-10-29 13:10 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-10-29 13:10 +0000
1"""
2The module provides help methods for serialization and deserialization of generated objects.
3"""
5import typing
7from zserio.bitwriter import BitStreamWriter
8from zserio.bitbuffer import BitBuffer
9from zserio.bitreader import BitStreamReader
12def serialize(obj: typing.Any) -> BitBuffer:
13 """
14 Serializes generated object to the bit buffer.
16 Before serialization, the method calls initialize_offsets() on the given zserio object.
18 Because serialization to the bit buffer does not have to be byte aligned (divisible by 8), it's possible
19 that not all bits of the last byte are used. In this case, only most significant bits of the corresponded
20 size are used.
22 Example:
24 .. code:: python
26 import zserio
28 obj = SomeZserioObject()
29 bitbuffer = zserio.serialize(obj)
31 :param obj: Generated object to serialize.
32 :returns: Bit buffer which represents generated object in binary format.
33 :raises PythonRuntimeException: Throws in case of any error during serialization.
34 """
36 writer = _serialize(obj)
38 return BitBuffer(writer.byte_array, writer.bitposition)
41def deserialize(obj_class: typing.Type[typing.Any], bitbuffer: BitBuffer, *args) -> typing.Any:
42 """
43 Deserializes bit buffer to the generated object.
45 Example:
47 .. code:: python
49 import zserio
51 bitbuffer = zserio.BitBuffer(b'\\x00\\x01\\xBD\\x5A', 31)
52 obj = zserio.deserialize(SomeZserioObject, bitbuffer, 0xAB)
54 :param obj_class: Class instance of the generated object to deserialize.
55 :param bitbuffer: Bit buffer which represents generated object in binary format.
56 :param args: Additional arguments needed for obj_class.from_reader method.
57 :returns: Generated object created from given bit buffer.
58 :raises PythonRuntimeException: Throws in case of any error during deserialization.
59 """
61 reader = BitStreamReader.from_bitbuffer(bitbuffer)
63 return obj_class.from_reader(reader, *args)
66def serialize_to_bytes(obj: typing.Any) -> bytes:
67 """
68 Serializes generated object to the byte buffer.
70 Before serialization, the method calls initialize_offsets() on the given zserio object.
72 This is a convenient method for users which do not need exact number of bits to which the given object
73 will be serialized.
75 However, it's still possible that not all bits of the last byte are used. In this case, only most
76 significant bits of the corresponding size are used.
78 Example:
80 .. code:: python
82 import zserio
84 obj = SomeZserioObject()
85 buffer = zserio.serialize_to_bytes(obj)
87 :param obj: Generated object to serialize.
88 :returns: Bytes which represents generated object in binary format.
89 :raises PythonRuntimeException: Throws in case of any error during serialization.
90 """
92 writer = _serialize(obj)
94 return writer.byte_array
97def deserialize_from_bytes(obj_class: typing.Type[typing.Any], buffer: bytes, *args) -> typing.Any:
98 """
99 Deserializes byte buffer to the generated object.
101 This method can potentially use all bits of the last byte even if not all of them were written during
102 serialization (because there is no way how to specify exact number of bits). Thus, it could allow reading
103 behind stream (possibly in case of damaged data).
105 Example:
107 .. code:: python
109 import zserio
111 buffer = b'\\x00\\x01\\xBD\\x5A'
112 obj = zserio.deserialize_from_bytes(SomeZserioObject, buffer, 0xAB)
114 :param obj_class: Class instance of the generated object to deserialize.
115 :param buffer: Byte buffer which represents generated object in binary format.
116 :param args: Additional arguments needed for obj_class.from_reader method.
117 :returns: Generated object created from given byte buffer.
118 :raises PythonRuntimeException: Throws in case of any error during deserialization.
119 """
121 bitbuffer = BitBuffer(buffer)
123 return deserialize(obj_class, bitbuffer, *args)
126def serialize_to_file(obj: typing.Any, filename: str) -> None:
127 """
128 Serializes generated object to the file.
130 Before serialization, the method calls initialize_offsets() on the given zserio object.
132 This is a convenient method for users to easily write given generated object to file.
134 Example:
136 .. code:: python
138 import zserio
140 obj = SomeZserioObject()
141 buffer = zserio.serialize_to_file(obj, "file_name.bin")
143 :param obj: Generated object to serialize.
144 :param filename: File to write.
145 :raises PythonRuntimeException: Throws in case of any error during serialization.
146 """
148 writer = _serialize(obj)
149 writer.to_file(filename)
152def deserialize_from_file(obj_class: typing.Type[typing.Any], filename: str, *args) -> typing.Any:
153 """
154 Deserializes file to the generated object.
156 This is a convenient method for users to easily read given generated object from file.
158 Example:
160 .. code:: python
162 import zserio
164 obj = zserio.deserialize_from_file(SomeZserioObject, "file_name.bin", 0xAB)
166 :param obj_class: Class instance of the generated object to deserialize.
167 :param filename: File which represents generated object in binary format.
168 :param args: Additional arguments needed for obj_class.from_reader method.
169 :returns: Generated object created from given file contents.
170 :raises PythonRuntimeException: Throws in case of any error during deserialization.
171 """
173 reader = BitStreamReader.from_file(filename)
175 return obj_class.from_reader(reader, *args)
178def _serialize(obj: typing.Any) -> BitStreamWriter:
179 writer = BitStreamWriter()
180 obj.initialize_offsets(writer.bitposition)
181 obj.write(writer)
183 return writer