1#ifndef ZSERIO_JSON_READER_H_INC
2#define ZSERIO_JSON_READER_H_INC
23template <
typename ALLOC>
24class IObjectValueAdapter :
public BasicJsonParser<ALLOC>::IObserver
27 virtual BasicAny<ALLOC>
get()
const = 0;
30template <
typename ALLOC>
31class BitBufferAdapter :
public IObjectValueAdapter<ALLOC>,
public AllocatorHolder<ALLOC>
39 explicit BitBufferAdapter(
const ALLOC& allocator) :
43 ~BitBufferAdapter()
override =
default;
45 BitBufferAdapter(BitBufferAdapter& other) =
delete;
46 BitBufferAdapter&
operator=(BitBufferAdapter& other) =
delete;
48 BitBufferAdapter(BitBufferAdapter&& other) :
49 m_state(other.m_state),
50 m_buffer(
std::move(other.m_buffer)),
51 m_bitSize(other.m_bitSize)
54 BitBufferAdapter&
operator=(BitBufferAdapter&& other)
56 m_state = other.m_state;
57 m_buffer = std::move(other.m_buffer);
58 m_bitSize = other.m_bitSize;
63 BasicAny<ALLOC>
get()
const override;
69 void visitKey(std::string_view key)
override;
75 void visitValue(std::string_view stringValue)
override;
87 Optional<Vector<uint8_t, ALLOC>> m_buffer;
88 Optional<size_t> m_bitSize;
91template <
typename ALLOC>
92class BytesAdapter :
public IObjectValueAdapter<ALLOC>,
public AllocatorHolder<ALLOC>
100 explicit BytesAdapter(
const ALLOC& allocator) :
104 ~BytesAdapter()
override =
default;
106 BytesAdapter(BytesAdapter& other) =
delete;
107 BytesAdapter&
operator=(BytesAdapter& other) =
delete;
109 BytesAdapter(BytesAdapter&& other) :
110 m_state(other.m_state),
111 m_buffer(
std::move(other.m_buffer))
114 BytesAdapter&
operator=(BytesAdapter&& other)
116 m_state = other.m_state;
117 m_buffer = std::move(other.m_buffer);
122 BasicAny<ALLOC>
get()
const override;
128 void visitKey(std::string_view key)
override;
134 void visitValue(std::string_view stringValue)
override;
145 Optional<Vector<uint8_t, ALLOC>> m_buffer;
148template <
typename ALLOC>
149class CreatorAdapter :
public BasicJsonParser<ALLOC>::IObserver,
public AllocatorHolder<ALLOC>
154 explicit CreatorAdapter(
const ALLOC& allocator) :
158 void setType(
const IBasicTypeInfo<ALLOC>&
typeInfo);
159 IBasicReflectableDataPtr<ALLOC>
get()
const;
165 void visitKey(std::string_view key)
override;
171 void visitValue(std::string_view stringValue)
override;
174 template <
typename T>
175 void setValue(T&& value);
177 template <
typename T>
178 void convertValue(T&& value)
const;
180 Optional<BasicZserioTreeCreator<ALLOC>> m_creator;
181 using StringType = BasicString<RebindAlloc<ALLOC, char>>;
182 Vector<StringType, RebindAlloc<ALLOC, StringType>> m_keyStack;
183 IBasicReflectableDataPtr<ALLOC> m_object;
184 std::shared_ptr<IObjectValueAdapter<ALLOC>> m_objectValueAdapter;
189using namespace std::literals::string_view_literals;
194template <
typename ALLOC = std::allocator<u
int8_t>>
232 <<
" (JsonParser:" << m_parser.getLine() <<
":" << m_parser.getColumn() <<
")";
235 return m_creatorAdapter.get();
239 detail::CreatorAdapter<ALLOC> m_creatorAdapter;
249template <
typename ALLOC>
252 if (m_state != VISIT_KEY || !m_buffer.has_value() || !m_bitSize.has_value())
260template <
typename ALLOC>
261void BitBufferAdapter<ALLOC>::beginObject()
266template <
typename ALLOC>
267void BitBufferAdapter<ALLOC>::endObject()
272template <
typename ALLOC>
273void BitBufferAdapter<ALLOC>::beginArray()
275 if (m_state == BEGIN_ARRAY_BUFFER)
277 m_state = VISIT_VALUE_BUFFER;
286template <
typename ALLOC>
287void BitBufferAdapter<ALLOC>::endArray()
289 if (m_state == VISIT_VALUE_BUFFER)
299template <
typename ALLOC>
300void BitBufferAdapter<ALLOC>::visitKey(std::string_view
key)
302 if (m_state == VISIT_KEY)
304 if (
key ==
"buffer"sv)
306 m_state = BEGIN_ARRAY_BUFFER;
308 else if (
key ==
"bitSize"sv)
310 m_state = VISIT_VALUE_BITSIZE;
323template <
typename ALLOC>
324void BitBufferAdapter<ALLOC>::visitValue(std::nullptr_t)
329template <
typename ALLOC>
330void BitBufferAdapter<ALLOC>::visitValue(
bool)
335template <
typename ALLOC>
336void BitBufferAdapter<ALLOC>::visitValue(
int64_t)
341template <
typename ALLOC>
344 if (m_state == VISIT_VALUE_BUFFER)
354 else if (m_state == VISIT_VALUE_BITSIZE)
365template <
typename ALLOC>
366void BitBufferAdapter<ALLOC>::visitValue(
double)
371template <
typename ALLOC>
372void BitBufferAdapter<ALLOC>::visitValue(std::string_view)
377template <
typename ALLOC>
380 if (m_state != VISIT_KEY || !m_buffer.has_value())
388template <
typename ALLOC>
389void BytesAdapter<ALLOC>::beginObject()
394template <
typename ALLOC>
395void BytesAdapter<ALLOC>::endObject()
400template <
typename ALLOC>
401void BytesAdapter<ALLOC>::beginArray()
403 if (m_state == BEGIN_ARRAY_BUFFER)
405 m_state = VISIT_VALUE_BUFFER;
414template <
typename ALLOC>
415void BytesAdapter<ALLOC>::endArray()
417 if (m_state == VISIT_VALUE_BUFFER)
427template <
typename ALLOC>
428void BytesAdapter<ALLOC>::visitKey(std::string_view
key)
430 if (m_state == VISIT_KEY)
432 if (
key ==
"buffer"sv)
434 m_state = BEGIN_ARRAY_BUFFER;
447template <
typename ALLOC>
448void BytesAdapter<ALLOC>::visitValue(std::nullptr_t)
453template <
typename ALLOC>
454void BytesAdapter<ALLOC>::visitValue(
bool)
459template <
typename ALLOC>
460void BytesAdapter<ALLOC>::visitValue(
int64_t)
465template <
typename ALLOC>
468 if (m_state == VISIT_VALUE_BUFFER)
484template <
typename ALLOC>
485void BytesAdapter<ALLOC>::visitValue(
double)
490template <
typename ALLOC>
491void BytesAdapter<ALLOC>::visitValue(std::string_view)
496template <
typename ALLOC>
502template <
typename ALLOC>
513template <
typename ALLOC>
514void CreatorAdapter<ALLOC>::beginObject()
516 if (m_objectValueAdapter)
518 m_objectValueAdapter->beginObject();
527 if (m_keyStack.empty())
529 m_creator->beginRoot();
533 if (!m_keyStack.back().empty())
535 const CppType cppType = m_creator->getFieldType(m_keyStack.back()).getCppType();
538 m_objectValueAdapter = std::allocate_shared<BitBufferAdapter<ALLOC>>(get_allocator());
542 m_objectValueAdapter = std::allocate_shared<BytesAdapter<ALLOC>>(get_allocator());
546 m_creator->beginCompound(m_keyStack.back());
554 m_objectValueAdapter = std::allocate_shared<BitBufferAdapter<ALLOC>>(get_allocator());
558 m_objectValueAdapter = std::allocate_shared<BytesAdapter<ALLOC>>(get_allocator());
562 m_creator->beginCompoundElement();
569template <
typename ALLOC>
570void CreatorAdapter<ALLOC>::endObject()
572 if (m_objectValueAdapter)
574 setValue(m_objectValueAdapter->get());
575 m_objectValueAdapter.reset();
584 if (m_keyStack.empty())
586 m_object = m_creator->endRoot();
591 if (!m_keyStack.back().empty())
593 m_creator->endCompound();
594 m_keyStack.pop_back();
598 m_creator->endCompoundElement();
604template <
typename ALLOC>
605void CreatorAdapter<ALLOC>::beginArray()
607 if (m_objectValueAdapter)
609 m_objectValueAdapter->beginArray();
618 if (m_keyStack.empty())
623 m_creator->beginArray(m_keyStack.back());
625 m_keyStack.push_back(
"");
629template <
typename ALLOC>
630void CreatorAdapter<ALLOC>::endArray()
632 if (m_objectValueAdapter)
634 m_objectValueAdapter->endArray();
643 m_creator->endArray();
645 m_keyStack.pop_back();
646 m_keyStack.pop_back();
650template <
typename ALLOC>
651void CreatorAdapter<ALLOC>::visitKey(std::string_view
key)
653 if (m_objectValueAdapter)
655 m_objectValueAdapter->visitKey(
key);
664 m_keyStack.push_back(
toString(
key, get_allocator()));
668template <
typename ALLOC>
669void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t
nullValue)
671 if (m_objectValueAdapter)
673 m_objectValueAdapter->visitValue(
nullValue);
686template <
typename ALLOC>
687void CreatorAdapter<ALLOC>::visitValue(
bool boolValue)
689 if (m_objectValueAdapter)
691 m_objectValueAdapter->visitValue(
boolValue);
704template <
typename ALLOC>
707 if (m_objectValueAdapter)
709 m_objectValueAdapter->visitValue(
intValue);
722template <
typename ALLOC>
725 if (m_objectValueAdapter)
727 m_objectValueAdapter->visitValue(
uintValue);
740template <
typename ALLOC>
741void CreatorAdapter<ALLOC>::visitValue(
double doubleValue)
743 if (m_objectValueAdapter)
758template <
typename ALLOC>
759void CreatorAdapter<ALLOC>::visitValue(std::string_view
stringValue)
761 if (m_objectValueAdapter)
776template <
typename ALLOC>
778void CreatorAdapter<ALLOC>::setValue(T&& value)
780 if (m_keyStack.empty())
785 if (!m_keyStack.back().empty())
787 m_creator->setValue(m_keyStack.back(), std::forward<T>(value));
788 m_keyStack.pop_back();
792 m_creator->addValueElement(std::forward<T>(value));
allocator_type get_allocator() const
AllocatorHolder & operator=(const AllocatorHolder &other)=default
virtual void endArray()=0
virtual void beginArray()=0
virtual void beginObject()=0
virtual void visitKey(std::string_view key)=0
virtual void visitValue(std::nullptr_t nullValue)=0
virtual void endObject()=0
BasicJsonReader(std::istream &in, const ALLOC &allocator=ALLOC())
IBasicReflectableDataPtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
const IBasicTypeInfo< ALLOC > & typeInfo()
decltype(auto) get(BasicVariant< ALLOC, INDEX, T... > &var)
BasicString< RebindAlloc< ALLOC, char > > toString(T value, const ALLOC &allocator=ALLOC())
size_t convertUInt64ToSize(uint64_t value)