Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef ZSERIO_JSON_READER_H_INC |
2 | | #define ZSERIO_JSON_READER_H_INC |
3 | | |
4 | | #include <istream> |
5 | | #include <limits> |
6 | | |
7 | | #include "zserio/AllocatorHolder.h" |
8 | | #include "zserio/JsonParser.h" |
9 | | #include "zserio/OptionalHolder.h" |
10 | | #include "zserio/SizeConvertUtil.h" |
11 | | #include "zserio/StringView.h" |
12 | | #include "zserio/Types.h" |
13 | | #include "zserio/UniquePtr.h" |
14 | | #include "zserio/ZserioTreeCreator.h" |
15 | | |
16 | | namespace zserio |
17 | | { |
18 | | |
19 | | namespace detail |
20 | | { |
21 | | |
22 | | // adapter for values which are encoded as a JSON object |
23 | | template <typename ALLOC> |
24 | | class IObjectValueAdapter : public BasicJsonParser<ALLOC>::IObserver |
25 | | { |
26 | | public: |
27 | | virtual AnyHolder<ALLOC> get() const = 0; |
28 | | }; |
29 | | |
30 | | template <typename ALLOC> |
31 | | class BitBufferAdapter : public IObjectValueAdapter<ALLOC>, public AllocatorHolder<ALLOC> |
32 | | { |
33 | | public: |
34 | | using AllocatorHolder<ALLOC>::get_allocator; |
35 | | |
36 | | explicit BitBufferAdapter(const ALLOC& allocator) : |
37 | | AllocatorHolder<ALLOC>(allocator), |
38 | | m_state(VISIT_KEY) |
39 | 21 | {} |
40 | 21 | ~BitBufferAdapter() override = default; |
41 | | |
42 | | BitBufferAdapter(BitBufferAdapter& other) = delete; |
43 | | BitBufferAdapter& operator=(BitBufferAdapter& other) = delete; |
44 | | |
45 | | BitBufferAdapter(BitBufferAdapter&& other) : |
46 | | m_state(other.m_state), |
47 | | m_buffer(std::move(other.m_buffer)), |
48 | | m_bitSize(other.m_bitSize) |
49 | | {} |
50 | | |
51 | | BitBufferAdapter& operator=(BitBufferAdapter&& other) |
52 | | { |
53 | | m_state = other.m_state; |
54 | | m_buffer = std::move(other.m_buffer); |
55 | | m_bitSize = other.m_bitSize; |
56 | | |
57 | | return *this; |
58 | | } |
59 | | |
60 | | AnyHolder<ALLOC> get() const override; |
61 | | |
62 | | void beginObject() override; |
63 | | void endObject() override; |
64 | | void beginArray() override; |
65 | | void endArray() override; |
66 | | void visitKey(StringView key) override; |
67 | | void visitValue(std::nullptr_t) override; |
68 | | void visitValue(bool boolValue) override; |
69 | | void visitValue(int64_t intValue) override; |
70 | | void visitValue(uint64_t uintValue) override; |
71 | | void visitValue(double doubleValue) override; |
72 | | void visitValue(StringView stringValue) override; |
73 | | |
74 | | private: |
75 | | enum State : uint8_t |
76 | | { |
77 | | VISIT_KEY, |
78 | | BEGIN_ARRAY_BUFFER, |
79 | | VISIT_VALUE_BUFFER, |
80 | | VISIT_VALUE_BITSIZE |
81 | | }; |
82 | | |
83 | | State m_state; |
84 | | InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer; |
85 | | InplaceOptionalHolder<size_t> m_bitSize; |
86 | | }; |
87 | | |
88 | | template <typename ALLOC> |
89 | | class BytesAdapter : public IObjectValueAdapter<ALLOC>, public AllocatorHolder<ALLOC> |
90 | | { |
91 | | public: |
92 | | using AllocatorHolder<ALLOC>::get_allocator; |
93 | | |
94 | | explicit BytesAdapter(const ALLOC& allocator) : |
95 | | AllocatorHolder<ALLOC>(allocator), |
96 | | m_state(VISIT_KEY) |
97 | 14 | {} |
98 | 14 | ~BytesAdapter() override = default; |
99 | | |
100 | | BytesAdapter(BytesAdapter& other) = delete; |
101 | | BytesAdapter& operator=(BytesAdapter& other) = delete; |
102 | | |
103 | | BytesAdapter(BytesAdapter&& other) : |
104 | | m_state(other.m_state), |
105 | | m_buffer(std::move(other.m_buffer)) |
106 | | {} |
107 | | |
108 | | BytesAdapter& operator=(BytesAdapter&& other) |
109 | | { |
110 | | m_state = other.m_state; |
111 | | m_buffer = std::move(other.m_buffer); |
112 | | |
113 | | return *this; |
114 | | } |
115 | | |
116 | | AnyHolder<ALLOC> get() const override; |
117 | | |
118 | | void beginObject() override; |
119 | | void endObject() override; |
120 | | void beginArray() override; |
121 | | void endArray() override; |
122 | | void visitKey(StringView key) override; |
123 | | void visitValue(std::nullptr_t) override; |
124 | | void visitValue(bool boolValue) override; |
125 | | void visitValue(int64_t intValue) override; |
126 | | void visitValue(uint64_t uintValue) override; |
127 | | void visitValue(double doubleValue) override; |
128 | | void visitValue(StringView stringValue) override; |
129 | | |
130 | | private: |
131 | | enum State : uint8_t |
132 | | { |
133 | | VISIT_KEY, |
134 | | BEGIN_ARRAY_BUFFER, |
135 | | VISIT_VALUE_BUFFER, |
136 | | }; |
137 | | |
138 | | State m_state; |
139 | | InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer; |
140 | | }; |
141 | | |
142 | | template <typename ALLOC> |
143 | | class CreatorAdapter : public BasicJsonParser<ALLOC>::IObserver, public AllocatorHolder<ALLOC> |
144 | | { |
145 | | public: |
146 | | using AllocatorHolder<ALLOC>::get_allocator; |
147 | | |
148 | | explicit CreatorAdapter(const ALLOC& allocator) : |
149 | | AllocatorHolder<ALLOC>(allocator) |
150 | 77 | {} |
151 | | |
152 | | void setType(const IBasicTypeInfo<ALLOC>& typeInfo); |
153 | | IBasicReflectablePtr<ALLOC> get() const; |
154 | | |
155 | | void beginObject() override; |
156 | | void endObject() override; |
157 | | void beginArray() override; |
158 | | void endArray() override; |
159 | | void visitKey(StringView key) override; |
160 | | void visitValue(std::nullptr_t) override; |
161 | | void visitValue(bool boolValue) override; |
162 | | void visitValue(int64_t intValue) override; |
163 | | void visitValue(uint64_t uintValue) override; |
164 | | void visitValue(double doubleValue) override; |
165 | | void visitValue(StringView stringValue) override; |
166 | | |
167 | | private: |
168 | | template <typename T> |
169 | | void setValue(T&& value); |
170 | | |
171 | | template <typename T> |
172 | | void convertValue(T&& value) const; |
173 | | |
174 | | InplaceOptionalHolder<BasicZserioTreeCreator<ALLOC>> m_creator; |
175 | | vector<string<ALLOC>, ALLOC> m_keyStack; |
176 | | IBasicReflectablePtr<ALLOC> m_object; |
177 | | std::shared_ptr<IObjectValueAdapter<ALLOC>> m_objectValueAdapter; |
178 | | }; |
179 | | |
180 | | } // namespace detail |
181 | | |
182 | | /** |
183 | | * Reads zserio object tree defined by a type info from a text stream. |
184 | | */ |
185 | | template <typename ALLOC = std::allocator<uint8_t>> |
186 | | class BasicJsonReader |
187 | | { |
188 | | public: |
189 | | /** |
190 | | * Constructor. |
191 | | * |
192 | | * \param in Text stream to read. |
193 | | * \param allocator Allocator to use. |
194 | | */ |
195 | | explicit BasicJsonReader(std::istream& in, const ALLOC& allocator = ALLOC()) : |
196 | | m_creatorAdapter(allocator), |
197 | | m_parser(in, m_creatorAdapter, allocator) |
198 | 76 | {} |
199 | | |
200 | | /** |
201 | | * Reads a zserio object tree defined by the given type info from the text stream. |
202 | | * |
203 | | * \param typeInfo Type info defining the expected zserio object tree. |
204 | | * |
205 | | * \return Zserio object tree initialized using the JSON data. |
206 | | * \throw CppRuntimeException When the JSON doesn't contain expected zserio object tree. |
207 | | */ |
208 | | IBasicReflectablePtr<ALLOC> read(const IBasicTypeInfo<ALLOC>& typeInfo) |
209 | 77 | { |
210 | 77 | m_creatorAdapter.setType(typeInfo); |
211 | | |
212 | 77 | try |
213 | 77 | { |
214 | 77 | m_parser.parse(); |
215 | 77 | } |
216 | 77 | catch (const JsonParserException&) |
217 | 77 | { |
218 | 2 | throw; |
219 | 2 | } |
220 | 77 | catch (const CppRuntimeException& e) |
221 | 77 | { |
222 | 28 | throw CppRuntimeException(e.what()) |
223 | 28 | << " (JsonParser:" << m_parser.getLine() << ":" << m_parser.getColumn() << ")"; |
224 | 28 | } |
225 | | |
226 | 47 | return m_creatorAdapter.get(); |
227 | 77 | } |
228 | | |
229 | | private: |
230 | | detail::CreatorAdapter<ALLOC> m_creatorAdapter; |
231 | | BasicJsonParser<ALLOC> m_parser; |
232 | | }; |
233 | | |
234 | | /** Typedef to Json Reader provided for convenience - using default std::allocator<uint8_t>. */ |
235 | | using JsonReader = BasicJsonReader<>; |
236 | | |
237 | | namespace detail |
238 | | { |
239 | | |
240 | | template <typename ALLOC> |
241 | | AnyHolder<ALLOC> BitBufferAdapter<ALLOC>::get() const |
242 | 14 | { |
243 | 14 | if (m_state != VISIT_KEY || !m_buffer.hasValue()13 || !m_bitSize.hasValue()12 ) |
244 | 3 | { |
245 | 3 | throw CppRuntimeException("JsonReader: Unexpected end in BitBuffer!"); |
246 | 3 | } |
247 | | |
248 | 11 | return AnyHolder<ALLOC>(BasicBitBuffer<ALLOC>(m_buffer.value(), m_bitSize.value()), get_allocator()); |
249 | 14 | } |
250 | | |
251 | | template <typename ALLOC> |
252 | | void BitBufferAdapter<ALLOC>::beginObject() |
253 | 2 | { |
254 | 2 | throw CppRuntimeException("JsonReader: Unexpected beginObject in BitBuffer!"); |
255 | 2 | } |
256 | | |
257 | | template <typename ALLOC> |
258 | | void BitBufferAdapter<ALLOC>::endObject() |
259 | 1 | { |
260 | 1 | throw CppRuntimeException("JsonReader: Unexpected endObject in BitBuffer!"); |
261 | 1 | } |
262 | | |
263 | | template <typename ALLOC> |
264 | | void BitBufferAdapter<ALLOC>::beginArray() |
265 | 21 | { |
266 | 21 | if (m_state == BEGIN_ARRAY_BUFFER) |
267 | 20 | { |
268 | 20 | m_state = VISIT_VALUE_BUFFER; |
269 | 20 | m_buffer = vector<uint8_t, ALLOC>(get_allocator()); |
270 | 20 | } |
271 | 1 | else |
272 | 1 | { |
273 | 1 | throw CppRuntimeException("JsonReader: Unexpected beginArray in BitBuffer!"); |
274 | 1 | } |
275 | 21 | } |
276 | | |
277 | | template <typename ALLOC> |
278 | | void BitBufferAdapter<ALLOC>::endArray() |
279 | 19 | { |
280 | 19 | if (m_state == VISIT_VALUE_BUFFER) |
281 | 18 | { |
282 | 18 | m_state = VISIT_KEY; |
283 | 18 | } |
284 | 1 | else |
285 | 1 | { |
286 | 1 | throw CppRuntimeException("JsonReader: Unexpected endArray in BitBuffer!"); |
287 | 1 | } |
288 | 19 | } |
289 | | |
290 | | template <typename ALLOC> |
291 | | void BitBufferAdapter<ALLOC>::visitKey(StringView key) |
292 | 40 | { |
293 | 40 | if (m_state == VISIT_KEY) |
294 | 39 | { |
295 | 39 | if (key == "buffer"_sv) |
296 | 21 | { |
297 | 21 | m_state = BEGIN_ARRAY_BUFFER; |
298 | 21 | } |
299 | 18 | else if (key == "bitSize"_sv) |
300 | 17 | { |
301 | 17 | m_state = VISIT_VALUE_BITSIZE; |
302 | 17 | } |
303 | 1 | else |
304 | 1 | { |
305 | 1 | throw CppRuntimeException("JsonReader: Unexpected key '") << key << "' in BitBuffer!"; |
306 | 1 | } |
307 | 39 | } |
308 | 1 | else |
309 | 1 | { |
310 | 1 | throw CppRuntimeException("JsonReader: Unexpected visitKey in BitBuffer!"); |
311 | 1 | } |
312 | 40 | } |
313 | | |
314 | | template <typename ALLOC> |
315 | | void BitBufferAdapter<ALLOC>::visitValue(std::nullptr_t) |
316 | 2 | { |
317 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue (null) in BitBuffer!"); |
318 | 2 | } |
319 | | |
320 | | template <typename ALLOC> |
321 | | void BitBufferAdapter<ALLOC>::visitValue(bool) |
322 | 2 | { |
323 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue (bool) in BitBuffer!"); |
324 | 2 | } |
325 | | |
326 | | template <typename ALLOC> |
327 | | void BitBufferAdapter<ALLOC>::visitValue(int64_t) |
328 | 2 | { |
329 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue (int) in BitBuffer!"); |
330 | 2 | } |
331 | | |
332 | | template <typename ALLOC> |
333 | | void BitBufferAdapter<ALLOC>::visitValue(uint64_t uintValue) |
334 | 41 | { |
335 | 41 | if (m_state == VISIT_VALUE_BUFFER) |
336 | 28 | { |
337 | 28 | if (uintValue > static_cast<uint64_t>(std::numeric_limits<uint8_t>::max())) |
338 | 1 | { |
339 | 1 | throw CppRuntimeException("JsonReader: Cannot create byte for Bit Buffer from value '") |
340 | 1 | << uintValue << "'!"; |
341 | 1 | } |
342 | | |
343 | 27 | m_buffer->push_back(static_cast<uint8_t>(uintValue)); |
344 | 27 | } |
345 | 13 | else if (m_state == VISIT_VALUE_BITSIZE) |
346 | 11 | { |
347 | 11 | m_bitSize = convertUInt64ToSize(uintValue); |
348 | 11 | m_state = VISIT_KEY; |
349 | 11 | } |
350 | 2 | else |
351 | 2 | { |
352 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue in BitBuffer!"); |
353 | 2 | } |
354 | 41 | } |
355 | | |
356 | | template <typename ALLOC> |
357 | | void BitBufferAdapter<ALLOC>::visitValue(double) |
358 | 2 | { |
359 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue (double) in BitBuffer!"); |
360 | 2 | } |
361 | | |
362 | | template <typename ALLOC> |
363 | | void BitBufferAdapter<ALLOC>::visitValue(StringView) |
364 | 2 | { |
365 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue (string) in BitBuffer!"); |
366 | 2 | } |
367 | | |
368 | | template <typename ALLOC> |
369 | | AnyHolder<ALLOC> BytesAdapter<ALLOC>::get() const |
370 | 12 | { |
371 | 12 | if (m_state != VISIT_KEY || !m_buffer.hasValue()11 ) |
372 | 2 | { |
373 | 2 | throw CppRuntimeException("JsonReader: Unexpected end in bytes!"); |
374 | 2 | } |
375 | | |
376 | 10 | return AnyHolder<ALLOC>(m_buffer.value(), get_allocator()); |
377 | 12 | } |
378 | | |
379 | | template <typename ALLOC> |
380 | | void BytesAdapter<ALLOC>::beginObject() |
381 | 2 | { |
382 | 2 | throw CppRuntimeException("JsonReader: Unexpected beginObject in bytes!"); |
383 | 2 | } |
384 | | |
385 | | template <typename ALLOC> |
386 | | void BytesAdapter<ALLOC>::endObject() |
387 | 1 | { |
388 | 1 | throw CppRuntimeException("JsonReader: Unexpected endObject in bytes!"); |
389 | 1 | } |
390 | | |
391 | | template <typename ALLOC> |
392 | | void BytesAdapter<ALLOC>::beginArray() |
393 | 13 | { |
394 | 13 | if (m_state == BEGIN_ARRAY_BUFFER) |
395 | 12 | { |
396 | 12 | m_state = VISIT_VALUE_BUFFER; |
397 | 12 | m_buffer = vector<uint8_t, ALLOC>(get_allocator()); |
398 | 12 | } |
399 | 1 | else |
400 | 1 | { |
401 | 1 | throw CppRuntimeException("JsonReader: Unexpected beginArray in bytes!"); |
402 | 1 | } |
403 | 13 | } |
404 | | |
405 | | template <typename ALLOC> |
406 | | void BytesAdapter<ALLOC>::endArray() |
407 | 11 | { |
408 | 11 | if (m_state == VISIT_VALUE_BUFFER) |
409 | 10 | { |
410 | 10 | m_state = VISIT_KEY; |
411 | 10 | } |
412 | 1 | else |
413 | 1 | { |
414 | 1 | throw CppRuntimeException("JsonReader: Unexpected endArray in bytes!"); |
415 | 1 | } |
416 | 11 | } |
417 | | |
418 | | template <typename ALLOC> |
419 | | void BytesAdapter<ALLOC>::visitKey(StringView key) |
420 | 16 | { |
421 | 16 | if (m_state == VISIT_KEY) |
422 | 15 | { |
423 | 15 | if (key == "buffer"_sv) |
424 | 14 | { |
425 | 14 | m_state = BEGIN_ARRAY_BUFFER; |
426 | 14 | } |
427 | 1 | else |
428 | 1 | { |
429 | 1 | throw CppRuntimeException("JsonReader: Unexpected key '") << key << "' in bytes!"; |
430 | 1 | } |
431 | 15 | } |
432 | 1 | else |
433 | 1 | { |
434 | 1 | throw CppRuntimeException("JsonReader: Unexpected visitKey in bytes!"); |
435 | 1 | } |
436 | 16 | } |
437 | | |
438 | | template <typename ALLOC> |
439 | | void BytesAdapter<ALLOC>::visitValue(std::nullptr_t) |
440 | 1 | { |
441 | 1 | throw CppRuntimeException("JsonReader: Unexpected visitValue (null) in bytes!"); |
442 | 1 | } |
443 | | |
444 | | template <typename ALLOC> |
445 | | void BytesAdapter<ALLOC>::visitValue(bool) |
446 | 1 | { |
447 | 1 | throw CppRuntimeException("JsonReader: Unexpected visitValue (bool) in bytes!"); |
448 | 1 | } |
449 | | |
450 | | template <typename ALLOC> |
451 | | void BytesAdapter<ALLOC>::visitValue(int64_t) |
452 | 2 | { |
453 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue (int) in bytes!"); |
454 | 2 | } |
455 | | |
456 | | template <typename ALLOC> |
457 | | void BytesAdapter<ALLOC>::visitValue(uint64_t uintValue) |
458 | 19 | { |
459 | 19 | if (m_state == VISIT_VALUE_BUFFER) |
460 | 17 | { |
461 | 17 | if (uintValue > static_cast<uint64_t>(std::numeric_limits<uint8_t>::max())) |
462 | 1 | { |
463 | 1 | throw CppRuntimeException("JsonReader: Cannot create byte for bytes from value '") |
464 | 1 | << uintValue << "'!"; |
465 | 1 | } |
466 | | |
467 | 16 | m_buffer->push_back(static_cast<uint8_t>(uintValue)); |
468 | 16 | } |
469 | 2 | else |
470 | 2 | { |
471 | 2 | throw CppRuntimeException("JsonReader: Unexpected visitValue in bytes!"); |
472 | 2 | } |
473 | 19 | } |
474 | | |
475 | | template <typename ALLOC> |
476 | | void BytesAdapter<ALLOC>::visitValue(double) |
477 | 1 | { |
478 | 1 | throw CppRuntimeException("JsonReader: Unexpected visitValue (double) in bytes!"); |
479 | 1 | } |
480 | | |
481 | | template <typename ALLOC> |
482 | | void BytesAdapter<ALLOC>::visitValue(StringView) |
483 | 1 | { |
484 | 1 | throw CppRuntimeException("JsonReader: Unexpected visitValue (string) in bytes!"); |
485 | 1 | } |
486 | | |
487 | | template <typename ALLOC> |
488 | | void CreatorAdapter<ALLOC>::setType(const IBasicTypeInfo<ALLOC>& typeInfo) |
489 | 77 | { |
490 | 77 | m_creator = BasicZserioTreeCreator<ALLOC>(typeInfo, get_allocator()); |
491 | 77 | } |
492 | | |
493 | | template <typename ALLOC> |
494 | | IBasicReflectablePtr<ALLOC> CreatorAdapter<ALLOC>::get() const |
495 | 48 | { |
496 | 48 | if (!m_object) |
497 | 1 | { |
498 | 1 | throw CppRuntimeException("JsonReader: Zserio tree not created!"); |
499 | 1 | } |
500 | | |
501 | 47 | return m_object; |
502 | 48 | } |
503 | | |
504 | | template <typename ALLOC> |
505 | | void CreatorAdapter<ALLOC>::beginObject() |
506 | 148 | { |
507 | 148 | if (m_objectValueAdapter) |
508 | 2 | { |
509 | | #if defined(_MSC_VER) && (_MSC_VER >= 1920 && _MSC_VER < 1930) |
510 | | // catch and rethrow exceptions here to avoid access violation error |
511 | | // in msvc v142 release build |
512 | | try |
513 | | { |
514 | | m_objectValueAdapter->beginObject(); |
515 | | } |
516 | | catch (...) |
517 | | { |
518 | | throw; |
519 | | } |
520 | | #else |
521 | 2 | m_objectValueAdapter->beginObject(); |
522 | 2 | #endif |
523 | 2 | } |
524 | 146 | else |
525 | 146 | { |
526 | 146 | if (!m_creator) |
527 | 1 | { |
528 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
529 | 1 | } |
530 | | |
531 | 145 | if (m_keyStack.empty()) |
532 | 75 | { |
533 | 75 | m_creator->beginRoot(); |
534 | 75 | } |
535 | 70 | else |
536 | 70 | { |
537 | 70 | if (!m_keyStack.back().empty()) |
538 | 64 | { |
539 | 64 | const CppType cppType = m_creator->getFieldType(m_keyStack.back()).getCppType(); |
540 | 64 | if (cppType == CppType::BIT_BUFFER) |
541 | 18 | { |
542 | 18 | m_objectValueAdapter = |
543 | 18 | std::allocate_shared<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator()); |
544 | 18 | } |
545 | 46 | else if (cppType == CppType::BYTES) |
546 | 11 | { |
547 | 11 | m_objectValueAdapter = |
548 | 11 | std::allocate_shared<BytesAdapter<ALLOC>>(get_allocator(), get_allocator()); |
549 | 11 | } |
550 | 35 | else |
551 | 35 | { |
552 | 35 | m_creator->beginCompound(m_keyStack.back()); |
553 | 35 | } |
554 | 64 | } |
555 | 6 | else |
556 | 6 | { |
557 | 6 | const CppType cppType = m_creator->getElementType().getCppType(); |
558 | 6 | if (cppType == CppType::BIT_BUFFER) |
559 | 2 | { |
560 | 2 | m_objectValueAdapter = |
561 | 2 | std::allocate_shared<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator()); |
562 | 2 | } |
563 | 4 | else if (cppType == CppType::BYTES) |
564 | 2 | { |
565 | 2 | m_objectValueAdapter = |
566 | 2 | std::allocate_shared<BytesAdapter<ALLOC>>(get_allocator(), get_allocator()); |
567 | 2 | } |
568 | 2 | else |
569 | 2 | { |
570 | 2 | m_creator->beginCompoundElement(); |
571 | 2 | } |
572 | 6 | } |
573 | 70 | } |
574 | 145 | } |
575 | 148 | } |
576 | | |
577 | | template <typename ALLOC> |
578 | | void CreatorAdapter<ALLOC>::endObject() |
579 | 84 | { |
580 | 84 | if (m_objectValueAdapter) |
581 | 22 | { |
582 | 22 | setValue(m_objectValueAdapter->get()); |
583 | 22 | m_objectValueAdapter.reset(); |
584 | 22 | } |
585 | 62 | else |
586 | 62 | { |
587 | 62 | if (!m_creator) |
588 | 1 | { |
589 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
590 | 1 | } |
591 | | |
592 | 61 | if (m_keyStack.empty()) |
593 | 47 | { |
594 | 47 | m_object = m_creator->endRoot(); |
595 | 47 | m_creator.reset(); |
596 | 47 | } |
597 | 14 | else |
598 | 14 | { |
599 | 14 | if (!m_keyStack.back().empty()) |
600 | 12 | { |
601 | 12 | m_creator->endCompound(); |
602 | 12 | m_keyStack.pop_back(); |
603 | 12 | } |
604 | 2 | else |
605 | 2 | { |
606 | 2 | m_creator->endCompoundElement(); |
607 | 2 | } |
608 | 14 | } |
609 | 61 | } |
610 | 84 | } |
611 | | |
612 | | template <typename ALLOC> |
613 | | void CreatorAdapter<ALLOC>::beginArray() |
614 | 42 | { |
615 | 42 | if (m_objectValueAdapter) |
616 | 32 | { |
617 | 32 | m_objectValueAdapter->beginArray(); |
618 | 32 | } |
619 | 10 | else |
620 | 10 | { |
621 | 10 | if (!m_creator) |
622 | 1 | { |
623 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
624 | 1 | } |
625 | | |
626 | 9 | if (m_keyStack.empty()) |
627 | 1 | { |
628 | 1 | throw CppRuntimeException("JsonReader: ZserioTreeCreator expects json object!"); |
629 | 1 | } |
630 | | |
631 | 8 | m_creator->beginArray(m_keyStack.back()); |
632 | | |
633 | 8 | m_keyStack.push_back(""); |
634 | 8 | } |
635 | 42 | } |
636 | | |
637 | | template <typename ALLOC> |
638 | | void CreatorAdapter<ALLOC>::endArray() |
639 | 37 | { |
640 | 37 | if (m_objectValueAdapter) |
641 | 28 | { |
642 | 28 | m_objectValueAdapter->endArray(); |
643 | 28 | } |
644 | 9 | else |
645 | 9 | { |
646 | 9 | if (!m_creator) |
647 | 1 | { |
648 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
649 | 1 | } |
650 | | |
651 | 8 | m_creator->endArray(); |
652 | | |
653 | 8 | m_keyStack.pop_back(); // finish array |
654 | 8 | m_keyStack.pop_back(); // finish field |
655 | 8 | } |
656 | 37 | } |
657 | | |
658 | | template <typename ALLOC> |
659 | | void CreatorAdapter<ALLOC>::visitKey(StringView key) |
660 | 232 | { |
661 | 232 | if (m_objectValueAdapter) |
662 | 50 | { |
663 | 50 | m_objectValueAdapter->visitKey(key); |
664 | 50 | } |
665 | 182 | else |
666 | 182 | { |
667 | 182 | if (!m_creator) |
668 | 1 | { |
669 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
670 | 1 | } |
671 | | |
672 | 181 | m_keyStack.push_back(toString(key, get_allocator())); |
673 | 181 | } |
674 | 232 | } |
675 | | |
676 | | template <typename ALLOC> |
677 | | void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t nullValue) |
678 | 3 | { |
679 | 3 | if (m_objectValueAdapter) |
680 | 1 | { |
681 | 1 | m_objectValueAdapter->visitValue(nullValue); |
682 | 1 | } |
683 | 2 | else |
684 | 2 | { |
685 | 2 | if (!m_creator) |
686 | 1 | { |
687 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
688 | 1 | } |
689 | | |
690 | 1 | setValue(nullValue); |
691 | 1 | } |
692 | 3 | } |
693 | | |
694 | | template <typename ALLOC> |
695 | | void CreatorAdapter<ALLOC>::visitValue(bool boolValue) |
696 | 3 | { |
697 | 3 | if (m_objectValueAdapter) |
698 | 1 | { |
699 | 1 | m_objectValueAdapter->visitValue(boolValue); |
700 | 1 | } |
701 | 2 | else |
702 | 2 | { |
703 | 2 | if (!m_creator) |
704 | 1 | { |
705 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
706 | 1 | } |
707 | | |
708 | 1 | setValue(boolValue); |
709 | 1 | } |
710 | 3 | } |
711 | | |
712 | | template <typename ALLOC> |
713 | | void CreatorAdapter<ALLOC>::visitValue(int64_t intValue) |
714 | 6 | { |
715 | 6 | if (m_objectValueAdapter) |
716 | 2 | { |
717 | 2 | m_objectValueAdapter->visitValue(intValue); |
718 | 2 | } |
719 | 4 | else |
720 | 4 | { |
721 | 4 | if (!m_creator) |
722 | 1 | { |
723 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
724 | 1 | } |
725 | | |
726 | 3 | setValue(intValue); |
727 | 3 | } |
728 | 6 | } |
729 | | |
730 | | template <typename ALLOC> |
731 | | void CreatorAdapter<ALLOC>::visitValue(uint64_t uintValue) |
732 | 94 | { |
733 | 94 | if (m_objectValueAdapter) |
734 | 56 | { |
735 | 56 | m_objectValueAdapter->visitValue(uintValue); |
736 | 56 | } |
737 | 38 | else |
738 | 38 | { |
739 | 38 | if (!m_creator) |
740 | 1 | { |
741 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
742 | 1 | } |
743 | | |
744 | 37 | setValue(uintValue); |
745 | 37 | } |
746 | 94 | } |
747 | | |
748 | | template <typename ALLOC> |
749 | | void CreatorAdapter<ALLOC>::visitValue(double doubleValue) |
750 | 3 | { |
751 | 3 | if (m_objectValueAdapter) |
752 | 1 | { |
753 | 1 | m_objectValueAdapter->visitValue(doubleValue); |
754 | 1 | } |
755 | 2 | else |
756 | 2 | { |
757 | 2 | if (!m_creator) |
758 | 1 | { |
759 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
760 | 1 | } |
761 | | |
762 | 1 | setValue(doubleValue); |
763 | 1 | } |
764 | 3 | } |
765 | | |
766 | | template <typename ALLOC> |
767 | | void CreatorAdapter<ALLOC>::visitValue(StringView stringValue) |
768 | 76 | { |
769 | 76 | if (m_objectValueAdapter) |
770 | 1 | { |
771 | 1 | m_objectValueAdapter->visitValue(stringValue); |
772 | 1 | } |
773 | 75 | else |
774 | 75 | { |
775 | 75 | if (!m_creator) |
776 | 1 | { |
777 | 1 | throw CppRuntimeException("JsonReader: Adapter not initialized!"); |
778 | 1 | } |
779 | | |
780 | 74 | setValue(stringValue); |
781 | 74 | } |
782 | 76 | } |
783 | | |
784 | | template <typename ALLOC> |
785 | | template <typename T> |
786 | | void CreatorAdapter<ALLOC>::setValue(T&& value) |
787 | 138 | { |
788 | 138 | if (m_keyStack.empty()) |
789 | 1 | { |
790 | 1 | throw CppRuntimeException("JsonReader: ZserioTreeCreator expects json object!"); |
791 | 1 | } |
792 | | |
793 | 137 | if (!m_keyStack.back().empty()) |
794 | 125 | { |
795 | 125 | m_creator->setValue(m_keyStack.back(), std::forward<T>(value)); |
796 | 125 | m_keyStack.pop_back(); |
797 | 125 | } |
798 | 12 | else |
799 | 12 | { |
800 | 12 | m_creator->addValueElement(std::forward<T>(value)); |
801 | 12 | } |
802 | 137 | } |
803 | | |
804 | | } // namespace detail |
805 | | |
806 | | } // namespace zserio |
807 | | |
808 | | #endif // ZSERIO_JSON_READER_H_INC |