1from __future__
import annotations
4from typing
import TextIO, cast, Any, Union, Dict, List
6from ids_peak_common.serialization.iarchive
import IArchive
15 Serializer for writing `IArchive` objects to an output stream.
17 This class converts an `IArchive` object
18 into a specific serialization format
19 and writes the result to a stream.
23 from ids_peak_icv.serialization import Archive
24 from ids_peak_icv.serialization import Serializer, SerializationType
28 archive.set("name", "example")
30 serializer = Serializer(SerializationType.JSON)
31 with open("archive.json", "w") as f:
32 serializer.write(archive, f)
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 the serializer with the specified serialization type.
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.
49 @param serialization_type: The serialization format to use
50 (defaults to :class:`SerializationType.JSON`).
52 @since ids_peak_icv 1.0
56 def write(self, archive: IArchive, stream: TextIO) ->
None:
59 Serializes an archive and writes it to a stream.
61 Converts the given `IArchive` object
62 into the configured format
63 and writes the serialized data to the provided stream.
65 @param archive: The archive to serialize.
66 @param stream: The text stream that receives the serialized data
67 (e.g., file or StringIO).
69 @exception NotSupportedException: The serialization type is not supported.
71 @since ids_peak_icv 1.0
75 json.dump(data, cast(Any, stream), ensure_ascii=
False, indent=4)
82 Recursively converts an `IArchive` into a plain Python dict.
84 @param archive: The archive to convert.
86 @return A dictionary representation of the archive,
87 suitable for serialization.
89 @since ids_peak_icv 1.0
91 result: Dict[Any, Union[Dict[Any, Any], List[Dict[Any, Any]]]] = {}
93 if not isinstance(archive, Archive):
96 archive = cast(Archive, archive)
97 for key
in archive.keys():
98 value = archive.get(key,
None)
99 if isinstance(value, IArchive):
101 elif isinstance(value, list)
and value
and isinstance(value[0], IArchive):
102 result[key] = [self.
_to_dict(v)
for v
in value]
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Serializer for writing IArchive objects to an output stream.
None write(self, IArchive archive, TextIO stream)
Serializes an archive and writes it to a stream.
dict _to_dict(self, IArchive archive)
Recursively converts an IArchive into a plain Python dict.
None __init__(self, SerializationType serialization_type=SerializationType.JSON)
Initializes the serializer with the specified serialization type.