IDS peak ICV v1.1.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 A serializer that converts IArchive objects into a textual
16 representation.
17
18 Currently, only JSON is supported.
19
20 Example::
21
22 from ids_peak_icv.serialization.archive import Archive
23 archive = Archive()
24 archive.set("name", "example")
25 serializer = Serializer()
26 with open("input.json", "w") as f:
27 serializer.write(archive, f)
28
29 @since ids_peak_icv 1.0
30 @ingroup ids_peak_icv_python_serialization
31 """
32
33 def __init__(self, serialization_type: SerializationType = SerializationType.JSON) -> None:
34 """!
35 @brief
36 Initializes a new serializer with the given serialization type.
37
38 @param serialization_type: The output serialization format.
39 Defaults to :class:`SerializationType.JSON`.
40
41 @since ids_peak_icv 1.0
42 """
43 self._type_type = serialization_type
44
45 # -------------------------------------------------------------------------
46
47 def write(self, archive: IArchive, stream: TextIO) -> None:
48 """!
49 @brief
50 Serializes an IArchive object and writes it to the given stream.
51
52 @param archive: The archive to serialize.
53 @param stream: A writable text stream (e.g., file or StringIO).
54 @exception NotSupportedException: If the configured serialization type is not supported.
55
56 Example::
57
58 archive = Archive()
59 archive.set("id", 42)
60 serializer = Serializer(SerializationType.JSON)
61 with open("output.json", "w") as f:
62 serializer.write(archive, f)
63
64 @since ids_peak_icv 1.0
65 """
66 if self._type_type == SerializationType.JSON:
67 data = self._to_dict(archive)
68 json.dump(data, cast(Any, stream), ensure_ascii=False, indent=4)
69 else:
70 raise NotSupportedException(f"Serialization type {self._type} is not supported.")
71
72 def _to_dict(self, archive: IArchive) -> dict:
73 """!
74 @brief
75 Recursively converts an IArchive into a plain Python dict.
76
77 @param archive: The archive to convert.
78 :returns: A dictionary representation of the archive, suitable for serialization.
79
80 @since ids_peak_icv 1.0
81 """
82 result: Dict[Any, Union[Dict[Any, Any], List[Dict[Any, Any]]]] = {}
83
84 if not isinstance(archive, Archive):
85 raise NotSupportedException("Serialization is only supported for the Archive implementation.")
86
87 archive = cast(Archive, archive)
88 for key in archive.keys():
89 value = archive.get(key, None)
90 if isinstance(value, IArchive):
91 result[key] = self._to_dict(value)
92 elif isinstance(value, list) and value and isinstance(value[0], IArchive):
93 result[key] = [self._to_dict(v) for v in value]
94 else:
95 result[key] = value
96 return result
Thrown when a requested operation is not implemented or supported.
Definition exceptions.py:71
A serializer that converts IArchive objects into a textual representation.
Definition serializer.py:12
None write(self, IArchive archive, TextIO stream)
Serializes an IArchive object and writes it to the given stream.
Definition serializer.py:47
dict _to_dict(self, IArchive archive)
Recursively converts an IArchive into a plain Python dict.
Definition serializer.py:72
None __init__(self, SerializationType serialization_type=SerializationType.JSON)
Initializes a new serializer with the given serialization type.
Definition serializer.py:33