1from __future__
import annotations
3from typing
import TextIO, cast
15 A deserializer that reads serialized data and reconstructs
18 Currently, only JSON is supported.
22 deserializer = Deserializer()
23 with open("input.json", "r") as f:
24 archive = deserializer.read(f)
25 archive.get("id", int) # 42
26 archive.get("name", str) # 'example'
28 @since ids_peak_icv 1.0
29 @ingroup ids_peak_icv_python_serialization
32 def __init__(self, serialization_type: SerializationType = SerializationType.JSON) ->
None:
35 Initializes a deserializer instance.
37 @param serialization_type: The format to use for deserialization.
38 Defaults to :class:`SerializationType.JSON`.
40 @since ids_peak_icv 1.0
46 def read(self, stream: TextIO) -> IArchive:
49 Reads serialized data from the given text stream and returns
50 a reconstructed IArchive.
52 @param stream: The input stream to read from (e.g., file).
53 @return A new :class:`Archive` containing the deserialized data.
54 @exception NotSupportedException: If the serialization format is unsupported
55 or the JSON is invalid.
56 @exception IOException: If the parsing fails.
58 @since ids_peak_icv 1.0
60 if self.
_type != SerializationType.JSON:
65 parsed = json.loads(data)
66 except json.JSONDecodeError
as e:
78 Recursively populates an Archive with parsed JSON data.
80 @param archive: The archive to populate.
81 @param data: Parsed JSON data (dict, list, scalar).
82 @exception CorruptedException: If the format of the given data is invalid.
84 if not isinstance(data, dict):
87 for key, value
in data.items():
88 if isinstance(value, dict):
91 archive.set(key, nested)
92 elif isinstance(value, list):
93 if value
and isinstance(value[0], dict):
98 nested_list.append(nested)
99 archive.set(key, cast(list[IArchive], nested_list))
101 archive.set(key, value)
103 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 simple key-value store with type checking, similar to a dict.
A deserializer that reads serialized data and reconstructs IArchive objects.
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 given text stream and returns a reconstructed IArchive.