1 #ifndef ZSERIO_JSON_PARSER_H_INC
2 #define ZSERIO_JSON_PARSER_H_INC
17 template <
typename ALLOC = std::allocator<u
int8_t>>
110 m_tokenizer(in, allocator),
144 return m_tokenizer.getLine();
154 return m_tokenizer.getColumn();
163 void parseElements();
166 void visitValue()
const;
172 static const std::array<JsonToken, 3> ELEMENT_TOKENS;
175 IObserver& m_observer;
178 template <
typename ALLOC>
179 const std::array<JsonToken, 3> BasicJsonParser<ALLOC>::ELEMENT_TOKENS = {
182 template <
typename ALLOC>
183 void BasicJsonParser<ALLOC>::parseElement()
185 JsonToken token = m_tokenizer.getToken();
201 throw createUnexpectedTokenException(ELEMENT_TOKENS);
205 template <
typename ALLOC>
206 void BasicJsonParser<ALLOC>::parseObject()
209 m_observer.beginObject();
217 m_observer.endObject();
220 template <
typename ALLOC>
221 void BasicJsonParser<ALLOC>::parseMembers()
231 template <
typename ALLOC>
232 void BasicJsonParser<ALLOC>::parseMember()
235 const AnyHolder<ALLOC>& key = m_tokenizer.getValue();
236 if (!key.template isType<string<ALLOC>>())
238 throw JsonParserException(
"JsonParser:")
239 << getLine() <<
":" << getColumn() <<
": Key must be a string value!";
241 m_observer.visitKey(key.template get<string<ALLOC>>());
249 template <
typename ALLOC>
250 void BasicJsonParser<ALLOC>::parseArray()
253 m_observer.beginArray();
255 if (std::find(ELEMENT_TOKENS.begin(), ELEMENT_TOKENS.end(), m_tokenizer.getToken()) != ELEMENT_TOKENS.end())
261 m_observer.endArray();
264 template <
typename ALLOC>
265 void BasicJsonParser<ALLOC>::parseElements()
275 template <
typename ALLOC>
276 void BasicJsonParser<ALLOC>::parseValue()
282 template <
typename ALLOC>
283 void BasicJsonParser<ALLOC>::visitValue()
const
285 const AnyHolder<ALLOC>& value = m_tokenizer.getValue();
287 if (value.template isType<std::nullptr_t>())
289 m_observer.visitValue(
nullptr);
291 else if (value.template isType<bool>())
293 m_observer.visitValue(value.template get<bool>());
295 else if (value.template isType<int64_t>())
297 m_observer.visitValue(value.template get<int64_t>());
299 else if (value.template isType<uint64_t>())
301 m_observer.visitValue(value.template get<uint64_t>());
303 else if (value.template isType<double>())
305 m_observer.visitValue(value.template get<double>());
309 m_observer.visitValue(value.template get<string<ALLOC>>());
313 template <
typename ALLOC>
314 void BasicJsonParser<ALLOC>::checkToken(
JsonToken token)
316 if (m_tokenizer.getToken() != token)
318 throw createUnexpectedTokenException({{token}});
322 template <
typename ALLOC>
323 void BasicJsonParser<ALLOC>::consumeToken(
JsonToken token)
329 template <
typename ALLOC>
330 JsonParserException BasicJsonParser<ALLOC>::createUnexpectedTokenException(
331 Span<const JsonToken> expecting)
const
333 JsonParserException error(
"JsonParser:");
334 error << getLine() <<
":" << getColumn() <<
": unexpected token: " << m_tokenizer.getToken();
335 if (expecting.size() == 1)
337 error <<
", expecting " << expecting[0] <<
"!";
341 error <<
", expecting one of [";
342 for (
size_t i = 0; i < expecting.size(); ++i)
348 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 visitKey(StringView key)=0
virtual void beginObject()=0
virtual ~IObserver()=default
virtual void visitValue(std::nullptr_t nullValue)=0
virtual void visitValue(double doubleValue)=0
virtual void visitValue(StringView stringValue)=0
virtual void endObject()=0
virtual void visitValue(bool boolValue)=0
BasicJsonParser(std::istream &in, IObserver &observer, const ALLOC &allocator=ALLOC())