10 #include <nlohmann/detail/exceptions.hpp>
11 #include <nlohmann/detail/input/input_adapters.hpp>
12 #include <nlohmann/detail/input/json_sax.hpp>
13 #include <nlohmann/detail/input/lexer.hpp>
14 #include <nlohmann/detail/macro_scope.hpp>
15 #include <nlohmann/detail/meta/is_sax.hpp>
16 #include <nlohmann/detail/value_t.hpp>
42 template<
typename BasicJsonType>
43 using parser_callback_t =
51 template<
typename BasicJsonType,
typename InputAdapterType>
54 using number_integer_t =
typename BasicJsonType::number_integer_t;
55 using number_unsigned_t =
typename BasicJsonType::number_unsigned_t;
56 using number_float_t =
typename BasicJsonType::number_float_t;
57 using string_t =
typename BasicJsonType::string_t;
59 using token_type =
typename lexer_t::token_type;
63 explicit parser(InputAdapterType&& adapter,
64 const parser_callback_t<BasicJsonType> cb =
nullptr,
65 const bool allow_exceptions_ =
true,
66 const bool skip_comments =
false)
68 , m_lexer(std::move(adapter), skip_comments)
69 , allow_exceptions(allow_exceptions_)
85 void parse(
const bool strict, BasicJsonType& result)
90 sax_parse_internal(&sdp);
93 if (
strict && (get_token() != token_type::end_of_input))
98 exception_message(token_type::end_of_input,
"value"), BasicJsonType()));
102 if (sdp.is_errored())
110 if (result.is_discarded())
118 sax_parse_internal(&sdp);
121 if (
strict && (get_token() != token_type::end_of_input))
129 if (sdp.is_errored())
136 result.assert_invariant();
148 return sax_parse(&sax_acceptor,
strict);
151 template<
typename SAX>
152 JSON_HEDLEY_NON_NULL(2)
153 bool sax_parse(SAX* sax, const
bool strict = true)
156 const bool result = sax_parse_internal(sax);
159 if (result && strict && (get_token() != token_type::end_of_input))
170 template<
typename SAX>
171 JSON_HEDLEY_NON_NULL(2)
172 bool sax_parse_internal(SAX* sax)
176 std::vector<bool> states;
178 bool skip_to_state_evaluation =
false;
182 if (!skip_to_state_evaluation)
187 case token_type::begin_object:
189 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
195 if (get_token() == token_type::end_object)
197 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
205 if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
211 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.
get_string())))
217 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
225 states.push_back(
false);
232 case token_type::begin_array:
234 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
240 if (get_token() == token_type::end_array)
242 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
250 states.push_back(
true);
256 case token_type::value_float:
260 if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
264 out_of_range::create(406,
"number overflow parsing '" + m_lexer.
get_token_string() +
"'", BasicJsonType()));
267 if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.
get_string())))
275 case token_type::literal_false:
277 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(
false)))
284 case token_type::literal_null:
286 if (JSON_HEDLEY_UNLIKELY(!sax->null()))
293 case token_type::literal_true:
295 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(
true)))
302 case token_type::value_integer:
311 case token_type::value_string:
313 if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.
get_string())))
320 case token_type::value_unsigned:
329 case token_type::parse_error:
337 case token_type::uninitialized:
338 case token_type::end_array:
339 case token_type::end_object:
340 case token_type::name_separator:
341 case token_type::value_separator:
342 case token_type::end_of_input:
343 case token_type::literal_or_value:
354 skip_to_state_evaluation =
false;
367 if (get_token() == token_type::value_separator)
375 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
377 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
386 JSON_ASSERT(!states.empty());
388 skip_to_state_evaluation =
true;
400 if (get_token() == token_type::value_separator)
403 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
410 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.
get_string())))
416 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
429 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
431 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
440 JSON_ASSERT(!states.empty());
442 skip_to_state_evaluation =
true;
453 token_type get_token()
455 return last_token = m_lexer.scan();
458 std::string exception_message(
const token_type expected,
const std::string& context)
460 std::string error_msg =
"syntax error ";
462 if (!context.empty())
464 error_msg +=
"while parsing " + context +
" ";
469 if (last_token == token_type::parse_error)
479 if (expected != token_type::uninitialized)
489 const parser_callback_t<BasicJsonType> callback =
nullptr;
491 token_type last_token = token_type::uninitialized;
495 const bool allow_exceptions =
true;
Definition: json_sax.hpp:636
Definition: json_sax.hpp:329
SAX implementation to create a JSON value from SAX events.
Definition: json_sax.hpp:155
JSON_HEDLEY_RETURNS_NON_NULL static JSON_HEDLEY_CONST const char * token_type_name(const token_type t) noexcept
return name of values of type token_type (only used for errors)
Definition: lexer.hpp:54
lexical analysis
Definition: lexer.hpp:104
string_t & get_string()
return current string value (implicitly resets the token; useful only once)
Definition: lexer.hpp:1422
constexpr position_t get_position() const noexcept
return position of last read token
Definition: lexer.hpp:1432
constexpr number_integer_t get_number_integer() const noexcept
return integer value
Definition: lexer.hpp:1404
constexpr JSON_HEDLEY_RETURNS_NON_NULL const char * get_error_message() const noexcept
return syntax error message
Definition: lexer.hpp:1465
constexpr number_unsigned_t get_number_unsigned() const noexcept
return unsigned integer value
Definition: lexer.hpp:1410
constexpr number_float_t get_number_float() const noexcept
return floating-point value
Definition: lexer.hpp:1416
std::string get_token_string() const
return the last read token (for errors only).
Definition: lexer.hpp:1440
static parse_error create(int id_, const position_t &pos, const std::string &what_arg, const BasicJsonType &context)
create a parse error exception
Definition: exceptions.hpp:197
syntax analysis
Definition: parser.hpp:53
parser(InputAdapterType &&adapter, const parser_callback_t< BasicJsonType > cb=nullptr, const bool allow_exceptions_=true, const bool skip_comments=false)
a parser reading from an input adapter
Definition: parser.hpp:63
bool accept(const bool strict=true)
public accept interface
Definition: parser.hpp:145
void parse(const bool strict, BasicJsonType &result)
public parser interface
Definition: parser.hpp:85
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
@ discarded
discarded by the parser callback function
parse_event_t
Definition: parser.hpp:27
@ value
the parser finished reading a JSON value
@ key
the parser read a key of a value in an object
@ array_end
the parser read ] and finished processing a JSON array
@ array_start
the parser read [ and started to process a JSON array
@ object_start
the parser read { and started to process a JSON object
@ object_end
the parser read } and finished processing a JSON object
@ strict
throw a type_error exception in case of invalid UTF-8
namespace for Niels Lohmann
Definition: adl_serializer.hpp:12
Definition: is_sax.hpp:97