src/zserio/DebugStringUtil.h
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * \file |
3 | | * It provides utilities for JSON debug string which can be obtained from zserio objects. |
4 | | * |
5 | | * These utilities are not used by generated code and they are provided only for user convenience. |
6 | | * |
7 | | * \note Please note that Zserio objects must be generated with `-withTypeInfoCode` and `-withReflectionCode` |
8 | | * zserio options to enable JSON debug string! |
9 | | * |
10 | | * \note Please note that file operations allocate memory as needed and are not designed to use allocators. |
11 | | */ |
12 | | |
13 | | #ifndef ZSERIO_DEBUG_STRING_UTIL_H_INC |
14 | | #define ZSERIO_DEBUG_STRING_UTIL_H_INC |
15 | | |
16 | | #include <fstream> |
17 | | #include <sstream> |
18 | | #include <utility> |
19 | | |
20 | | #include "zserio/JsonReader.h" |
21 | | #include "zserio/JsonWriter.h" |
22 | | #include "zserio/ReflectableUtil.h" |
23 | | #include "zserio/Traits.h" |
24 | | #include "zserio/Types.h" |
25 | | #include "zserio/Walker.h" |
26 | | |
27 | | namespace zserio |
28 | | { |
29 | | |
30 | | namespace detail |
31 | | { |
32 | | |
33 | | // Implementations needs to be in detail because old MSVC compiler 2015 has problems with calling overload. |
34 | | |
35 | | template <typename T, typename WALK_FILTER, typename ALLOC> |
36 | | void toJsonStream( |
37 | | const T& object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator) |
38 | 36 | { |
39 | 36 | static_assert(has_reflectable<T>::value, |
40 | 36 | "DebugStringUtil.toJsonStream: " |
41 | 36 | "Zserio object must have reflections enabled (see zserio option -withReflectionCode)!"); |
42 | | |
43 | 36 | BasicJsonWriter<ALLOC> jsonWriter(stream, indent); |
44 | 36 | BasicWalker<ALLOC> walker(jsonWriter, walkFilter); |
45 | 36 | walker.walk(object.reflectable(allocator)); |
46 | 36 | } |
47 | | |
48 | | template <typename T, typename WALK_FILTER, typename ALLOC> |
49 | | string<ALLOC> toJsonString(const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator) |
50 | 12 | { |
51 | 12 | auto stream = std::basic_ostringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>>( |
52 | 12 | string<ALLOC>(allocator)); |
53 | 12 | detail::toJsonStream(object, stream, indent, walkFilter, allocator); |
54 | 12 | return stream.str(); |
55 | 12 | } |
56 | | |
57 | | template <typename T, typename WALK_FILTER, typename ALLOC> |
58 | | void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter, |
59 | | const ALLOC& allocator) |
60 | 13 | { |
61 | 13 | std::ofstream stream = std::ofstream(fileName.c_str(), std::ios::out | std::ios::trunc); |
62 | 13 | if (!stream) |
63 | 1 | { |
64 | 1 | throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to open '") << fileName << "'!"; |
65 | 1 | } |
66 | | |
67 | 12 | detail::toJsonStream(object, stream, indent, walkFilter, allocator); |
68 | | |
69 | 12 | if (!stream) |
70 | 0 | { |
71 | 0 | throw CppRuntimeException("DebugStringUtil.toJsonFile: Failed to write '") << fileName << "'!"; |
72 | 0 | } |
73 | 12 | } |
74 | | |
75 | | // needed due to GCC compilation problems, GCC tries to instantiate return type even though the |
76 | | // particular function template has different number of arguments, this prevents the return type instantiation |
77 | | // in case that the ALLOC is not an allocator |
78 | | template <typename ALLOC, typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0> |
79 | | struct DebugStringTraits |
80 | | { |
81 | | using ReflectablePtr = IBasicReflectablePtr<ALLOC>; |
82 | | }; |
83 | | |
84 | | } // namespace detail |
85 | | |
86 | | /** |
87 | | * Writes contents of given zserio object to debug stream in JSON format using Walker with JsonWriter. |
88 | | * |
89 | | * Example: |
90 | | * \code{.cpp} |
91 | | * #include <sstream> |
92 | | * #include <zserio/DebugStringUtil.h> |
93 | | * |
94 | | * SomeZserioObject object; |
95 | | * std::ostringstream stream; |
96 | | * zserio::toJsonStream(object, stream); |
97 | | * \endcode |
98 | | * |
99 | | * \param object Zserio object to use. |
100 | | * \param stream Output stream to use. |
101 | | * \param allocator Allocator to use. |
102 | | */ |
103 | | template <typename T, typename ALLOC = typename T::allocator_type, |
104 | | typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0> |
105 | | void toJsonStream(const T& object, std::ostream& stream, const ALLOC& allocator = ALLOC()) |
106 | 3 | { |
107 | 3 | detail::toJsonStream(object, stream, 4, BasicDefaultWalkFilter<ALLOC>(), allocator); |
108 | 3 | } |
109 | | |
110 | | /** |
111 | | * Writes contents of given zserio object to debug stream in JSON format using Walker with JsonWriter. |
112 | | * |
113 | | * This function allows setting of indentation of JSON output. |
114 | | * |
115 | | * Example: |
116 | | * \code{.cpp} |
117 | | * #include <sstream> |
118 | | * #include <zserio/DebugStringUtil.h> |
119 | | * |
120 | | * SomeZserioObject object; |
121 | | * std::ostringstream stream; |
122 | | * const uint8_t indent = 4; |
123 | | * zserio::toJsonStream(object, stream, indent); |
124 | | * \endcode |
125 | | * |
126 | | * \param object Zserio object to use. |
127 | | * \param stream Output stream to use. |
128 | | * \param indent Indent argument for JsonWriter. |
129 | | * \param allocator Allocator to use. |
130 | | */ |
131 | | template <typename T, typename ALLOC = typename T::allocator_type, |
132 | | typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0> |
133 | | void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, const ALLOC& allocator = ALLOC()) |
134 | 3 | { |
135 | 3 | detail::toJsonStream(object, stream, indent, BasicDefaultWalkFilter<ALLOC>(), allocator); |
136 | 3 | } |
137 | | |
138 | | /** |
139 | | * Writes contents of given zserio object to debug stream in JSON format using Walker with JsonWriter. |
140 | | * |
141 | | * This function allows setting of walk filter. |
142 | | * |
143 | | * The following example shows filtering of arrays up to 5 elements: |
144 | | * \code{.cpp} |
145 | | * #include <sstream> |
146 | | * #include <zserio/DebugStringUtil.h> |
147 | | * #include <zserio/Walker.h> |
148 | | * |
149 | | * SomeZserioObject object; |
150 | | * std::ostringstream stream; |
151 | | * zserio::ArrayLengthWalkFilter walkFilter(5); |
152 | | * zserio::toJsonStream(object, stream, walkFilter); |
153 | | * \endcode |
154 | | * |
155 | | * \param object Zserio object to use. |
156 | | * \param stream Output stream to use. |
157 | | * \param walkFilter WalkFilter to use by Walker. |
158 | | * \param allocator Allocator to use. |
159 | | */ |
160 | | template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type, |
161 | | typename std::enable_if< |
162 | | std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value, |
163 | | int>::type = 0> |
164 | | void toJsonStream( |
165 | | const T& object, std::ostream& stream, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC()) |
166 | 3 | { |
167 | 3 | detail::toJsonStream(object, stream, 4, walkFilter, allocator); |
168 | 3 | } |
169 | | |
170 | | /** |
171 | | * Writes contents of given zserio object to debug stream in JSON format using Walker with JsonWriter. |
172 | | * |
173 | | * This function allows setting of indentation of JSON output together with the walk filter. |
174 | | * |
175 | | * Example: |
176 | | * \code{.cpp} |
177 | | * #include <sstream> |
178 | | * #include <zserio/DebugStringUtil.h> |
179 | | * #include <zserio/Walker.h> |
180 | | * |
181 | | * SomeZserioObject object; |
182 | | * std::ostringstream stream; |
183 | | * const uint8_t indent = 4; |
184 | | * zserio::ArrayLengthWalkFilter walkFilter(5); |
185 | | * zserio::toJsonStream(object, stream, indent, walkFilter); |
186 | | * \endcode |
187 | | * |
188 | | * \param object Zserio object to use. |
189 | | * \param stream Output stream to use. |
190 | | * \param indent Indent argument for JsonWriter. |
191 | | * \param walkFilter WalkFilter to use by Walker. |
192 | | * \param allocator Allocator to use. |
193 | | */ |
194 | | template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type, |
195 | | typename std::enable_if< |
196 | | std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value, |
197 | | int>::type = 0> |
198 | | void toJsonStream(const T& object, std::ostream& stream, uint8_t indent, WALK_FILTER&& walkFilter, |
199 | | const ALLOC& allocator = ALLOC()) |
200 | 3 | { |
201 | 3 | detail::toJsonStream(object, stream, indent, walkFilter, allocator); |
202 | 3 | } |
203 | | |
204 | | /** |
205 | | * Gets debug string in JSON format using Walker with JsonWriter for given zserio object. |
206 | | * |
207 | | * Example: |
208 | | * \code{.cpp} |
209 | | * #include <iostream> |
210 | | * #include <zserio/DebugStringUtil.h> |
211 | | * |
212 | | * SomeZserioObject object; |
213 | | * std::cout << zserio::toJsonString(object) << std::endl; |
214 | | * \endcode |
215 | | * |
216 | | * \param object Zserio object to use. |
217 | | * \param allocator Allocator to use. |
218 | | * |
219 | | * \return JSON debug string. |
220 | | */ |
221 | | template <typename T, typename ALLOC = typename T::allocator_type, |
222 | | typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0> |
223 | | string<ALLOC> toJsonString(const T& object, const ALLOC& allocator = ALLOC()) |
224 | 3 | { |
225 | 3 | return detail::toJsonString(object, 4, BasicDefaultWalkFilter<ALLOC>(), allocator); |
226 | 3 | } |
227 | | |
228 | | /** |
229 | | * Gets debug string in JSON format using Walker with JsonWriter for given zserio object. |
230 | | * |
231 | | * This function allows setting of indentation of JSON output. |
232 | | * |
233 | | * Example: |
234 | | * \code{.cpp} |
235 | | * #include <iostream> |
236 | | * #include <zserio/DebugStringUtil.h> |
237 | | * |
238 | | * SomeZserioObject object; |
239 | | * const uint8_t indent = 4; |
240 | | * std::cout << zserio::toJsonString(object, indent) << std::endl; |
241 | | * \endcode |
242 | | * |
243 | | * \param object Zserio object to use. |
244 | | * \param indent Indent argument for JsonWriter. |
245 | | * \param allocator Allocator to use. |
246 | | * |
247 | | * \return JSON debug string. |
248 | | */ |
249 | | template <typename T, typename ALLOC = typename T::allocator_type, |
250 | | typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0> |
251 | | string<ALLOC> toJsonString(const T& object, uint8_t indent, const ALLOC& allocator = ALLOC()) |
252 | 3 | { |
253 | 3 | return detail::toJsonString(object, indent, BasicDefaultWalkFilter<ALLOC>(), allocator); |
254 | 3 | } |
255 | | |
256 | | /** |
257 | | * Gets debug string in JSON format using Walker with JsonWriter for given zserio object. |
258 | | * |
259 | | * This function allows setting of walk filter. |
260 | | * |
261 | | * The following example shows filtering of arrays up to 5 elements: |
262 | | * \code{.cpp} |
263 | | * #include <iostream> |
264 | | * #include <zserio/DebugStringUtil.h> |
265 | | * |
266 | | * SomeZserioObject object; |
267 | | * zserio::ArrayLengthWalkFilter walkFilter(5); |
268 | | * std::cout << zserio::toJsonString(object, walkFilter) << std::endl; |
269 | | * \endcode |
270 | | * |
271 | | * \param object Zserio object to use. |
272 | | * \param walkFilter WalkFilter to use by Walker. |
273 | | * \param allocator Allocator to use. |
274 | | * |
275 | | * \return JSON debug string. |
276 | | */ |
277 | | template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type, |
278 | | typename std::enable_if< |
279 | | std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value, |
280 | | int>::type = 0> |
281 | | string<ALLOC> toJsonString(const T& object, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC()) |
282 | 3 | { |
283 | 3 | return detail::toJsonString(object, 4, walkFilter, allocator); |
284 | 3 | } |
285 | | |
286 | | /** |
287 | | * Gets debug string in JSON format using Walker with JsonWriter for given zserio object. |
288 | | * |
289 | | * This function allows setting of indentation of JSON output together with the walk filter. |
290 | | * |
291 | | * Example: |
292 | | * \code{.cpp} |
293 | | * #include <iostream> |
294 | | * #include <zserio/DebugStringUtil.h> |
295 | | * |
296 | | * SomeZserioObject object; |
297 | | * const uint8_t indent = 4; |
298 | | * zserio::ArrayLengthWalkFilter walkFilter(5); |
299 | | * std::cout << zserio::toJsonString(object, indent, walkFilter) << std::endl; |
300 | | * \endcode |
301 | | * |
302 | | * \param object Zserio object to use. |
303 | | * \param indent Indent argument for JsonWriter. |
304 | | * \param walkFilter WalkFilter to use by Walker. |
305 | | * \param allocator Allocator to use. |
306 | | * |
307 | | * \return JSON debug string. |
308 | | */ |
309 | | template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type, |
310 | | typename std::enable_if< |
311 | | std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value, |
312 | | int>::type = 0> |
313 | | string<ALLOC> toJsonString( |
314 | | const T& object, uint8_t indent, WALK_FILTER&& walkFilter, const ALLOC& allocator = ALLOC()) |
315 | 3 | { |
316 | 3 | return detail::toJsonString(object, indent, walkFilter, allocator); |
317 | 3 | } |
318 | | |
319 | | /** |
320 | | * Writes contents of given zserio object to debug file in JSON format using Walker with JsonWriter. |
321 | | * |
322 | | * Example: |
323 | | * \code{.cpp} |
324 | | * #include <zserio/DebugStringUtil.h> |
325 | | * |
326 | | * SomeZserioObject object; |
327 | | * zserio::toJsonFile(object, "FileName.json"); |
328 | | * \endcode |
329 | | * |
330 | | * \param object Zserio object to use. |
331 | | * \param fileName Name of file to write. |
332 | | * \param allocator Allocator to use. |
333 | | */ |
334 | | template <typename T, typename ALLOC = typename T::allocator_type, |
335 | | typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0> |
336 | | void toJsonFile(const T& object, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC()) |
337 | 4 | { |
338 | 4 | return detail::toJsonFile(object, fileName, 4, BasicDefaultWalkFilter<ALLOC>(), allocator); |
339 | 4 | } |
340 | | |
341 | | /** |
342 | | * Writes contents of given zserio object to debug file in JSON format using Walker with JsonWriter. |
343 | | * |
344 | | * This function allows setting of indentation of JSON output. |
345 | | * |
346 | | * Example: |
347 | | * \code{.cpp} |
348 | | * #include <zserio/DebugStringUtil.h> |
349 | | * |
350 | | * SomeZserioObject object; |
351 | | * const uint8_t indent = 4; |
352 | | * zserio::toJsonFile(object, "FileName.json", indent); |
353 | | * \endcode |
354 | | * |
355 | | * \param object Zserio object to use. |
356 | | * \param fileName Name of file to write. |
357 | | * \param indent Indent argument for JsonWriter. |
358 | | * \param allocator Allocator to use. |
359 | | * |
360 | | * \throw CppRuntimeException When the writing fails. |
361 | | */ |
362 | | template <typename T, typename ALLOC = typename T::allocator_type, |
363 | | typename std::enable_if<is_allocator<ALLOC>::value, int>::type = 0> |
364 | | void toJsonFile( |
365 | | const T& object, const string<ALLOC>& fileName, uint8_t indent, const ALLOC& allocator = ALLOC()) |
366 | 3 | { |
367 | 3 | return detail::toJsonFile(object, fileName, indent, BasicDefaultWalkFilter<ALLOC>(), allocator); |
368 | 3 | } |
369 | | |
370 | | /** |
371 | | * Writes contents of given zserio object to debug file in JSON format using Walker with JsonWriter. |
372 | | * |
373 | | * This function allows setting of walk filter. |
374 | | * |
375 | | * The following example shows filtering of arrays up to 5 elements: |
376 | | * |
377 | | * Example: |
378 | | * \code{.cpp} |
379 | | * #include <zserio/DebugStringUtil.h> |
380 | | * |
381 | | * SomeZserioObject object; |
382 | | * zserio::ArrayLengthWalkFilter walkFilter(5); |
383 | | * zserio::toJsonFile(object, "FileName.json", walkFilter); |
384 | | * \endcode |
385 | | * |
386 | | * \param object Zserio object to use. |
387 | | * \param fileName Name of file to write. |
388 | | * \param walkFilter WalkFilter to use by Walker. |
389 | | * \param allocator Allocator to use. |
390 | | */ |
391 | | template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type, |
392 | | typename std::enable_if< |
393 | | std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value, |
394 | | int>::type = 0> |
395 | | void toJsonFile(const T& object, const string<ALLOC>& fileName, WALK_FILTER&& walkFilter, |
396 | | const ALLOC& allocator = ALLOC()) |
397 | 3 | { |
398 | 3 | return detail::toJsonFile(object, fileName, 4, walkFilter, allocator); |
399 | 3 | } |
400 | | |
401 | | /** |
402 | | * Writes contents of given zserio object to debug file in JSON format using Walker with JsonWriter. |
403 | | * |
404 | | * This function allows setting of indentation of JSON output together with the walk filter. |
405 | | * |
406 | | * Example: |
407 | | * \code{.cpp} |
408 | | * #include <zserio/DebugStringUtil.h> |
409 | | * |
410 | | * SomeZserioObject object; |
411 | | * const uint8_t indent = 4; |
412 | | * zserio::ArrayLengthWalkFilter walkFilter(5); |
413 | | * zserio::toJsonFile(object, "FileName.json", indent, walkFilter); |
414 | | * \endcode |
415 | | * |
416 | | * \param object Zserio object to use. |
417 | | * \param fileName Name of file to write. |
418 | | * \param indent Indent argument for JsonWriter. |
419 | | * \param walkFilter WalkFilter to use by Walker. |
420 | | * \param allocator Allocator to use. |
421 | | */ |
422 | | template <typename T, typename WALK_FILTER, typename ALLOC = typename T::allocator_type, |
423 | | typename std::enable_if< |
424 | | std::is_base_of<IBasicWalkFilter<ALLOC>, typename std::decay<WALK_FILTER>::type>::value, |
425 | | int>::type = 0> |
426 | | void toJsonFile(const T& object, const string<ALLOC>& fileName, uint8_t indent, WALK_FILTER&& walkFilter, |
427 | | const ALLOC& allocator = ALLOC()) |
428 | 3 | { |
429 | 3 | return detail::toJsonFile(object, fileName, indent, walkFilter, allocator); |
430 | 3 | } |
431 | | |
432 | | /** |
433 | | * Parses JSON debug string from given text stream and creates instance of the requested zserio object |
434 | | * according to the data contained in the debug string. |
435 | | * |
436 | | * \note The created object can be only partially initialized depending on the JSON debug string. |
437 | | * |
438 | | * Example: |
439 | | * \code{.cpp} |
440 | | * #include <sstream> |
441 | | * #include <zserio/DebugStringUtil.h> |
442 | | * |
443 | | * std::istringstream is("{ \"fieldU32\": 13 }"); |
444 | | * IReflectablePtr reflectable = fromJsonStream(SomeZserioObject::typeInfo(), is); |
445 | | * |
446 | | * reflectable->getField("fieldU32")->getUInt32(); // 13 |
447 | | * \endcode |
448 | | * |
449 | | * \param typeInfo Type info of the generated zserio object to create. |
450 | | * \param is Text stream to use. |
451 | | * \param allocator Allocator to use. |
452 | | * |
453 | | * \return Reflectable instance of the requested zserio object. |
454 | | * \throw CppRuntimeException In case of any error. |
455 | | */ |
456 | | template <typename ALLOC = std::allocator<uint8_t>> |
457 | | typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonStream( |
458 | | const IBasicTypeInfo<ALLOC>& typeInfo, std::istream& is, const ALLOC& allocator = ALLOC()) |
459 | 32 | { |
460 | 32 | BasicJsonReader<ALLOC> jsonReader(is, allocator); |
461 | 32 | return jsonReader.read(typeInfo); |
462 | 32 | } |
463 | | |
464 | | /** |
465 | | * Parses JSON debug string from given text stream and creates instance of the requested zserio object |
466 | | * according to the data contained in the debug string. |
467 | | * |
468 | | * \note The created object can be only partially initialized depending on the JSON debug string. |
469 | | * \note An reflectable instance is created first and then the object is move-constructed from it's any value. |
470 | | * |
471 | | * Example: |
472 | | * \code{.cpp} |
473 | | * #include <sstream> |
474 | | * #include <zserio/DebugStringUtil.h> |
475 | | * |
476 | | * std::istringstream is("{ \"fieldU32\": 13 }"); |
477 | | * SomeZserioObject someZserioObject = fromJsonStream<SomeZserioObject>(is); |
478 | | * |
479 | | * someZserioObject.getFieldU32(); // 13 |
480 | | * \endcode |
481 | | * |
482 | | * \param is Text stream to use. |
483 | | * \param allocator Allocator to use. |
484 | | * |
485 | | * \return Instance of the requested zserio object. |
486 | | * \throw CppRuntimeException In case of any error. |
487 | | */ |
488 | | template <typename T, typename ALLOC = typename T::allocator_type> |
489 | | T fromJsonStream(std::istream& is, const ALLOC& allocator = ALLOC()) |
490 | 5 | { |
491 | 5 | return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonStream(T::typeInfo(), is, allocator))); |
492 | 5 | } |
493 | | |
494 | | /** |
495 | | * Parses JSON debug string from given JSON string and creates instance of the requested zserio object |
496 | | * according to the data contained in the debug string. |
497 | | * |
498 | | * \note The created object can be only partially initialized depending on the JSON debug string. |
499 | | * |
500 | | * Example: |
501 | | * \code{.cpp} |
502 | | * #include <sstream> |
503 | | * #include <zserio/DebugStringUtil.h> |
504 | | * |
505 | | * std::string str("{ \"fieldU32\": 13 }") |
506 | | * IReflectablePtr reflectable = fromJsonString(SomeZserioObject::typeInfo(), str); |
507 | | * |
508 | | * reflectable->getField("fieldU32")->getUInt32(); // 13 |
509 | | * \endcode |
510 | | * |
511 | | * \param typeInfo Type info of the generated zserio object to create. |
512 | | * \param json String to use. |
513 | | * \param allocator Allocator to use. |
514 | | * |
515 | | * \return Reflectable instance of the requested zserio object. |
516 | | * \throw CppRuntimeException In case of any error. |
517 | | */ |
518 | | template <typename ALLOC = std::allocator<uint8_t>> |
519 | | typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonString( |
520 | | const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& json, const ALLOC& allocator = ALLOC()) |
521 | 10 | { |
522 | 10 | std::basic_istringstream<char, std::char_traits<char>, RebindAlloc<ALLOC, char>> stream(json); |
523 | 10 | return fromJsonStream(typeInfo, stream, allocator); |
524 | 10 | } |
525 | | |
526 | | /** |
527 | | * Parses JSON debug string from given JSON string and creates instance of the requested zserio object |
528 | | * according to the data contained in the debug string. |
529 | | * |
530 | | * \note The created object can be only partially initialized depending on the JSON debug string. |
531 | | * \note An reflectable instance is created first and then the object is move-constructed from it's any value. |
532 | | * |
533 | | * Example: |
534 | | * \code{.cpp} |
535 | | * #include <sstream> |
536 | | * #include <zserio/DebugStringUtil.h> |
537 | | * |
538 | | * std::string str("{ \"fieldU32\": 13 }") |
539 | | * SomeZserioObject someZserioObject = fromJsonString<SomeZserioObject>(str); |
540 | | * |
541 | | * someZserioObject.getFieldU32(); // 13 |
542 | | * \endcode |
543 | | * |
544 | | * \param json String to use. |
545 | | * \param allocator Allocator to use. |
546 | | * |
547 | | * \return Instance of the requested zserio object. |
548 | | * \throw CppRuntimeException In case of any error. |
549 | | */ |
550 | | template <typename T, typename ALLOC = typename T::allocator_type> |
551 | | T fromJsonString(const string<ALLOC>& json, const ALLOC& allocator = ALLOC()) |
552 | 5 | { |
553 | 5 | return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonString(T::typeInfo(), json, allocator))); |
554 | 5 | } |
555 | | |
556 | | /** |
557 | | * Parses JSON debug string from given text file and creates instance of the requested zserio object |
558 | | * according to the data contained in the debug string. |
559 | | * |
560 | | * \note The created object can be only partially initialized depending on the JSON debug string. |
561 | | * |
562 | | * Example: |
563 | | * \code{.cpp} |
564 | | * #include <sstream> |
565 | | * #include <zserio/DebugStringUtil.h> |
566 | | * |
567 | | * IReflectablePtr reflectable = fromJsonFile(SomeZserioObject::typeInfo(), "FileName.json"); |
568 | | * |
569 | | * reflectable->getField("fieldU32")->getUInt32(); // 13 |
570 | | * \endcode |
571 | | * |
572 | | * \param typeInfo Type info of the generated zserio object to create. |
573 | | * \param fileName Name of file to read. |
574 | | * \param allocator Allocator to use. |
575 | | * |
576 | | * \return Reflectable instance of the requested zserio object. |
577 | | * \throw CppRuntimeException In case of any error. |
578 | | */ |
579 | | template <typename ALLOC = std::allocator<uint8_t>> |
580 | | typename detail::DebugStringTraits<ALLOC>::ReflectablePtr fromJsonFile( |
581 | | const IBasicTypeInfo<ALLOC>& typeInfo, const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC()) |
582 | 13 | { |
583 | 13 | std::ifstream is = std::ifstream(fileName.c_str()); |
584 | 13 | if (!is) |
585 | 1 | { |
586 | 1 | throw CppRuntimeException("DebugStringUtil.fromJsonFile: Failed to open '") << fileName + "'!"; |
587 | 1 | } |
588 | | |
589 | 12 | return fromJsonStream(typeInfo, is, allocator); |
590 | 13 | } |
591 | | |
592 | | /** |
593 | | * Parses JSON debug string from given text file and creates instance of the requested zserio object |
594 | | * according to the data contained in the debug string. |
595 | | * |
596 | | * \note The created object can be only partially initialized depending on the JSON debug string. |
597 | | * \note An reflectable instance is created first and then the object is move-constructed from it's any value. |
598 | | * |
599 | | * Example: |
600 | | * \code{.cpp} |
601 | | * #include <sstream> |
602 | | * #include <zserio/DebugStringUtil.h> |
603 | | * |
604 | | * SomeZserioObject someZserioObject = fromJsonFile<SomeZserioObject>("FileName.json"); |
605 | | * |
606 | | * someZserioObject.getFieldU32(); // 13 |
607 | | * \endcode |
608 | | * |
609 | | * \param fileName Name of file to read. |
610 | | * \param allocator Allocator to use. |
611 | | * |
612 | | * \return Instance of the requested zserio object. |
613 | | * \throw CppRuntimeException In case of any error. |
614 | | */ |
615 | | template <typename T, typename ALLOC = typename T::allocator_type> |
616 | | T fromJsonFile(const string<ALLOC>& fileName, const ALLOC& allocator = ALLOC()) |
617 | 6 | { |
618 | 6 | return std::move(ReflectableUtil::getValue<T, ALLOC>(fromJsonFile(T::typeInfo(), fileName, allocator))); |
619 | 6 | } |
620 | | |
621 | | } // namespace zserio |
622 | | |
623 | | #endif // ZSERIO_DEBUG_STRING_UTIL_H_INC |