1 #ifndef ZSERIO_JSON_PARSER_H_INC
2 #define ZSERIO_JSON_PARSER_H_INC
20 template <
typename ALLOC = std::allocator<u
int8_t>>
60 virtual void visitKey(std::string_view key) = 0;
113 m_tokenizer(in, allocator),
147 return m_tokenizer.getLine();
157 return m_tokenizer.getColumn();
166 void parseElements();
169 void visitValue()
const;
175 static const std::array<JsonToken, 3> ELEMENT_TOKENS;
178 IObserver& m_observer;
181 template <
typename ALLOC>
182 const std::array<JsonToken, 3> BasicJsonParser<ALLOC>::ELEMENT_TOKENS = {
185 template <
typename ALLOC>
186 void BasicJsonParser<ALLOC>::parseElement()
188 JsonToken token = m_tokenizer.getToken();
204 throw createUnexpectedTokenException(ELEMENT_TOKENS);
208 template <
typename ALLOC>
209 void BasicJsonParser<ALLOC>::parseObject()
212 m_observer.beginObject();
220 m_observer.endObject();
223 template <
typename ALLOC>
224 void BasicJsonParser<ALLOC>::parseMembers()
234 template <
typename ALLOC>
235 void BasicJsonParser<ALLOC>::parseMember()
238 const BasicAny<ALLOC>& key = m_tokenizer.getValue();
239 if (!key.template isType<
BasicString<RebindAlloc<ALLOC, char>>>())
241 throw JsonParserException(
"JsonParser:")
242 << getLine() <<
":" << getColumn() <<
": Key must be a string value!";
244 m_observer.visitKey(key.template
get<
BasicString<RebindAlloc<ALLOC, char>>>());
252 template <
typename ALLOC>
253 void BasicJsonParser<ALLOC>::parseArray()
256 m_observer.beginArray();
258 if (std::find(ELEMENT_TOKENS.begin(), ELEMENT_TOKENS.end(), m_tokenizer.getToken()) != ELEMENT_TOKENS.end())
264 m_observer.endArray();
267 template <
typename ALLOC>
268 void BasicJsonParser<ALLOC>::parseElements()
278 template <
typename ALLOC>
279 void BasicJsonParser<ALLOC>::parseValue()
285 template <
typename ALLOC>
286 void BasicJsonParser<ALLOC>::visitValue()
const
288 const BasicAny<ALLOC>& value = m_tokenizer.getValue();
290 if (value.template isType<std::nullptr_t>())
292 m_observer.visitValue(
nullptr);
294 else if (value.template isType<bool>())
296 m_observer.visitValue(value.template get<bool>());
298 else if (value.template isType<int64_t>())
300 m_observer.visitValue(value.template get<int64_t>());
302 else if (value.template isType<uint64_t>())
304 m_observer.visitValue(value.template get<uint64_t>());
306 else if (value.template isType<double>())
308 m_observer.visitValue(value.template get<double>());
312 m_observer.visitValue(value.template
get<
BasicString<RebindAlloc<ALLOC, char>>>());
316 template <
typename ALLOC>
317 void BasicJsonParser<ALLOC>::checkToken(
JsonToken token)
319 if (m_tokenizer.getToken() != token)
321 throw createUnexpectedTokenException({{token}});
325 template <
typename ALLOC>
326 void BasicJsonParser<ALLOC>::consumeToken(
JsonToken token)
332 template <
typename ALLOC>
333 JsonParserException BasicJsonParser<ALLOC>::createUnexpectedTokenException(
334 Span<const JsonToken> expecting)
const
336 JsonParserException error(
"JsonParser:");
337 error << getLine() <<
":" << getColumn() <<
": unexpected token: " << m_tokenizer.getToken();
338 if (expecting.size() == 1)
340 error <<
", expecting " << expecting[0] <<
"!";
344 error <<
", expecting one of [";
345 for (
size_t i = 0; i < expecting.size(); ++i)
351 error << expecting[i];
virtual void endArray()=0
virtual void visitValue(uint64_t uintValue)=0
virtual void visitValue(int64_t intValue)=0
virtual void beginArray()=0
virtual void beginObject()=0
virtual void visitKey(std::string_view key)=0
virtual ~IObserver()=default
virtual void visitValue(std::nullptr_t nullValue)=0
virtual void visitValue(double doubleValue)=0
virtual void endObject()=0
virtual void visitValue(bool boolValue)=0
virtual void visitValue(std::string_view stringValue)=0
BasicJsonParser(std::istream &in, IObserver &observer, const ALLOC &allocator=ALLOC())
std::basic_string< char, std::char_traits< char >, ALLOC > BasicString
decltype(auto) get(BasicVariant< ALLOC, INDEX, T... > &var)