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) :
39 ~BitBufferAdapter()
override =
default;
41 BitBufferAdapter(BitBufferAdapter& other) =
delete;
42 BitBufferAdapter&
operator=(BitBufferAdapter& other) =
delete;
44 BitBufferAdapter(BitBufferAdapter&& other) :
45 m_state(other.m_state),
46 m_buffer(std::move(other.m_buffer)),
47 m_bitSize(other.m_bitSize)
50 BitBufferAdapter&
operator=(BitBufferAdapter&& other)
52 m_state = other.m_state;
53 m_buffer = std::move(other.m_buffer);
54 m_bitSize = other.m_bitSize;
59 AnyHolder<ALLOC> get()
const override;
83 InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer;
84 InplaceOptionalHolder<size_t> m_bitSize;
87 template <
typename ALLOC>
88 class BytesAdapter :
public IObjectValueAdapter<ALLOC>,
public AllocatorHolder<ALLOC>
93 explicit BytesAdapter(
const ALLOC& allocator) :
97 ~BytesAdapter()
override =
default;
99 BytesAdapter(BytesAdapter& other) =
delete;
100 BytesAdapter&
operator=(BytesAdapter& other) =
delete;
102 BytesAdapter(BytesAdapter&& other) :
103 m_state(other.m_state),
104 m_buffer(std::move(other.m_buffer))
107 BytesAdapter&
operator=(BytesAdapter&& other)
109 m_state = other.m_state;
110 m_buffer = std::move(other.m_buffer);
115 AnyHolder<ALLOC> get()
const override;
138 InplaceOptionalHolder<vector<uint8_t, ALLOC>> m_buffer;
141 template <
typename ALLOC>
142 class CreatorAdapter :
public BasicJsonParser<ALLOC>::IObserver,
public AllocatorHolder<ALLOC>
147 explicit CreatorAdapter(
const ALLOC& allocator) :
151 void setType(
const IBasicTypeInfo<ALLOC>& typeInfo);
152 IBasicReflectablePtr<ALLOC> get()
const;
167 template <
typename T>
168 void setValue(T&& value);
170 template <
typename T>
171 void convertValue(T&& value)
const;
173 InplaceOptionalHolder<BasicZserioTreeCreator<ALLOC>> m_creator;
174 vector<string<ALLOC>, ALLOC> m_keyStack;
175 IBasicReflectablePtr<ALLOC> m_object;
176 unique_ptr<IObjectValueAdapter<ALLOC>, RebindAlloc<ALLOC, IObjectValueAdapter<ALLOC>>> m_objectValueAdapter;
184 template <
typename ALLOC = std::allocator<u
int8_t>>
195 m_creatorAdapter(allocator),
196 m_parser(in, m_creatorAdapter, allocator)
209 m_creatorAdapter.setType(typeInfo);
222 <<
" (JsonParser:" << m_parser.getLine() <<
":" << m_parser.getColumn() <<
")";
225 return m_creatorAdapter.get();
229 detail::CreatorAdapter<ALLOC> m_creatorAdapter;
239 template <
typename ALLOC>
242 if (m_state != VISIT_KEY || !m_buffer.hasValue() || !m_bitSize.hasValue())
250 template <
typename ALLOC>
251 void BitBufferAdapter<ALLOC>::beginObject()
256 template <
typename ALLOC>
257 void BitBufferAdapter<ALLOC>::endObject()
262 template <
typename ALLOC>
263 void BitBufferAdapter<ALLOC>::beginArray()
265 if (m_state == BEGIN_ARRAY_BUFFER)
267 m_state = VISIT_VALUE_BUFFER;
276 template <
typename ALLOC>
277 void BitBufferAdapter<ALLOC>::endArray()
279 if (m_state == VISIT_VALUE_BUFFER)
289 template <
typename ALLOC>
290 void BitBufferAdapter<ALLOC>::visitKey(
StringView key)
292 if (m_state == VISIT_KEY)
294 if (key ==
"buffer"_sv)
296 m_state = BEGIN_ARRAY_BUFFER;
298 else if (key ==
"bitSize"_sv)
300 m_state = VISIT_VALUE_BITSIZE;
313 template <
typename ALLOC>
314 void BitBufferAdapter<ALLOC>::visitValue(std::nullptr_t)
319 template <
typename ALLOC>
320 void BitBufferAdapter<ALLOC>::visitValue(
bool)
325 template <
typename ALLOC>
326 void BitBufferAdapter<ALLOC>::visitValue(int64_t)
331 template <
typename ALLOC>
332 void BitBufferAdapter<ALLOC>::visitValue(uint64_t uintValue)
334 if (m_state == VISIT_VALUE_BUFFER)
336 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
339 << uintValue <<
"'!";
342 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
344 else if (m_state == VISIT_VALUE_BITSIZE)
355 template <
typename ALLOC>
356 void BitBufferAdapter<ALLOC>::visitValue(
double)
361 template <
typename ALLOC>
362 void BitBufferAdapter<ALLOC>::visitValue(
StringView)
367 template <
typename ALLOC>
370 if (m_state != VISIT_KEY || !m_buffer.hasValue())
378 template <
typename ALLOC>
379 void BytesAdapter<ALLOC>::beginObject()
384 template <
typename ALLOC>
385 void BytesAdapter<ALLOC>::endObject()
390 template <
typename ALLOC>
391 void BytesAdapter<ALLOC>::beginArray()
393 if (m_state == BEGIN_ARRAY_BUFFER)
395 m_state = VISIT_VALUE_BUFFER;
404 template <
typename ALLOC>
405 void BytesAdapter<ALLOC>::endArray()
407 if (m_state == VISIT_VALUE_BUFFER)
417 template <
typename ALLOC>
418 void BytesAdapter<ALLOC>::visitKey(
StringView key)
420 if (m_state == VISIT_KEY)
422 if (key ==
"buffer"_sv)
424 m_state = BEGIN_ARRAY_BUFFER;
437 template <
typename ALLOC>
438 void BytesAdapter<ALLOC>::visitValue(std::nullptr_t)
443 template <
typename ALLOC>
444 void BytesAdapter<ALLOC>::visitValue(
bool)
449 template <
typename ALLOC>
450 void BytesAdapter<ALLOC>::visitValue(int64_t)
455 template <
typename ALLOC>
456 void BytesAdapter<ALLOC>::visitValue(uint64_t uintValue)
458 if (m_state == VISIT_VALUE_BUFFER)
460 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
463 << uintValue <<
"'!";
466 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
474 template <
typename ALLOC>
475 void BytesAdapter<ALLOC>::visitValue(
double)
480 template <
typename ALLOC>
481 void BytesAdapter<ALLOC>::visitValue(
StringView)
486 template <
typename ALLOC>
492 template <
typename ALLOC>
503 template <
typename ALLOC>
504 void CreatorAdapter<ALLOC>::beginObject()
506 if (m_objectValueAdapter)
508 m_objectValueAdapter->beginObject();
517 if (m_keyStack.empty())
519 m_creator->beginRoot();
523 if (!m_keyStack.back().empty())
525 const CppType cppType = m_creator->getFieldType(m_keyStack.back()).getCppType();
528 m_objectValueAdapter =
529 allocate_unique<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator());
533 m_objectValueAdapter =
534 allocate_unique<BytesAdapter<ALLOC>>(get_allocator(), get_allocator());
538 m_creator->beginCompound(m_keyStack.back());
543 const CppType cppType = m_creator->getElementType().getCppType();
546 m_objectValueAdapter =
547 allocate_unique<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator());
551 m_objectValueAdapter =
552 allocate_unique<BytesAdapter<ALLOC>>(get_allocator(), get_allocator());
556 m_creator->beginCompoundElement();
563 template <
typename ALLOC>
564 void CreatorAdapter<ALLOC>::endObject()
566 if (m_objectValueAdapter)
568 setValue(m_objectValueAdapter->get());
569 m_objectValueAdapter.reset();
578 if (m_keyStack.empty())
580 m_object = m_creator->endRoot();
585 if (!m_keyStack.back().empty())
587 m_creator->endCompound();
588 m_keyStack.pop_back();
592 m_creator->endCompoundElement();
598 template <
typename ALLOC>
599 void CreatorAdapter<ALLOC>::beginArray()
601 if (m_objectValueAdapter)
603 m_objectValueAdapter->beginArray();
612 if (m_keyStack.empty())
617 m_creator->beginArray(m_keyStack.back());
619 m_keyStack.push_back(
"");
623 template <
typename ALLOC>
624 void CreatorAdapter<ALLOC>::endArray()
626 if (m_objectValueAdapter)
628 m_objectValueAdapter->endArray();
637 m_creator->endArray();
639 m_keyStack.pop_back();
640 m_keyStack.pop_back();
644 template <
typename ALLOC>
645 void CreatorAdapter<ALLOC>::visitKey(
StringView key)
647 if (m_objectValueAdapter)
649 m_objectValueAdapter->visitKey(key);
658 m_keyStack.push_back(
toString(key, get_allocator()));
662 template <
typename ALLOC>
663 void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t nullValue)
665 if (m_objectValueAdapter)
667 m_objectValueAdapter->visitValue(nullValue);
680 template <
typename ALLOC>
681 void CreatorAdapter<ALLOC>::visitValue(
bool boolValue)
683 if (m_objectValueAdapter)
685 m_objectValueAdapter->visitValue(boolValue);
698 template <
typename ALLOC>
699 void CreatorAdapter<ALLOC>::visitValue(int64_t intValue)
701 if (m_objectValueAdapter)
703 m_objectValueAdapter->visitValue(intValue);
716 template <
typename ALLOC>
717 void CreatorAdapter<ALLOC>::visitValue(uint64_t uintValue)
719 if (m_objectValueAdapter)
721 m_objectValueAdapter->visitValue(uintValue);
734 template <
typename ALLOC>
735 void CreatorAdapter<ALLOC>::visitValue(
double doubleValue)
737 if (m_objectValueAdapter)
739 m_objectValueAdapter->visitValue(doubleValue);
748 setValue(doubleValue);
752 template <
typename ALLOC>
753 void CreatorAdapter<ALLOC>::visitValue(
StringView stringValue)
755 if (m_objectValueAdapter)
757 m_objectValueAdapter->visitValue(stringValue);
766 setValue(stringValue);
770 template <
typename ALLOC>
771 template <
typename T>
772 void CreatorAdapter<ALLOC>::setValue(T&& value)
774 if (m_keyStack.empty())
779 if (!m_keyStack.back().empty())
781 m_creator->setValue(m_keyStack.back(), std::forward<T>(value));
782 m_keyStack.pop_back();
786 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