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