1from __future__
import annotations
4from typing
import TextIO, cast, Any, Union, Dict, List
15 A serializer that converts IArchive objects into a textual
18 Currently, only JSON is supported.
22 from ids_peak_icv.serialization.archive import Archive
24 archive.set("name", "example")
25 serializer = Serializer()
26 with open("input.json", "w") as f:
27 serializer.write(archive, f)
29 @since ids_peak_icv 1.0
30 @ingroup ids_peak_icv_python_serialization
33 def __init__(self, serialization_type: SerializationType = SerializationType.JSON) ->
None:
36 Initializes a new serializer with the given serialization type.
38 @param serialization_type: The output serialization format.
39 Defaults to :class:`SerializationType.JSON`.
41 @since ids_peak_icv 1.0
47 def write(self, archive: IArchive, stream: TextIO) ->
None:
50 Serializes an IArchive object and writes it to the given stream.
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.
60 serializer = Serializer(SerializationType.JSON)
61 with open("output.json", "w") as f:
62 serializer.write(archive, f)
64 @since ids_peak_icv 1.0
68 json.dump(data, cast(Any, stream), ensure_ascii=
False, indent=4)
75 Recursively converts an IArchive into a plain Python dict.
77 @param archive: The archive to convert.
78 :returns: A dictionary representation of the archive, suitable for serialization.
80 @since ids_peak_icv 1.0
82 result: Dict[Any, Union[Dict[Any, Any], List[Dict[Any, Any]]]] = {}
84 if not isinstance(archive, Archive):
87 archive = cast(Archive, archive)
88 for key
in archive.keys():
89 value = archive.get(key,
None)
90 if isinstance(value, IArchive):
92 elif isinstance(value, list)
and value
and isinstance(value[0], IArchive):
93 result[key] = [self.
_to_dict(v)
for v
in value]
Thrown when a requested operation is not implemented or supported.
A serializer that converts IArchive objects into a textual representation.
None write(self, IArchive archive, TextIO stream)
Serializes an IArchive object and writes it to the given 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 a new serializer with the given serialization type.