1from __future__
import annotations
3from typing
import TextIO, cast
15 Deserializer for reading serialized data
16 and converting it into an `IArchive` object.
18 This class provides functionality to read data
19 from a supported serialization format
20 (currently, only JSON)
21 and reconstruct it as an `IArchive` object.
25 from ids_peak_icv.serialization import Deserializer, SerializationType
27 deserializer = Deserializer(SerializationType.JSON)
28 with open("archive.json", "r") as f:
29 archive = deserializer.read(f)
31 id = archive.get("id", int)
32 name = archive.get("name", str)
34 @since ids_peak_icv 1.0
36 @ingroup ids_peak_icv_python_serialization
39 def __init__(self, serialization_type: SerializationType = SerializationType.JSON) ->
None:
42 Initializes a deserializer instance.
44 @param serialization_type: The format to use for deserialization.
45 Defaults to `SerializationType.JSON`.
47 @since ids_peak_icv 1.0
51 def read(self, stream: TextIO) -> IArchive:
54 Reads serialized data from the specified stream
55 and deserializes it into an archive.
57 @param stream: The stream to read serialized data from.
59 @return An `IArchive` object containing the deserialized data.
61 @exception NotSupportedException: The serialization type is not supported.
62 @exception IOException: The parsing failed.
63 @exception CorruptedException: The format of the given data is invalid.
65 @since ids_peak_icv 1.0
67 if self.
_type != SerializationType.JSON:
72 parsed = json.loads(data)
73 except json.JSONDecodeError
as e:
83 Recursively populates an Archive with parsed JSON data.
85 @param archive: The archive to populate.
86 @param data: Parsed JSON data (dict, list, scalar).
88 @exception CorruptedException: The format of the given data is invalid.
90 @since ids_peak_icv 1.0
92 if not isinstance(data, dict):
95 for key, value
in data.items():
96 if isinstance(value, dict):
99 archive.set(key, nested)
100 elif isinstance(value, list):
101 if value
and isinstance(value[0], dict):
106 nested_list.append(nested)
107 archive.set(key, cast(list[IArchive], nested_list))
109 archive.set(key, value)
111 archive.set(key, value)
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
Thrown when a file input/output operation fails.
Thrown when a requested operation is not implemented or supported.
A key-value store with type checking, based on a dict.
Deserializer for reading serialized data and converting it into an IArchive object.
None _populate_archive(self, Archive archive, list|dict data)
Recursively populates an Archive with parsed JSON data.
None __init__(self, SerializationType serialization_type=SerializationType.JSON)
Initializes a deserializer instance.
IArchive read(self, TextIO stream)
Reads serialized data from the specified stream and deserializes it into an archive.