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);
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();
74 m_handler.end_parse();
77 template<
typename _Handler>
91 json::parse_error::throw_with(
92 "root_value: either '[' or '{' was expected, but '", cur_char(),
"' was found.",
offset());
96 template<
typename _Handler>
119 m_handler.boolean_true();
123 m_handler.boolean_false();
133 json::parse_error::throw_with(
"value: failed to parse '", cur_char(),
"'.",
offset());
137 template<
typename _Handler>
140 assert(cur_char() ==
'[');
142 m_handler.begin_array();
143 for (next(); has_char(); next())
147 if (cur_char() ==
']')
164 if (next_char() ==
']')
166 json::parse_error::throw_with(
167 "array: ']' expected but '", cur_char(),
"' found.",
offset() );
171 json::parse_error::throw_with(
172 "array: either ']' or ',' expected, but '", cur_char(),
"' found.",
offset());
187 template<
typename _Handler>
190 m_handler.end_array();
195 template<
typename _Handler>
198 assert(cur_char() ==
'{');
200 bool require_new_key =
false;
201 m_handler.begin_object();
202 for (next(); has_char(); next())
213 json::parse_error::throw_with(
214 "object: new key expected, but '", cur_char(),
"' found.",
offset());
216 m_handler.end_object();
223 json::parse_error::throw_with(
224 "object: '\"' was expected, but '", cur_char(),
"' found.",
offset());
226 require_new_key =
false;
232 if (res.length == parse_quoted_string_state::error_no_closing_quote)
233 throw json::parse_error(
"object: stream ended prematurely before reaching the closing quote of a key.",
offset());
234 else if (res.length == parse_quoted_string_state::error_illegal_escape_char)
235 json::parse_error::throw_with(
236 "object: illegal escape character '", cur_char(),
"' in key value.",
offset());
241 m_handler.object_key(res.str, res.length, res.
transient);
244 if (cur_char() !=
':')
245 json::parse_error::throw_with(
246 "object: ':' was expected, but '", cur_char(),
"' found.",
offset());
263 m_handler.end_object();
268 require_new_key =
true;
271 json::parse_error::throw_with(
272 "object: either '}' or ',' expected, but '", cur_char(),
"' found.",
offset());
279 template<
typename _Handler>
282 assert(is_numeric(cur_char()) || cur_char() ==
'-');
284 double val = parse_double_or_throw();
285 m_handler.number(val);
289 template<
typename _Handler>
295 m_handler.string(res.str, res.length, res.
transient);
300 if (res.length == parse_quoted_string_state::error_no_closing_quote)
302 else if (res.length == parse_quoted_string_state::error_illegal_escape_char)
303 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
Definition: json_parser_base.hpp:18
Definition: json_parser.hpp:23
Definition: parser_base.hpp:39
Definition: parser_global.hpp:32
Definition: base64.hpp:15
std::ptrdiff_t offset() const