Coverage for /home/jenkins/workspace/NDS/Zserio/NDS_ZSERIO-linux-build/compiler/extensions/python/runtime/src/zserio/debugstring.py: 100%
23 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-12-13 15:12 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-12-13 15:12 +0000
1"""
2The module provides utilities for JSON debug string which can be obtained from zserio objects.
4.. note:: Zserio objects must be generated with `-withTypeInfoCode` zserio option to enable JSON debug string!
5"""
7import io
8import typing
10from zserio.walker import Walker, WalkFilter
11from zserio.json import JsonWriter, JsonReader
13def to_json_stream(obj: typing.Any, text_io: typing.TextIO, *, indent: typing.Union[None, int, str] = 4,
14 walk_filter: typing.Optional[WalkFilter] = None) -> None:
15 """
16 Writes contents of given zserio object to debug stream in JSON format using Walker with JsonWriter.
18 Example:
20 .. code:: python
22 import io
23 import zserio
25 obj = SomeZserioObject()
26 text_io = io.StringIO()
27 zserio.to_json_stream(obj, text_io)
29 The function allows usage of walk filters as well. The following example shows filtering of arrays
30 up to 5 elements:
32 .. code:: python
34 import io
35 import zserio
37 obj = SomeZserioObject()
38 text_io = io.StringIO()
39 walk_filter = zserio.ArrayLengthWalkFilter(5)
40 zserio.to_json_stream(obj, text_io, walk_filter=walk_filter)
42 :param obj: Zserio object to use.
43 :param text_io: Text stream to use.
44 :param indent: Indent argument for JsonWriter.
45 :param walk_filter: Walk filter to use by Walker.
46 """
48 json_writer = JsonWriter(text_io=text_io, indent=indent)
49 walker = Walker(json_writer, walk_filter)
50 walker.walk(obj)
52def to_json_string(obj: typing.Any, *, indent: typing.Union[None, int, str] = 4,
53 walk_filter: typing.Optional[WalkFilter] = None) -> str:
54 """
55 Gets debug string in JSON format using Walker with JsonWriter for given zserio object.
57 Example:
59 .. code:: python
61 import zserio
63 obj = SomeZserioObject()
64 zserio.to_json_string(obj)
66 The function allows usage of walk filters as well. The following example shows filtering of arrays
67 up to 5 elements:
69 .. code:: python
71 import zserio
73 obj = SomeZserioObject()
74 walk_filter = zserio.ArrayLengthWalkFilter(5)
75 zserio.to_json_string(obj, walk_filter=walk_filter)
77 :param obj: Zserio object to use.
78 :param indent: Indent argument for JsonWriter.
79 :param walk_filter: Walk filter to use by Walker.
81 :returns: JSON debug string.
82 """
84 text_io = io.StringIO()
85 to_json_stream(obj, text_io, indent=indent, walk_filter=walk_filter)
86 return text_io.getvalue()
88def to_json_file(obj: typing.Any, filename: str, *, indent: typing.Union[None, int, str] = 4,
89 walk_filter: typing.Optional[WalkFilter] = None) -> None:
90 """
91 Writes contents of given zserio object to debug file in JSON format using Walker with JsonWriter.
93 Example:
95 .. code:: python
97 import zserio
99 obj = SomeZserioObject()
100 zserio.to_json_file(obj, "file_name.json")
102 The function allows usage of walk filters as well. The following example shows filtering of arrays
103 up to 5 elements:
105 .. code:: python
107 import zserio
109 obj = SomeZserioObject()
110 walk_filter = zserio.ArrayLengthWalkFilter(5)
111 zserio.to_json_file(obj, "file_name.json", walk_filter=walk_filter)
113 :param obj: Zserio object to use.
114 :param filename: Name of file to write.
115 :param indent: Indent argument for JsonWriter.
116 :param walk_filter: Walk filter to use by Walker.
117 """
119 with open(filename, "w", encoding="utf-8") as text_io:
120 to_json_stream(obj, text_io, indent=indent, walk_filter=walk_filter)
122def from_json_stream(obj_class: typing.Type[typing.Any], text_io: typing.TextIO,
123 *arguments: typing.List[typing.Any]) -> typing.Any:
124 """
125 Parses JSON debug string from given text stream and creates instance of the requested zserio object
126 according to the data contained in the debug string.
128 .. note:: The created object can be only partially initialized depending on the data stored in the
129 JSON debug string.
131 .. code:: python
133 import io
134 import zserio
136 text_io = io.StringIO("{\\\"field1\\\": 13}")
137 obj = zserio.from_json_stream(SomeZserioObject, text_io)
139 :param obj_class: Class instance of the generated object to create.
140 :param text_io: Text stream to use.
141 :param arguments: Arguments of the generated zserio object.
142 :returns: Instance of the requested zserio object.
143 :raises PythonRuntimeException: In case of any error.
144 """
146 json_reader = JsonReader(text_io)
147 return json_reader.read(obj_class.type_info(), *arguments)
149def from_json_string(obj_class: typing.Type[typing.Any], json_string: str,
150 *arguments: typing.List[typing.Any]) -> typing.Any:
151 """
152 Parses JSON debug string and creates instance of the requested zserio object
153 according to the data contained in the debug string.
155 .. note:: The created object can be only partially initialized depending on the data stored in the
156 JSON debug string.
158 .. code:: python
160 import zserio
162 json_string = "{\\\"field1\\\": 13}"
163 obj = zserio.from_json_string(SomeZserioObject, json_string)
165 :param obj_class: Class instance of the generated object to create.
166 :param json_string: JSON debug string to parse.
167 :param arguments: Arguments of the generated zserio object.
168 :returns: Instance of the requested zserio object.
169 :raises PythonRuntimeException: In case of any error.
170 """
172 return from_json_stream(obj_class, io.StringIO(json_string), *arguments)
174def from_json_file(obj_class: typing.Type[typing.Any], filename: str,
175 *arguments: typing.List[typing.Any]) -> typing.Any:
176 """
177 Parses JSON debug string from given file and creates instance of the requested zserio object
178 according to the data contained in the debug string.
180 .. note:: The created object can be only partially initialized depending on the data stored in the
181 JSON debug string.
183 .. code:: python
185 import zserio
187 obj = zserio.from_json_file(SomeZserioObject, "file_name.json")
189 :param obj_class: Class instance of the generated object to create.
190 :param filename: File name to read.
191 :param arguments: Arguments of the generated zserio object.
192 :returns: Instance of the requested zserio object.
193 :raises PythonRuntimeException: In case of any error.
194 """
196 with open(filename, "r", encoding="utf-8") as text_io:
197 return from_json_stream(obj_class, text_io, *arguments)