1 #ifndef ZSERIO_JSON_READER_H_INC
2 #define ZSERIO_JSON_READER_H_INC
23 template <
typename ALLOC>
24 class IObjectValueAdapter :
public BasicJsonParser<ALLOC>::IObserver
27 virtual BasicAny<ALLOC>
get()
const = 0;
30 template <
typename ALLOC>
31 class 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;
91 template <
typename ALLOC>
92 class 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;
148 template <
typename ALLOC>
149 class 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;
189 using namespace std::literals::string_view_literals;
194 template <
typename ALLOC = std::allocator<u
int8_t>>
205 m_creatorAdapter(allocator),
206 m_parser(in, m_creatorAdapter, allocator)
232 <<
" (JsonParser:" << m_parser.getLine() <<
":" << m_parser.getColumn() <<
")";
235 return m_creatorAdapter.get();
239 detail::CreatorAdapter<ALLOC> m_creatorAdapter;
249 template <
typename ALLOC>
252 if (m_state != VISIT_KEY || !m_buffer.has_value() || !m_bitSize.has_value())
260 template <
typename ALLOC>
261 void BitBufferAdapter<ALLOC>::beginObject()
266 template <
typename ALLOC>
267 void BitBufferAdapter<ALLOC>::endObject()
272 template <
typename ALLOC>
273 void BitBufferAdapter<ALLOC>::beginArray()
275 if (m_state == BEGIN_ARRAY_BUFFER)
277 m_state = VISIT_VALUE_BUFFER;
286 template <
typename ALLOC>
287 void BitBufferAdapter<ALLOC>::endArray()
289 if (m_state == VISIT_VALUE_BUFFER)
299 template <
typename ALLOC>
300 void 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;
323 template <
typename ALLOC>
324 void BitBufferAdapter<ALLOC>::visitValue(std::nullptr_t)
329 template <
typename ALLOC>
330 void BitBufferAdapter<ALLOC>::visitValue(
bool)
335 template <
typename ALLOC>
336 void BitBufferAdapter<ALLOC>::visitValue(int64_t)
341 template <
typename ALLOC>
342 void BitBufferAdapter<ALLOC>::visitValue(uint64_t uintValue)
344 if (m_state == VISIT_VALUE_BUFFER)
346 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
349 << uintValue <<
"'!";
352 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
354 else if (m_state == VISIT_VALUE_BITSIZE)
365 template <
typename ALLOC>
366 void BitBufferAdapter<ALLOC>::visitValue(
double)
371 template <
typename ALLOC>
372 void BitBufferAdapter<ALLOC>::visitValue(std::string_view)
377 template <
typename ALLOC>
380 if (m_state != VISIT_KEY || !m_buffer.has_value())
388 template <
typename ALLOC>
389 void BytesAdapter<ALLOC>::beginObject()
394 template <
typename ALLOC>
395 void BytesAdapter<ALLOC>::endObject()
400 template <
typename ALLOC>
401 void BytesAdapter<ALLOC>::beginArray()
403 if (m_state == BEGIN_ARRAY_BUFFER)
405 m_state = VISIT_VALUE_BUFFER;
414 template <
typename ALLOC>
415 void BytesAdapter<ALLOC>::endArray()
417 if (m_state == VISIT_VALUE_BUFFER)
427 template <
typename ALLOC>
428 void BytesAdapter<ALLOC>::visitKey(std::string_view key)
430 if (m_state == VISIT_KEY)
432 if (key ==
"buffer"sv)
434 m_state = BEGIN_ARRAY_BUFFER;
447 template <
typename ALLOC>
448 void BytesAdapter<ALLOC>::visitValue(std::nullptr_t)
453 template <
typename ALLOC>
454 void BytesAdapter<ALLOC>::visitValue(
bool)
459 template <
typename ALLOC>
460 void BytesAdapter<ALLOC>::visitValue(int64_t)
465 template <
typename ALLOC>
466 void BytesAdapter<ALLOC>::visitValue(uint64_t uintValue)
468 if (m_state == VISIT_VALUE_BUFFER)
470 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
473 << uintValue <<
"'!";
476 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
484 template <
typename ALLOC>
485 void BytesAdapter<ALLOC>::visitValue(
double)
490 template <
typename ALLOC>
491 void BytesAdapter<ALLOC>::visitValue(std::string_view)
496 template <
typename ALLOC>
502 template <
typename ALLOC>
513 template <
typename ALLOC>
514 void 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());
551 const CppType cppType = m_creator->getElementType().getCppType();
554 m_objectValueAdapter = std::allocate_shared<BitBufferAdapter<ALLOC>>(get_allocator());
558 m_objectValueAdapter = std::allocate_shared<BytesAdapter<ALLOC>>(get_allocator());
562 m_creator->beginCompoundElement();
569 template <
typename ALLOC>
570 void 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();
604 template <
typename ALLOC>
605 void 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(
"");
629 template <
typename ALLOC>
630 void 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();
650 template <
typename ALLOC>
651 void 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()));
668 template <
typename ALLOC>
669 void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t nullValue)
671 if (m_objectValueAdapter)
673 m_objectValueAdapter->visitValue(nullValue);
686 template <
typename ALLOC>
687 void CreatorAdapter<ALLOC>::visitValue(
bool boolValue)
689 if (m_objectValueAdapter)
691 m_objectValueAdapter->visitValue(boolValue);
704 template <
typename ALLOC>
705 void CreatorAdapter<ALLOC>::visitValue(int64_t intValue)
707 if (m_objectValueAdapter)
709 m_objectValueAdapter->visitValue(intValue);
722 template <
typename ALLOC>
723 void CreatorAdapter<ALLOC>::visitValue(uint64_t uintValue)
725 if (m_objectValueAdapter)
727 m_objectValueAdapter->visitValue(uintValue);
740 template <
typename ALLOC>
741 void CreatorAdapter<ALLOC>::visitValue(
double doubleValue)
743 if (m_objectValueAdapter)
745 m_objectValueAdapter->visitValue(doubleValue);
754 setValue(doubleValue);
758 template <
typename ALLOC>
759 void CreatorAdapter<ALLOC>::visitValue(std::string_view stringValue)
761 if (m_objectValueAdapter)
763 m_objectValueAdapter->visitValue(stringValue);
772 setValue(stringValue);
776 template <
typename ALLOC>
777 template <
typename T>
778 void 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
IBasicReflectableDataPtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
BasicJsonReader(std::istream &in, const ALLOC &allocator=ALLOC())
const char * what() const noexcept override
typename IBasicReflectableData< ALLOC >::Ptr IBasicReflectableDataPtr
std::vector< T, ALLOC > Vector
decltype(auto) get(BasicVariant< ALLOC, INDEX, T... > &var)
const IBasicTypeInfo< ALLOC > & typeInfo()
BasicString< RebindAlloc< ALLOC, char > > toString(T value, const ALLOC &allocator=ALLOC())
size_t convertUInt64ToSize(uint64_t value)