Coverage Report

Created: 2023-12-13 14:58

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