1#ifndef ZSERIO_JSON_WRITER_H_INC
2#define ZSERIO_JSON_WRITER_H_INC
16using namespace std::literals::string_view_literals;
21template <
typename ALLOC = std::allocator<u
int8_t>>
170 void writeKey(std::string_view
key);
183 bool m_isFirst =
true;
192template <
typename ALLOC>
197template <
typename ALLOC>
202template <
typename ALLOC>
208template <
typename ALLOC>
215 m_indent.has_value() ? DEFAULT_ITEM_SEPARATOR_WITH_INDENT : DEFAULT_ITEM_SEPARATOR,
allocator),
216 m_keySeparator(DEFAULT_KEY_SEPARATOR,
allocator)
219template <
typename ALLOC>
225template <
typename ALLOC>
231template <
typename ALLOC>
237template <
typename ALLOC>
243template <
typename ALLOC>
250template <
typename ALLOC>
261template <
typename ALLOC>
269template <
typename ALLOC>
283template <
typename ALLOC>
292template <
typename ALLOC>
308template <
typename ALLOC>
313 m_out.write(m_itemSeparator.data(),
static_cast<std::streamsize
>(m_itemSeparator.size()));
316 if (m_indent.has_value())
324template <
typename ALLOC>
325void BasicJsonWriter<ALLOC>::endItem()
330template <
typename ALLOC>
331void BasicJsonWriter<ALLOC>::beginObject()
339template <
typename ALLOC>
340void BasicJsonWriter<ALLOC>::endObject()
342 if (m_indent.has_value())
354template <
typename ALLOC>
355void BasicJsonWriter<ALLOC>::beginArray()
363template <
typename ALLOC>
364void BasicJsonWriter<ALLOC>::endArray()
366 if (m_indent.has_value())
378template <
typename ALLOC>
379void BasicJsonWriter<ALLOC>::writeIndent()
381 if (m_indent.has_value())
383 const auto& indent = m_indent.value();
386 for (
size_t i = 0; i < m_level; ++i)
388 m_out.write(indent.data(),
static_cast<std::streamsize
>(indent.size()));
394template <
typename ALLOC>
395void BasicJsonWriter<ALLOC>::writeKey(std::string_view key)
398 m_out.write(m_keySeparator.data(),
static_cast<std::streamsize
>(m_keySeparator.size()));
402template <
typename ALLOC>
403void BasicJsonWriter<ALLOC>::writeValue(
const IBasicReflectableDataConstPtr<ALLOC>&
reflectable)
445 if (m_enumerableFormat == EnumerableFormat::STRING)
459 if (m_enumerableFormat == EnumerableFormat::STRING)
469 throw CppRuntimeException(
"JsonWriter: Unexpected not-null value of type '")
470 <<
typeInfo.getSchemaName() <<
"'!";
476template <
typename ALLOC>
477void BasicJsonWriter<ALLOC>::writeBitBuffer(
const BasicBitBuffer<ALLOC>& bitBuffer)
481 writeKey(
"buffer"sv);
483 Span<const uint8_t> buffer = bitBuffer.getData();
484 for (uint8_t element : buffer)
493 writeKey(
"bitSize"sv);
499template <
typename ALLOC>
500void BasicJsonWriter<ALLOC>::writeBytes(Span<const uint8_t> value)
504 writeKey(
"buffer"sv);
506 for (uint8_t
byte : value)
517template <
typename ALLOC>
518void BasicJsonWriter<ALLOC>::writeStringifiedEnum(
const IBasicReflectableDataConstPtr<ALLOC>&
reflectable)
524 for (
const auto& itemInfo :
typeInfo.getEnumItems())
526 if (itemInfo.value == enumValue)
535 BasicString<RebindAlloc<ALLOC, char>> stringValue =
539 stringValue.append(
" /* no match */");
543template <
typename ALLOC>
544void BasicJsonWriter<ALLOC>::writeStringifiedBitmask(
const IBasicReflectableDataConstPtr<ALLOC>&
reflectable)
546 BasicString<RebindAlloc<ALLOC, char>> stringValue(get_allocator());
548 const uint64_t bitmaskValue =
reflectable->toUInt();
549 uint64_t valueCheck = 0;
550 for (
const auto& itemInfo :
typeInfo.getBitmaskValues())
552 if ((itemInfo.value != 0 && (bitmaskValue & itemInfo.value) == itemInfo.value) ||
553 (itemInfo.value == 0 && bitmaskValue == 0))
555 valueCheck |= itemInfo.value;
556 if (!stringValue.empty())
558 stringValue +=
" | ";
560 stringValue +=
toString(itemInfo.schemaName, get_allocator());
564 if (stringValue.empty())
567 stringValue.append(
toString(bitmaskValue, get_allocator()));
568 stringValue.append(
" /* no match */");
570 else if (bitmaskValue != valueCheck)
574 toString(bitmaskValue, get_allocator())
575 .append(
" /* partial match: ")
allocator_type get_allocator() const
void beginArray(const IBasicReflectableDataConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
void setKeySeparator(const BasicString< RebindAlloc< ALLOC, char > > &keySeparator)
BasicJsonWriter(std::ostream &out, const ALLOC &allocator=ALLOC())
static constexpr const char * DEFAULT_ITEM_SEPARATOR_WITH_INDENT
void endRoot(const IBasicReflectableDataConstPtr< ALLOC > &compound) override
BasicJsonWriter & operator=(BasicJsonWriter &&other)=delete
static constexpr const char * DEFAULT_KEY_SEPARATOR
BasicJsonWriter(BasicJsonWriter &&other)=delete
void visitValue(const IBasicReflectableDataConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
BasicJsonWriter & operator=(const BasicJsonWriter &other)=delete
void endArray(const IBasicReflectableDataConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
void endCompound(const IBasicReflectableDataConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
~BasicJsonWriter() override=default
BasicJsonWriter(const BasicJsonWriter &other)=delete
void setEnumerableFormat(EnumerableFormat enumerableFormat)
void beginCompound(const IBasicReflectableDataConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
void beginRoot(const IBasicReflectableDataConstPtr< ALLOC > &compound) override
static constexpr const char * DEFAULT_ITEM_SEPARATOR
void setItemSeparator(const BasicString< RebindAlloc< ALLOC, char > > &itemSeparator)
static constexpr EnumerableFormat DEFAULT_ENUMERABLE_FORMAT
static void encodeFloatingPoint(std::ostream &stream, double value)
static void encodeNull(std::ostream &stream)
static void encodeBool(std::ostream &stream, bool value)
static void encodeIntegral(std::ostream &stream, T value)
static void encodeString(std::ostream &stream, std::string_view value)
const IBasicTypeInfo< ALLOC > & typeInfo()
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
BasicString< RebindAlloc< ALLOC, char > > toString(T value, const ALLOC &allocator=ALLOC())
IBasicReflectableDataConstPtr< ALLOC > reflectable(const T &value, const ALLOC &allocator=ALLOC())
static bool isSigned(SchemaType schemaType)