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 unique_ptr<IObjectValueAdapter<ALLOC>, RebindAlloc<ALLOC, 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 m_objectValueAdapter->beginObject();
518 if (m_keyStack.empty())
520 m_creator->beginRoot();
524 if (!m_keyStack.back().empty())
526 const CppType cppType = m_creator->getFieldType(m_keyStack.back()).getCppType();
529 m_objectValueAdapter =
530 allocate_unique<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator());
534 m_objectValueAdapter =
535 allocate_unique<BytesAdapter<ALLOC>>(get_allocator(), get_allocator());
539 m_creator->beginCompound(m_keyStack.back());
544 const CppType cppType = m_creator->getElementType().getCppType();
547 m_objectValueAdapter =
548 allocate_unique<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator());
552 m_objectValueAdapter =
553 allocate_unique<BytesAdapter<ALLOC>>(get_allocator(), get_allocator());
557 m_creator->beginCompoundElement();
564 template <
typename ALLOC>
565 void CreatorAdapter<ALLOC>::endObject()
567 if (m_objectValueAdapter)
569 setValue(m_objectValueAdapter->get());
570 m_objectValueAdapter.reset();
579 if (m_keyStack.empty())
581 m_object = m_creator->endRoot();
586 if (!m_keyStack.back().empty())
588 m_creator->endCompound();
589 m_keyStack.pop_back();
593 m_creator->endCompoundElement();
599 template <
typename ALLOC>
600 void CreatorAdapter<ALLOC>::beginArray()
602 if (m_objectValueAdapter)
604 m_objectValueAdapter->beginArray();
613 if (m_keyStack.empty())
618 m_creator->beginArray(m_keyStack.back());
620 m_keyStack.push_back(
"");
624 template <
typename ALLOC>
625 void CreatorAdapter<ALLOC>::endArray()
627 if (m_objectValueAdapter)
629 m_objectValueAdapter->endArray();
638 m_creator->endArray();
640 m_keyStack.pop_back();
641 m_keyStack.pop_back();
645 template <
typename ALLOC>
646 void CreatorAdapter<ALLOC>::visitKey(
StringView key)
648 if (m_objectValueAdapter)
650 m_objectValueAdapter->visitKey(key);
659 m_keyStack.push_back(
toString(key, get_allocator()));
663 template <
typename ALLOC>
664 void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t nullValue)
666 if (m_objectValueAdapter)
668 m_objectValueAdapter->visitValue(nullValue);
681 template <
typename ALLOC>
682 void CreatorAdapter<ALLOC>::visitValue(
bool boolValue)
684 if (m_objectValueAdapter)
686 m_objectValueAdapter->visitValue(boolValue);
699 template <
typename ALLOC>
700 void CreatorAdapter<ALLOC>::visitValue(int64_t intValue)
702 if (m_objectValueAdapter)
704 m_objectValueAdapter->visitValue(intValue);
717 template <
typename ALLOC>
718 void CreatorAdapter<ALLOC>::visitValue(uint64_t uintValue)
720 if (m_objectValueAdapter)
722 m_objectValueAdapter->visitValue(uintValue);
735 template <
typename ALLOC>
736 void CreatorAdapter<ALLOC>::visitValue(
double doubleValue)
738 if (m_objectValueAdapter)
740 m_objectValueAdapter->visitValue(doubleValue);
749 setValue(doubleValue);
753 template <
typename ALLOC>
754 void CreatorAdapter<ALLOC>::visitValue(
StringView stringValue)
756 if (m_objectValueAdapter)
758 m_objectValueAdapter->visitValue(stringValue);
767 setValue(stringValue);
771 template <
typename ALLOC>
772 template <
typename T>
773 void CreatorAdapter<ALLOC>::setValue(T&& value)
775 if (m_keyStack.empty())
780 if (!m_keyStack.back().empty())
782 m_creator->setValue(m_keyStack.back(), std::forward<T>(value));
783 m_keyStack.pop_back();
787 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