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

1""" 

2The module provides utilities for JSON debug string which can be obtained from zserio objects. 

3 

4.. note:: Zserio objects must be generated with `-withTypeInfoCode` zserio option to enable JSON debug string! 

5""" 

6 

7import io 

8import typing 

9 

10from zserio.walker import Walker, WalkFilter 

11from zserio.json import JsonWriter, JsonReader 

12 

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. 

17 

18 Example: 

19 

20 .. code:: python 

21 

22 import io 

23 import zserio 

24 

25 obj = SomeZserioObject() 

26 text_io = io.StringIO() 

27 zserio.to_json_stream(obj, text_io) 

28 

29 The function allows usage of walk filters as well. The following example shows filtering of arrays 

30 up to 5 elements: 

31 

32 .. code:: python 

33 

34 import io 

35 import zserio 

36 

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) 

41 

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 """ 

47 

48 json_writer = JsonWriter(text_io=text_io, indent=indent) 

49 walker = Walker(json_writer, walk_filter) 

50 walker.walk(obj) 

51 

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. 

56 

57 Example: 

58 

59 .. code:: python 

60 

61 import zserio 

62 

63 obj = SomeZserioObject() 

64 zserio.to_json_string(obj) 

65 

66 The function allows usage of walk filters as well. The following example shows filtering of arrays 

67 up to 5 elements: 

68 

69 .. code:: python 

70 

71 import zserio 

72 

73 obj = SomeZserioObject() 

74 walk_filter = zserio.ArrayLengthWalkFilter(5) 

75 zserio.to_json_string(obj, walk_filter=walk_filter) 

76 

77 :param obj: Zserio object to use. 

78 :param indent: Indent argument for JsonWriter. 

79 :param walk_filter: Walk filter to use by Walker. 

80 

81 :returns: JSON debug string. 

82 """ 

83 

84 text_io = io.StringIO() 

85 to_json_stream(obj, text_io, indent=indent, walk_filter=walk_filter) 

86 return text_io.getvalue() 

87 

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. 

92 

93 Example: 

94 

95 .. code:: python 

96 

97 import zserio 

98 

99 obj = SomeZserioObject() 

100 zserio.to_json_file(obj, "file_name.json") 

101 

102 The function allows usage of walk filters as well. The following example shows filtering of arrays 

103 up to 5 elements: 

104 

105 .. code:: python 

106 

107 import zserio 

108 

109 obj = SomeZserioObject() 

110 walk_filter = zserio.ArrayLengthWalkFilter(5) 

111 zserio.to_json_file(obj, "file_name.json", walk_filter=walk_filter) 

112 

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 """ 

118 

119 with open(filename, "w", encoding="utf-8") as text_io: 

120 to_json_stream(obj, text_io, indent=indent, walk_filter=walk_filter) 

121 

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. 

127 

128 .. note:: The created object can be only partially initialized depending on the data stored in the 

129 JSON debug string. 

130 

131 .. code:: python 

132 

133 import io 

134 import zserio 

135 

136 text_io = io.StringIO("{\\\"field1\\\": 13}") 

137 obj = zserio.from_json_stream(SomeZserioObject, text_io) 

138 

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 """ 

145 

146 json_reader = JsonReader(text_io) 

147 return json_reader.read(obj_class.type_info(), *arguments) 

148 

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. 

154 

155 .. note:: The created object can be only partially initialized depending on the data stored in the 

156 JSON debug string. 

157 

158 .. code:: python 

159 

160 import zserio 

161 

162 json_string = "{\\\"field1\\\": 13}" 

163 obj = zserio.from_json_string(SomeZserioObject, json_string) 

164 

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 """ 

171 

172 return from_json_stream(obj_class, io.StringIO(json_string), *arguments) 

173 

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. 

179 

180 .. note:: The created object can be only partially initialized depending on the data stored in the 

181 JSON debug string. 

182 

183 .. code:: python 

184 

185 import zserio 

186 

187 obj = zserio.from_json_file(SomeZserioObject, "file_name.json") 

188 

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 """ 

195 

196 with open(filename, "r", encoding="utf-8") as text_io: 

197 return from_json_stream(obj_class, text_io, *arguments)