1 #ifndef ZSERIO_JSON_READER_H_INC 2 #define ZSERIO_JSON_READER_H_INC 22 template <
typename ALLOC>
23 class IObjectValueAdapter :
public BasicJsonParser<ALLOC>::IObserver
26 virtual AnyHolder<ALLOC>
get()
const = 0;
29 template <
typename ALLOC>
30 class BitBufferAdapter :
public IObjectValueAdapter<ALLOC>,
public AllocatorHolder<ALLOC>
35 explicit BitBufferAdapter(
const ALLOC& allocator) :
36 AllocatorHolder<ALLOC>(allocator), m_state(VISIT_KEY)
38 ~BitBufferAdapter()
override =
default;
40 BitBufferAdapter(BitBufferAdapter& other) =
delete;
41 BitBufferAdapter& operator=(BitBufferAdapter& other) =
delete;
43 BitBufferAdapter(BitBufferAdapter&& other) :
44 m_state(other.m_state), m_buffer(std::move(other.m_buffer)), m_bitSize(other.m_bitSize)
47 BitBufferAdapter& operator=(BitBufferAdapter&& other)
49 m_state = other.m_state;
50 m_buffer = std::move(other.m_buffer);
51 m_bitSize = other.m_bitSize;
56 AnyHolder<ALLOC>
get()
const override;
58 void beginObject()
override;
59 void endObject()
override;
60 void beginArray()
override;
61 void endArray()
override;
63 void visitValue(std::nullptr_t)
override;
64 void visitValue(
bool boolValue)
override;
65 void visitValue(int64_t intValue)
override;
66 void visitValue(uint64_t uintValue)
override;
67 void visitValue(
double doubleValue)
override;
68 void visitValue(
StringView stringValue)
override;
80 InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer;
81 InplaceOptionalHolder<size_t> m_bitSize;
84 template <
typename ALLOC>
85 class BytesAdapter :
public IObjectValueAdapter<ALLOC>,
public AllocatorHolder<ALLOC>
90 explicit BytesAdapter(
const ALLOC& allocator) :
91 AllocatorHolder<ALLOC>(allocator), m_state(VISIT_KEY)
93 ~BytesAdapter()
override =
default;
95 BytesAdapter(BytesAdapter& other) =
delete;
96 BytesAdapter& operator=(BytesAdapter& other) =
delete;
98 BytesAdapter(BytesAdapter&& other) :
99 m_state(other.m_state), m_buffer(std::move(other.m_buffer))
102 BytesAdapter& operator=(BytesAdapter&& other)
104 m_state = other.m_state;
105 m_buffer = std::move(other.m_buffer);
110 AnyHolder<ALLOC>
get()
const override;
112 void beginObject()
override;
113 void endObject()
override;
114 void beginArray()
override;
115 void endArray()
override;
117 void visitValue(std::nullptr_t)
override;
118 void visitValue(
bool boolValue)
override;
119 void visitValue(int64_t intValue)
override;
120 void visitValue(uint64_t uintValue)
override;
121 void visitValue(
double doubleValue)
override;
122 void visitValue(
StringView stringValue)
override;
133 InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer;
136 template <
typename ALLOC>
137 class CreatorAdapter :
public BasicJsonParser<ALLOC>::IObserver,
public AllocatorHolder<ALLOC>
142 explicit CreatorAdapter(
const ALLOC& allocator) :
143 AllocatorHolder<ALLOC>(allocator)
146 void setType(
const IBasicTypeInfo<ALLOC>& typeInfo);
147 IBasicReflectablePtr<ALLOC>
get()
const;
149 void beginObject()
override;
150 void endObject()
override;
151 void beginArray()
override;
152 void endArray()
override;
154 void visitValue(std::nullptr_t)
override;
155 void visitValue(
bool boolValue)
override;
156 void visitValue(int64_t intValue)
override;
157 void visitValue(uint64_t uintValue)
override;
158 void visitValue(
double doubleValue)
override;
159 void visitValue(
StringView stringValue)
override;
162 template <
typename T>
163 void setValue(T&& value);
165 template <
typename T>
166 void convertValue(T&& value)
const;
168 InplaceOptionalHolder<BasicZserioTreeCreator<ALLOC>> m_creator;
169 vector<string<ALLOC>, ALLOC> m_keyStack;
170 IBasicReflectablePtr<ALLOC> m_object;
171 unique_ptr<IObjectValueAdapter<ALLOC>, RebindAlloc<ALLOC, IObjectValueAdapter<ALLOC>>> m_objectValueAdapter;
179 template <
typename ALLOC = std::allocator<u
int8_t>>
190 m_creatorAdapter(allocator), m_parser(in, m_creatorAdapter, allocator)
203 m_creatorAdapter.setType(typeInfo);
216 " (JsonParser:" << m_parser.getLine() <<
":" << m_parser.getColumn() <<
")";
219 return m_creatorAdapter.get();
223 detail::CreatorAdapter<ALLOC> m_creatorAdapter;
233 template <
typename ALLOC>
236 if (m_state != VISIT_KEY || !m_buffer.hasValue() || !m_bitSize.hasValue())
242 template <
typename ALLOC>
243 void BitBufferAdapter<ALLOC>::beginObject()
248 template <
typename ALLOC>
249 void BitBufferAdapter<ALLOC>::endObject()
254 template <
typename ALLOC>
255 void BitBufferAdapter<ALLOC>::beginArray()
257 if (m_state == BEGIN_ARRAY_BUFFER)
258 m_state = VISIT_VALUE_BUFFER;
263 template <
typename ALLOC>
264 void BitBufferAdapter<ALLOC>::endArray()
266 if (m_state == VISIT_VALUE_BUFFER)
272 template <
typename ALLOC>
273 void BitBufferAdapter<ALLOC>::visitKey(
StringView key)
275 if (m_state == VISIT_KEY)
277 if (key ==
"buffer"_sv)
278 m_state = BEGIN_ARRAY_BUFFER;
279 else if (key ==
"bitSize"_sv)
280 m_state = VISIT_VALUE_BITSIZE;
290 template <
typename ALLOC>
291 void BitBufferAdapter<ALLOC>::visitValue(std::nullptr_t)
296 template <
typename ALLOC>
297 void BitBufferAdapter<ALLOC>::visitValue(
bool)
302 template <
typename ALLOC>
303 void BitBufferAdapter<ALLOC>::visitValue(int64_t)
308 template <
typename ALLOC>
309 void BitBufferAdapter<ALLOC>::visitValue(uint64_t uintValue)
311 if (m_state == VISIT_VALUE_BUFFER)
313 if (uintValue > static_cast<uint64_t>(std::numeric_limits<uint8_t>::max()))
319 if (!m_buffer.hasValue())
322 m_buffer->push_back(static_cast<uint8_t>(uintValue));
324 else if (m_state == VISIT_VALUE_BITSIZE)
335 template <
typename ALLOC>
336 void BitBufferAdapter<ALLOC>::visitValue(
double)
341 template <
typename ALLOC>
342 void BitBufferAdapter<ALLOC>::visitValue(
StringView)
347 template <
typename ALLOC>
350 if (m_state != VISIT_KEY || !m_buffer.hasValue())
356 template <
typename ALLOC>
357 void BytesAdapter<ALLOC>::beginObject()
362 template <
typename ALLOC>
363 void BytesAdapter<ALLOC>::endObject()
368 template <
typename ALLOC>
369 void BytesAdapter<ALLOC>::beginArray()
371 if (m_state == BEGIN_ARRAY_BUFFER)
372 m_state = VISIT_VALUE_BUFFER;
377 template <
typename ALLOC>
378 void BytesAdapter<ALLOC>::endArray()
380 if (m_state == VISIT_VALUE_BUFFER)
386 template <
typename ALLOC>
387 void BytesAdapter<ALLOC>::visitKey(
StringView key)
389 if (m_state == VISIT_KEY)
391 if (key ==
"buffer"_sv)
392 m_state = BEGIN_ARRAY_BUFFER;
402 template <
typename ALLOC>
403 void BytesAdapter<ALLOC>::visitValue(std::nullptr_t)
408 template <
typename ALLOC>
409 void BytesAdapter<ALLOC>::visitValue(
bool)
414 template <
typename ALLOC>
415 void BytesAdapter<ALLOC>::visitValue(int64_t)
420 template <
typename ALLOC>
421 void BytesAdapter<ALLOC>::visitValue(uint64_t uintValue)
423 if (m_state == VISIT_VALUE_BUFFER)
425 if (uintValue > static_cast<uint64_t>(std::numeric_limits<uint8_t>::max()))
431 if (!m_buffer.hasValue())
434 m_buffer->push_back(static_cast<uint8_t>(uintValue));
442 template <
typename ALLOC>
443 void BytesAdapter<ALLOC>::visitValue(
double)
448 template <
typename ALLOC>
449 void BytesAdapter<ALLOC>::visitValue(
StringView)
454 template <
typename ALLOC>
460 template <
typename ALLOC>
469 template <
typename ALLOC>
470 void CreatorAdapter<ALLOC>::beginObject()
472 if (m_objectValueAdapter)
474 m_objectValueAdapter->beginObject();
481 if (m_keyStack.empty())
483 m_creator->beginRoot();
487 if (!m_keyStack.back().empty())
489 const CppType cppType = m_creator->getFieldType(m_keyStack.back()).getCppType();
492 m_objectValueAdapter = allocate_unique<BitBufferAdapter<ALLOC>>(get_allocator(),
497 m_objectValueAdapter = allocate_unique<BytesAdapter<ALLOC>>(get_allocator(),
502 m_creator->beginCompound(m_keyStack.back());
507 const CppType cppType = m_creator->getElementType().getCppType();
510 m_objectValueAdapter = allocate_unique<BitBufferAdapter<ALLOC>>(get_allocator(),
515 m_objectValueAdapter = allocate_unique<BytesAdapter<ALLOC>>(get_allocator(),
520 m_creator->beginCompoundElement();
527 template <
typename ALLOC>
528 void CreatorAdapter<ALLOC>::endObject()
530 if (m_objectValueAdapter)
532 setValue(m_objectValueAdapter->get());
533 m_objectValueAdapter.reset();
540 if (m_keyStack.empty())
542 m_object = m_creator->endRoot();
547 if (!m_keyStack.back().empty())
549 m_creator->endCompound();
550 m_keyStack.pop_back();
554 m_creator->endCompoundElement();
560 template <
typename ALLOC>
561 void CreatorAdapter<ALLOC>::beginArray()
563 if (m_objectValueAdapter)
565 m_objectValueAdapter->beginArray();
572 if (m_keyStack.empty())
575 m_creator->beginArray(m_keyStack.back());
577 m_keyStack.push_back(
"");
581 template <
typename ALLOC>
582 void CreatorAdapter<ALLOC>::endArray()
584 if (m_objectValueAdapter)
586 m_objectValueAdapter->endArray();
593 m_creator->endArray();
595 m_keyStack.pop_back();
596 m_keyStack.pop_back();
600 template <
typename ALLOC>
601 void CreatorAdapter<ALLOC>::visitKey(
StringView key)
603 if (m_objectValueAdapter)
605 m_objectValueAdapter->visitKey(key);
612 m_keyStack.push_back(
toString(key, get_allocator()));
616 template <
typename ALLOC>
617 void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t nullValue)
619 if (m_objectValueAdapter)
621 m_objectValueAdapter->visitValue(nullValue);
632 template <
typename ALLOC>
633 void CreatorAdapter<ALLOC>::visitValue(
bool boolValue)
635 if (m_objectValueAdapter)
637 m_objectValueAdapter->visitValue(boolValue);
648 template <
typename ALLOC>
649 void CreatorAdapter<ALLOC>::visitValue(int64_t intValue)
651 if (m_objectValueAdapter)
653 m_objectValueAdapter->visitValue(intValue);
664 template <
typename ALLOC>
665 void CreatorAdapter<ALLOC>::visitValue(uint64_t uintValue)
667 if (m_objectValueAdapter)
669 m_objectValueAdapter->visitValue(uintValue);
680 template <
typename ALLOC>
681 void CreatorAdapter<ALLOC>::visitValue(
double doubleValue)
683 if (m_objectValueAdapter)
685 m_objectValueAdapter->visitValue(doubleValue);
692 setValue(doubleValue);
696 template <
typename ALLOC>
697 void CreatorAdapter<ALLOC>::visitValue(
StringView stringValue)
699 if (m_objectValueAdapter)
701 m_objectValueAdapter->visitValue(stringValue);
708 setValue(stringValue);
712 template <
typename ALLOC>
713 template <
typename T>
714 void CreatorAdapter<ALLOC>::setValue(T&& value)
716 if (m_keyStack.empty())
719 if (!m_keyStack.back().empty())
721 m_creator->setValue(m_keyStack.back(), std::forward<T>(value));
722 m_keyStack.pop_back();
726 m_creator->addValueElement(std::forward<T>(value));
734 #endif // ZSERIO_JSON_READER_H_INC
typename IBasicReflectable< ALLOC >::Ptr IBasicReflectablePtr
IBasicReflectablePtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
string< ALLOC > toString(T value, const ALLOC &allocator=ALLOC())
const char * what() const noexceptoverride
size_t convertUInt64ToSize(uint64_t value)
BasicJsonReader(std::istream &in, const ALLOC &allocator=ALLOC())
allocator_type get_allocator() const
std::vector< T, RebindAlloc< ALLOC, T >> vector
BasicStringView< char, std::char_traits< char >> StringView