8 #ifndef INCLUDED_ORCUS_JSON_PARSER_HPP 9 #define INCLUDED_ORCUS_JSON_PARSER_HPP 11 #include "orcus/json_parser_base.hpp" 22 template<
typename _Handler>
26 typedef _Handler handler_type;
36 json_parser(
const char* p,
size_t n, handler_type& hdl);
49 void number_with_exp(
double base);
53 handler_type& m_handler;
56 template<
typename _Handler>
58 const char* p,
size_t n, handler_type& hdl) :
61 template<
typename _Handler>
64 m_handler.begin_parse();
73 m_handler.end_parse();
76 template<
typename _Handler>
90 json::parse_error::throw_with(
91 "root_value: either '[' or '{' was expected, but '", cur_char(),
"' was found.",
offset());
95 template<
typename _Handler>
118 m_handler.boolean_true();
122 m_handler.boolean_false();
132 json::parse_error::throw_with(
"value: failed to parse '", cur_char(),
"'.",
offset());
136 template<
typename _Handler>
139 assert(cur_char() ==
'[');
141 m_handler.begin_array();
142 for (next(); has_char(); next())
144 if (cur_char() ==
']')
146 m_handler.end_array();
161 m_handler.end_array();
168 json::parse_error::throw_with(
169 "array: either ']' or ',' expected, but '", cur_char(),
"' found.",
offset());
177 template<
typename _Handler>
180 assert(cur_char() ==
'{');
182 m_handler.begin_object();
183 for (next(); has_char(); next())
192 m_handler.end_object();
199 json::parse_error::throw_with(
200 "object: '\"' was expected, but '", cur_char(),
"' found.",
offset());
209 case parse_quoted_string_state::error_no_closing_quote:
210 throw json::parse_error(
"object: stream ended prematurely before reaching the closing quote of a key.",
offset());
211 case parse_quoted_string_state::error_illegal_escape_char:
212 json::parse_error::throw_with(
213 "object: illegal escape character '", cur_char(),
"' in key value.",
offset());
219 m_handler.object_key(res.str, res.length, res.
transient);
222 if (cur_char() !=
':')
223 json::parse_error::throw_with(
224 "object: ':' was expected, but '", cur_char(),
"' found.",
offset());
241 m_handler.end_object();
248 json::parse_error::throw_with(
249 "object: either ']' or ',' expected, but '", cur_char(),
"' found.",
offset());
256 template<
typename _Handler>
259 assert(is_numeric(cur_char()) || cur_char() ==
'-');
261 double val = parse_double_or_throw();
266 number_with_exp(val);
271 m_handler.number(val);
275 template<
typename _Handler>
278 assert(cur_char() ==
'e' || cur_char() ==
'E');
283 long exp = parse_long_or_throw();
284 base *= std::pow(10.0, exp);
285 m_handler.number(base);
289 template<
typename _Handler>
295 m_handler.string(res.str, res.length, res.
transient);
302 case parse_quoted_string_state::error_no_closing_quote:
304 case parse_quoted_string_state::error_illegal_escape_char:
305 json::parse_error::throw_with(
"string: illegal escape character '", cur_char(),
"'.",
offset());
void parse()
Definition: json_parser.hpp:62
bool transient
Definition: parser_global.hpp:50
json_parser(const char *p, size_t n, handler_type &hdl)
Definition: json_parser.hpp:57
Definition: json_parser_base.hpp:30
Definition: json_parser_base.hpp:18
Definition: json_parser.hpp:23
Definition: parser_base.hpp:34
Definition: parser_global.hpp:32
Definition: base64.hpp:15
std::ptrdiff_t offset() const