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