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());
207 if (res.length == parse_quoted_string_state::error_no_closing_quote)
208 throw json::parse_error(
"object: stream ended prematurely before reaching the closing quote of a key.",
offset());
209 else if (res.length == parse_quoted_string_state::error_illegal_escape_char)
210 json::parse_error::throw_with(
211 "object: illegal escape character '", cur_char(),
"' in key value.",
offset());
216 m_handler.object_key(res.str, res.length, res.
transient);
219 if (cur_char() !=
':')
220 json::parse_error::throw_with(
221 "object: ':' was expected, but '", cur_char(),
"' found.",
offset());
238 m_handler.end_object();
245 json::parse_error::throw_with(
246 "object: either ']' or ',' expected, but '", cur_char(),
"' found.",
offset());
253 template<
typename _Handler>
256 assert(is_numeric(cur_char()) || cur_char() ==
'-');
258 double val = parse_double_or_throw();
263 number_with_exp(val);
268 m_handler.number(val);
272 template<
typename _Handler>
275 assert(cur_char() ==
'e' || cur_char() ==
'E');
280 long exp = parse_long_or_throw();
281 base *= std::pow(10.0, exp);
282 m_handler.number(base);
286 template<
typename _Handler>
292 m_handler.string(res.str, res.length, res.
transient);
297 if (res.length == parse_quoted_string_state::error_no_closing_quote)
299 else if (res.length == parse_quoted_string_state::error_illegal_escape_char)
300 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