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())
248 template <
typename ALLOC>
249 void BitBufferAdapter<ALLOC>::beginObject()
254 template <
typename ALLOC>
255 void BitBufferAdapter<ALLOC>::endObject()
260 template <
typename ALLOC>
261 void BitBufferAdapter<ALLOC>::beginArray()
263 if (m_state == BEGIN_ARRAY_BUFFER)
265 m_state = VISIT_VALUE_BUFFER;
274 template <
typename ALLOC>
275 void BitBufferAdapter<ALLOC>::endArray()
277 if (m_state == VISIT_VALUE_BUFFER)
287 template <
typename ALLOC>
288 void BitBufferAdapter<ALLOC>::visitKey(
StringView key)
290 if (m_state == VISIT_KEY)
292 if (key ==
"buffer"_sv)
293 m_state = BEGIN_ARRAY_BUFFER;
294 else if (key ==
"bitSize"_sv)
295 m_state = VISIT_VALUE_BITSIZE;
305 template <
typename ALLOC>
306 void BitBufferAdapter<ALLOC>::visitValue(std::nullptr_t)
311 template <
typename ALLOC>
312 void BitBufferAdapter<ALLOC>::visitValue(
bool)
317 template <
typename ALLOC>
318 void BitBufferAdapter<ALLOC>::visitValue(int64_t)
323 template <
typename ALLOC>
324 void BitBufferAdapter<ALLOC>::visitValue(uint64_t uintValue)
326 if (m_state == VISIT_VALUE_BUFFER)
328 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
331 << uintValue <<
"'!";
334 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
336 else if (m_state == VISIT_VALUE_BITSIZE)
347 template <
typename ALLOC>
348 void BitBufferAdapter<ALLOC>::visitValue(
double)
353 template <
typename ALLOC>
354 void BitBufferAdapter<ALLOC>::visitValue(
StringView)
359 template <
typename ALLOC>
362 if (m_state != VISIT_KEY || !m_buffer.hasValue())
368 template <
typename ALLOC>
369 void BytesAdapter<ALLOC>::beginObject()
374 template <
typename ALLOC>
375 void BytesAdapter<ALLOC>::endObject()
380 template <
typename ALLOC>
381 void BytesAdapter<ALLOC>::beginArray()
383 if (m_state == BEGIN_ARRAY_BUFFER)
385 m_state = VISIT_VALUE_BUFFER;
394 template <
typename ALLOC>
395 void BytesAdapter<ALLOC>::endArray()
397 if (m_state == VISIT_VALUE_BUFFER)
407 template <
typename ALLOC>
408 void BytesAdapter<ALLOC>::visitKey(
StringView key)
410 if (m_state == VISIT_KEY)
412 if (key ==
"buffer"_sv)
413 m_state = BEGIN_ARRAY_BUFFER;
423 template <
typename ALLOC>
424 void BytesAdapter<ALLOC>::visitValue(std::nullptr_t)
429 template <
typename ALLOC>
430 void BytesAdapter<ALLOC>::visitValue(
bool)
435 template <
typename ALLOC>
436 void BytesAdapter<ALLOC>::visitValue(int64_t)
441 template <
typename ALLOC>
442 void BytesAdapter<ALLOC>::visitValue(uint64_t uintValue)
444 if (m_state == VISIT_VALUE_BUFFER)
446 if (uintValue >
static_cast<uint64_t
>(std::numeric_limits<uint8_t>::max()))
449 << uintValue <<
"'!";
452 m_buffer->push_back(
static_cast<uint8_t
>(uintValue));
460 template <
typename ALLOC>
461 void BytesAdapter<ALLOC>::visitValue(
double)
466 template <
typename ALLOC>
467 void BytesAdapter<ALLOC>::visitValue(
StringView)
472 template <
typename ALLOC>
478 template <
typename ALLOC>
487 template <
typename ALLOC>
488 void CreatorAdapter<ALLOC>::beginObject()
490 if (m_objectValueAdapter)
492 m_objectValueAdapter->beginObject();
499 if (m_keyStack.empty())
501 m_creator->beginRoot();
505 if (!m_keyStack.back().empty())
507 const CppType cppType = m_creator->getFieldType(m_keyStack.back()).getCppType();
510 m_objectValueAdapter =
511 allocate_unique<BitBufferAdapter<ALLOC>>(get_allocator(), get_allocator());
515 m_objectValueAdapter =
516 allocate_unique<BytesAdapter<ALLOC>>(get_allocator(), get_allocator());
520 m_creator->beginCompound(m_keyStack.back());
525 const CppType cppType = m_creator->getElementType().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->beginCompoundElement();
545 template <
typename ALLOC>
546 void CreatorAdapter<ALLOC>::endObject()
548 if (m_objectValueAdapter)
550 setValue(m_objectValueAdapter->get());
551 m_objectValueAdapter.reset();
558 if (m_keyStack.empty())
560 m_object = m_creator->endRoot();
565 if (!m_keyStack.back().empty())
567 m_creator->endCompound();
568 m_keyStack.pop_back();
572 m_creator->endCompoundElement();
578 template <
typename ALLOC>
579 void CreatorAdapter<ALLOC>::beginArray()
581 if (m_objectValueAdapter)
583 m_objectValueAdapter->beginArray();
590 if (m_keyStack.empty())
593 m_creator->beginArray(m_keyStack.back());
595 m_keyStack.push_back(
"");
599 template <
typename ALLOC>
600 void CreatorAdapter<ALLOC>::endArray()
602 if (m_objectValueAdapter)
604 m_objectValueAdapter->endArray();
611 m_creator->endArray();
613 m_keyStack.pop_back();
614 m_keyStack.pop_back();
618 template <
typename ALLOC>
619 void CreatorAdapter<ALLOC>::visitKey(
StringView key)
621 if (m_objectValueAdapter)
623 m_objectValueAdapter->visitKey(key);
630 m_keyStack.push_back(
toString(key, get_allocator()));
634 template <
typename ALLOC>
635 void CreatorAdapter<ALLOC>::visitValue(std::nullptr_t nullValue)
637 if (m_objectValueAdapter)
639 m_objectValueAdapter->visitValue(nullValue);
650 template <
typename ALLOC>
651 void CreatorAdapter<ALLOC>::visitValue(
bool boolValue)
653 if (m_objectValueAdapter)
655 m_objectValueAdapter->visitValue(boolValue);
666 template <
typename ALLOC>
667 void CreatorAdapter<ALLOC>::visitValue(int64_t intValue)
669 if (m_objectValueAdapter)
671 m_objectValueAdapter->visitValue(intValue);
682 template <
typename ALLOC>
683 void CreatorAdapter<ALLOC>::visitValue(uint64_t uintValue)
685 if (m_objectValueAdapter)
687 m_objectValueAdapter->visitValue(uintValue);
698 template <
typename ALLOC>
699 void CreatorAdapter<ALLOC>::visitValue(
double doubleValue)
701 if (m_objectValueAdapter)
703 m_objectValueAdapter->visitValue(doubleValue);
710 setValue(doubleValue);
714 template <
typename ALLOC>
715 void CreatorAdapter<ALLOC>::visitValue(
StringView stringValue)
717 if (m_objectValueAdapter)
719 m_objectValueAdapter->visitValue(stringValue);
726 setValue(stringValue);
730 template <
typename ALLOC>
731 template <
typename T>
732 void CreatorAdapter<ALLOC>::setValue(T&& value)
734 if (m_keyStack.empty())
737 if (!m_keyStack.back().empty())
739 m_creator->setValue(m_keyStack.back(), std::forward<T>(value));
740 m_keyStack.pop_back();
744 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