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;
35 json_parser(
const char* p,
size_t n, handler_type& hdl);
48 void number_with_exp(
double base);
52 handler_type& m_handler;
55 template<
typename _Handler>
57 const char* p,
size_t n, handler_type& hdl) :
60 template<
typename _Handler>
63 m_handler.begin_parse();
72 m_handler.end_parse();
75 template<
typename _Handler>
89 json::parse_error::throw_with(
90 "root_value: either '[' or '{' was expected, but '", cur_char(),
"' was found.",
offset());
94 template<
typename _Handler>
117 m_handler.boolean_true();
121 m_handler.boolean_false();
131 json::parse_error::throw_with(
"value: failed to parse '", cur_char(),
"'.",
offset());
135 template<
typename _Handler>
138 assert(cur_char() ==
'[');
140 m_handler.begin_array();
141 for (next(); has_char(); next())
143 if (cur_char() ==
']')
145 m_handler.end_array();
160 m_handler.end_array();
167 json::parse_error::throw_with(
168 "array: either ']' or ',' expected, but '", cur_char(),
"' found.",
offset());
176 template<
typename _Handler>
179 assert(cur_char() ==
'{');
181 m_handler.begin_object();
182 for (next(); has_char(); next())
191 m_handler.end_object();
198 json::parse_error::throw_with(
199 "object: '\"' was expected, but '", cur_char(),
"' found.",
offset());
206 if (res.length == parse_quoted_string_state::error_no_closing_quote)
207 throw json::parse_error(
"object: stream ended prematurely before reaching the closing quote of a key.",
offset());
208 else if (res.length == parse_quoted_string_state::error_illegal_escape_char)
209 json::parse_error::throw_with(
210 "object: illegal escape character '", cur_char(),
"' in key value.",
offset());
215 m_handler.object_key(res.str, res.length, res.
transient);
218 if (cur_char() !=
':')
219 json::parse_error::throw_with(
220 "object: ':' was expected, but '", cur_char(),
"' found.",
offset());
237 m_handler.end_object();
244 json::parse_error::throw_with(
245 "object: either ']' or ',' expected, but '", cur_char(),
"' found.",
offset());
252 template<
typename _Handler>
255 assert(is_numeric(cur_char()) || cur_char() ==
'-');
257 double val = parse_double_or_throw();
262 number_with_exp(val);
267 m_handler.number(val);
271 template<
typename _Handler>
274 assert(cur_char() ==
'e' || cur_char() ==
'E');
279 long exp = parse_long_or_throw();
280 base *= std::pow(10.0, exp);
281 m_handler.number(base);
285 template<
typename _Handler>
291 m_handler.string(res.str, res.length, res.
transient);
296 if (res.length == parse_quoted_string_state::error_no_closing_quote)
298 else if (res.length == parse_quoted_string_state::error_illegal_escape_char)
299 json::parse_error::throw_with(
"string: illegal escape character '", cur_char(),
"'.",
offset());
void parse()
Definition: json_parser.hpp:61
bool transient
Definition: parser_global.hpp:50
json_parser(const char *p, size_t n, handler_type &hdl)
Definition: json_parser.hpp:56
Definition: json_parser_base.hpp:30
std::ptrdiff_t offset() const
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