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 AnyHolder<ALLOC> get()
const = 0;
30 template <
typename ALLOC>
31 class BitBufferAdapter :
public IObjectValueAdapter<ALLOC>,
public AllocatorHolder<ALLOC>
36 explicit BitBufferAdapter(
const ALLOC& allocator) :
40 ~BitBufferAdapter()
override =
default;
42 BitBufferAdapter(BitBufferAdapter& other) =
delete;
43 BitBufferAdapter&
operator=(BitBufferAdapter& other) =
delete;
45 BitBufferAdapter(BitBufferAdapter&& other) :
46 m_state(other.m_state),
47 m_buffer(std::move(other.m_buffer)),
48 m_bitSize(other.m_bitSize)
51 BitBufferAdapter&
operator=(BitBufferAdapter&& other)
53 m_state = other.m_state;
54 m_buffer = std::move(other.m_buffer);
55 m_bitSize = other.m_bitSize;
60 AnyHolder<ALLOC> get()
const override;
84 InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer;
85 InplaceOptionalHolder<size_t> m_bitSize;
88 template <
typename ALLOC>
89 class BytesAdapter :
public IObjectValueAdapter<ALLOC>,
public AllocatorHolder<ALLOC>
94 explicit BytesAdapter(
const ALLOC& allocator) :
98 ~BytesAdapter()
override =
default;
100 BytesAdapter(BytesAdapter& other) =
delete;
101 BytesAdapter&
operator=(BytesAdapter& other) =
delete;
103 BytesAdapter(BytesAdapter&& other) :
104 m_state(other.m_state),
105 m_buffer(std::move(other.m_buffer))
108 BytesAdapter&
operator=(BytesAdapter&& other)
110 m_state = other.m_state;
111 m_buffer = std::move(other.m_buffer);
116 AnyHolder<ALLOC> get()
const override;
139 InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer;
142 template <
typename ALLOC>
143 class CreatorAdapter :
public BasicJsonParser<ALLOC>::IObserver,
public AllocatorHolder<ALLOC>
148 explicit CreatorAdapter(
const ALLOC& allocator) :
152 void setType(
const IBasicTypeInfo<ALLOC>& typeInfo);
153 IBasicReflectablePtr<ALLOC> get()
const;
168 template <
typename T>
169 void setValue(T&& value);
171 template <
typename T>
172 void convertValue(T&& value)
const;
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;
185 template <
typename ALLOC = std::allocator<u
int8_t>>
196 m_creatorAdapter(allocator),
197 m_parser(in, m_creatorAdapter, allocator)
210 m_creatorAdapter.setType(typeInfo);
223 <<
" (JsonParser:" << m_parser.getLine() <<
":" << m_parser.getColumn() <<
")";
226 return m_creatorAdapter.get();
230 detail::CreatorAdapter<ALLOC> m_creatorAdapter;
240 template <
typename ALLOC>
243 if (m_state != VISIT_KEY || !m_buffer.hasValue() || !m_bitSize.hasValue())
251 template <
typename ALLOC>
252 void BitBufferAdapter<ALLOC>::beginObject()
257 template <
typename ALLOC>
258 void BitBufferAdapter<ALLOC>::endObject()
263 template <
typename ALLOC>
264 void BitBufferAdapter<ALLOC>::beginArray()
266 if (m_state == BEGIN_ARRAY_BUFFER)
268 m_state = VISIT_VALUE_BUFFER;
277 template <
typename ALLOC>
278 void BitBufferAdapter<ALLOC>::endArray()
280 if (m_state == VISIT_VALUE_BUFFER)
290 template <
typename ALLOC>
291 void BitBufferAdapter<ALLOC>::visitKey(
StringView key)
293 if (m_state == VISIT_KEY)
295 if (key ==
"buffer"_sv)
297 m_state = BEGIN_ARRAY_BUFFER;
299 else if (key ==
"bitSize"_sv)
301 m_state = VISIT_VALUE_BITSIZE;
314 template <
typename ALLOC>
315 void BitBufferAdapter<ALLOC>::visitValue(std::nullptr_t)
320 template <
typename ALLOC>
321 void BitBufferAdapter<ALLOC>::visitValue(
bool)
326 template <
typename ALLOC>
327 void BitBufferAdapter<ALLOC>::visitValue(int64_t)
332 template <
typename ALLOC>
333 void BitBufferAdapter<ALLOC>::visitValue(uint64_t uintValue)
335 if (m_state == VISIT_VALUE_BUFFER)
337 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
340 << uintValue <<
"'!";
343 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
345 else if (m_state == VISIT_VALUE_BITSIZE)
356 template <
typename ALLOC>
357 void BitBufferAdapter<ALLOC>::visitValue(
double)
362 template <
typename ALLOC>
363 void BitBufferAdapter<ALLOC>::visitValue(
StringView)
368 template <
typename ALLOC>
371 if (m_state != VISIT_KEY || !m_buffer.hasValue())
379 template <
typename ALLOC>
380 void BytesAdapter<ALLOC>::beginObject()
385 template <
typename ALLOC>
386 void BytesAdapter<ALLOC>::endObject()
391 template <
typename ALLOC>
392 void BytesAdapter<ALLOC>::beginArray()
394 if (m_state == BEGIN_ARRAY_BUFFER)
396 m_state = VISIT_VALUE_BUFFER;
405 template <
typename ALLOC>
406 void BytesAdapter<ALLOC>::endArray()
408 if (m_state == VISIT_VALUE_BUFFER)
418 template <
typename ALLOC>
419 void BytesAdapter<ALLOC>::visitKey(
StringView key)
421 if (m_state == VISIT_KEY)
423 if (key ==
"buffer"_sv)
425 m_state = BEGIN_ARRAY_BUFFER;
438 template <
typename ALLOC>
439 void BytesAdapter<ALLOC>::visitValue(std::nullptr_t)
444 template <
typename ALLOC>
445 void BytesAdapter<ALLOC>::visitValue(
bool)
450 template <
typename ALLOC>
451 void BytesAdapter<ALLOC>::visitValue(int64_t)
456 template <
typename ALLOC>
457 void BytesAdapter<ALLOC>::visitValue(uint64_t uintValue)
459 if (m_state == VISIT_VALUE_BUFFER)
461 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
464 << uintValue <<
"'!";
467 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
475 template <
typename ALLOC>
476 void BytesAdapter<ALLOC>::visitValue(
double)
481 template <
typename ALLOC>
482 void BytesAdapter<ALLOC>::visitValue(
StringView)
487 template <
typename ALLOC>
493 template <
typename ALLOC>
504 template <
typename ALLOC>
505 void CreatorAdapter<ALLOC>::beginObject()
507 if (m_objectValueAdapter)
509 #if defined(_MSC_VER) && (_MSC_VER >= 1920 && _MSC_VER < 1930)
514 m_objectValueAdapter->beginObject();
521 m_objectValueAdapter->beginObject();
531 if (m_keyStack.empty())
533 m_creator->beginRoot();
537 if (!m_keyStack.back().empty())
539 const CppType cppType = m_creator->getFieldType(m_keyStack.back()).getCppType();
542 m_objectValueAdapter =
543 std::allocate_shared<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator());
547 m_objectValueAdapter =
548 std::allocate_shared<BytesAdapter<ALLOC>>(get_allocator(), get_allocator());
552 m_creator->beginCompound(m_keyStack.back());
557 const CppType cppType = m_creator->getElementType().getCppType();
560 m_objectValueAdapter =
561 std::allocate_shared<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator());
565 m_objectValueAdapter =
566 std::allocate_shared<BytesAdapter<ALLOC>>(get_allocator(), get_allocator());
570 m_creator->beginCompoundElement();
577 template <
typename ALLOC>
578 void CreatorAdapter<ALLOC>::endObject()
580 if (m_objectValueAdapter)
582 setValue(m_objectValueAdapter->get());
583 m_objectValueAdapter.reset();
592 if (m_keyStack.empty())
594 m_object = m_creator->endRoot();
599 if (!m_keyStack.back().empty())
601 m_creator->endCompound();
602 m_keyStack.pop_back();
606 m_creator->endCompoundElement();
612 template <
typename ALLOC>
613 void CreatorAdapter<ALLOC>::beginArray()
615 if (m_objectValueAdapter)
617 m_objectValueAdapter->beginArray();
626 if (m_keyStack.empty())
631 m_creator->beginArray(m_keyStack.back());
633 m_keyStack.push_back(
"");
637 template <
typename ALLOC>
638 void CreatorAdapter<ALLOC>::endArray()
640 if (m_objectValueAdapter)
642 m_objectValueAdapter->endArray();
651 m_creator->endArray();
653 m_keyStack.pop_back();
654 m_keyStack.pop_back();
658 template <
typename ALLOC>
659 void CreatorAdapter<ALLOC>::visitKey(
StringView key)
661 if (m_objectValueAdapter)
663 m_objectValueAdapter->visitKey(key);
672 m_keyStack.push_back(
toString(key, get_allocator()));
676 template <
typename ALLOC>
677 void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t nullValue)
679 if (m_objectValueAdapter)
681 m_objectValueAdapter->visitValue(nullValue);
694 template <
typename ALLOC>
695 void CreatorAdapter<ALLOC>::visitValue(
bool boolValue)
697 if (m_objectValueAdapter)
699 m_objectValueAdapter->visitValue(boolValue);
712 template <
typename ALLOC>
713 void CreatorAdapter<ALLOC>::visitValue(int64_t intValue)
715 if (m_objectValueAdapter)
717 m_objectValueAdapter->visitValue(intValue);
730 template <
typename ALLOC>
731 void CreatorAdapter<ALLOC>::visitValue(uint64_t uintValue)
733 if (m_objectValueAdapter)
735 m_objectValueAdapter->visitValue(uintValue);
748 template <
typename ALLOC>
749 void CreatorAdapter<ALLOC>::visitValue(
double doubleValue)
751 if (m_objectValueAdapter)
753 m_objectValueAdapter->visitValue(doubleValue);
762 setValue(doubleValue);
766 template <
typename ALLOC>
767 void CreatorAdapter<ALLOC>::visitValue(
StringView stringValue)
769 if (m_objectValueAdapter)
771 m_objectValueAdapter->visitValue(stringValue);
780 setValue(stringValue);
784 template <
typename ALLOC>
785 template <
typename T>
786 void CreatorAdapter<ALLOC>::setValue(T&& value)
788 if (m_keyStack.empty())
793 if (!m_keyStack.back().empty())
795 m_creator->setValue(m_keyStack.back(), std::forward<T>(value));
796 m_keyStack.pop_back();
800 m_creator->addValueElement(std::forward<T>(value));
AllocatorHolder & operator=(const AllocatorHolder &other)=default
allocator_type get_allocator() const
virtual void endArray()=0
virtual void beginArray()=0
virtual void visitKey(StringView key)=0
virtual void beginObject()=0
virtual void visitValue(std::nullptr_t nullValue)=0
virtual void endObject()=0
IBasicReflectablePtr< ALLOC > read(const IBasicTypeInfo< ALLOC > &typeInfo)
BasicJsonReader(std::istream &in, const ALLOC &allocator=ALLOC())
const char * what() const noexcept override
BasicStringView< char, std::char_traits< char > > StringView
std::vector< T, RebindAlloc< ALLOC, T > > vector
size_t convertUInt64ToSize(uint64_t value)
string< ALLOC > toString(T value, const ALLOC &allocator=ALLOC())
typename IBasicReflectable< ALLOC >::Ptr IBasicReflectablePtr