IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
serializer.py
Go to the documentation of this file.
1from __future__ import annotations
2
3import json
4from typing import TextIO, cast, Any, Union, Dict, List
5
7from ids_peak_icv.exceptions import NotSupportedException
8from ids_peak_icv.serialization.serialization_type import SerializationType
10
11
13 """!
14 @brief
15 Serializer for writing `IArchive` objects to an output stream.
16
17 This class converts an `IArchive` object
18 into a specific serialization format
19 and writes the result to a stream.
20
21 **Example**
22
23 from ids_peak_icv.serialization import Archive
24 from ids_peak_icv.serialization import Serializer, SerializationType
25
26 archive = Archive()
27 archive.set("id", 42)
28 archive.set("name", "example")
29
30 serializer = Serializer(SerializationType.JSON)
31 with open("archive.json", "w") as f:
32 serializer.write(archive, f)
33
34 @since ids_peak_icv 1.0
35
36 @ingroup ids_peak_icv_python_serialization
37 """
38
39 def __init__(self, serialization_type: SerializationType = SerializationType.JSON) -> None:
40 """!
41 @brief
42 Initializes the serializer with the specified serialization type.
43
44 The chosen format determines how the archive is written
45 when `write()` is called.
46 If no type is provided,
47 JSON is used by default.
48
49 @param serialization_type: The serialization format to use
50 (defaults to :class:`SerializationType.JSON`).
51
52 @since ids_peak_icv 1.0
53 """
54 self._type_type = serialization_type
55
56 def write(self, archive: IArchive, stream: TextIO) -> None:
57 """!
58 @brief
59 Serializes an archive and writes it to a stream.
60
61 Converts the given `IArchive` object
62 into the configured format
63 and writes the serialized data to the provided stream.
64
65 @param archive: The archive to serialize.
66 @param stream: The text stream that receives the serialized data
67 (e.g., file or StringIO).
68
69 @exception NotSupportedException: The serialization type is not supported.
70
71 @since ids_peak_icv 1.0
72 """
73 if self._type_type == SerializationType.JSON:
74 data = self._to_dict(archive)
75 json.dump(data, cast(Any, stream), ensure_ascii=False, indent=4)
76 else:
77 raise NotSupportedException(f"Serialization type {self._type} is not supported.")
78
79 def _to_dict(self, archive: IArchive) -> dict:
80 """!
81 @brief
82 Recursively converts an `IArchive` into a plain Python dict.
83
84 @param archive: The archive to convert.
85
86 @return A dictionary representation of the archive,
87 suitable for serialization.
88
89 @since ids_peak_icv 1.0
90 """
91 result: Dict[Any, Union[Dict[Any, Any], List[Dict[Any, Any]]]] = {}
92
93 if not isinstance(archive, Archive):
94 raise NotSupportedException("Serialization is only supported for the Archive implementation.")
95
96 archive = cast(Archive, archive)
97 for key in archive.keys():
98 value = archive.get(key, None)
99 if isinstance(value, IArchive):
100 result[key] = self._to_dict(value)
101 elif isinstance(value, list) and value and isinstance(value[0], IArchive):
102 result[key] = [self._to_dict(v) for v in value]
103 else:
104 result[key] = value
105 return result
Thrown when a requested operation is not implemented or supported.
Definition exceptions.py:71
Serializer for writing IArchive objects to an output stream.
Definition serializer.py:12
None write(self, IArchive archive, TextIO stream)
Serializes an archive and writes it to a stream.
Definition serializer.py:56
dict _to_dict(self, IArchive archive)
Recursively converts an IArchive into a plain Python dict.
Definition serializer.py:79
None __init__(self, SerializationType serialization_type=SerializationType.JSON)
Initializes the serializer with the specified serialization type.
Definition serializer.py:39