1 #ifndef ZSERIO_JSON_TOKENIZER_H_INC
2 #define ZSERIO_JSON_TOKENIZER_H_INC
56 template <
typename ALLOC = std::allocator<u
int8_t>>
72 m_decoderResult(0, allocator),
73 m_content(readContent(allocator)),
127 return m_tokenColumnNumber;
134 bool skipWhitespaces();
136 template <
typename T>
137 void setToken(
JsonToken token, T&& value);
140 void setPosition(
size_t newPos,
size_t newColumnNumber);
141 void setTokenValue();
143 static constexpr
size_t BUFFER_SIZE = 64 * 1024;
144 std::array<char, BUFFER_SIZE> m_buffer;
150 size_t m_lineNumber = 1;
151 size_t m_columnNumber = 1;
152 size_t m_tokenColumnNumber = 1;
158 template <
typename ALLOC>
161 while (!decodeNext())
164 if (newContent.empty())
168 m_tokenColumnNumber = m_columnNumber;
179 m_content = m_content.substr(m_pos) + newContent;
186 template <
typename ALLOC>
189 const size_t count =
static_cast<size_t>(m_in.rdbuf()->sgetn(m_buffer.data(), BUFFER_SIZE));
194 template <
typename ALLOC>
195 bool BasicJsonTokenizer<ALLOC>::decodeNext()
197 if (!skipWhitespaces())
202 m_tokenColumnNumber = m_columnNumber;
204 const char nextChar = m_content[m_pos];
209 setPosition(m_pos + 1, m_columnNumber + 1);
213 setPosition(m_pos + 1, m_columnNumber + 1);
217 setPosition(m_pos + 1, m_columnNumber + 1);
221 setPosition(m_pos + 1, m_columnNumber + 1);
225 setPosition(m_pos + 1, m_columnNumber + 1);
229 setPosition(m_pos + 1, m_columnNumber + 1);
232 m_decoderResult = m_decoder.decodeValue(std::string_view(m_content.data()).substr(m_pos));
233 if (m_pos + m_decoderResult.numReadChars >= m_content.size())
245 template <
typename ALLOC>
246 bool BasicJsonTokenizer<ALLOC>::skipWhitespaces()
250 if (m_pos >= m_content.size())
256 const char nextChar = m_content[m_pos];
261 setPosition(m_pos + 1, m_columnNumber + 1);
265 setPosition(m_pos + 1, 1);
268 if (m_pos + 1 >= m_content.size())
274 setPosition(m_pos + (m_content[m_pos + 1] ==
'\n' ? 2 : 1), 1);
282 template <
typename ALLOC>
283 template <
typename T>
284 void BasicJsonTokenizer<ALLOC>::setToken(
JsonToken token, T&& value)
287 m_value.set(std::forward<T>(value));
290 template <
typename ALLOC>
291 void BasicJsonTokenizer<ALLOC>::setToken(
JsonToken token, BasicAny<ALLOC>&& value)
294 m_value = std::move(value);
297 template <
typename ALLOC>
298 void BasicJsonTokenizer<ALLOC>::setToken(
JsonToken token)
304 template <
typename ALLOC>
305 void BasicJsonTokenizer<ALLOC>::setPosition(
size_t newPos,
size_t newColumnNumber)
308 m_columnNumber = newColumnNumber;
311 template <
typename ALLOC>
312 void BasicJsonTokenizer<ALLOC>::setTokenValue()
314 if (!m_decoderResult.value.hasValue())
316 throw JsonParserException(
"JsonTokenizer:")
317 << m_lineNumber <<
":" << m_tokenColumnNumber <<
": "
318 << (m_decoderResult.integerOverflow ?
"Value is outside of the 64-bit integer range!"
323 setPosition(m_pos + m_decoderResult.numReadChars, m_columnNumber + m_decoderResult.numReadChars);
const BasicAny< ALLOC > & getValue() const
JsonToken getToken() const
BasicJsonTokenizer(std::istream &in, const ALLOC &allocator)
CppRuntimeException(const char *message="")
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
CppRuntimeException & operator<<(CppRuntimeException &exception, const BasicBitBuffer< ALLOC > &bitBuffer)