🔨 changed SAX interface

This commit is contained in:
Niels Lohmann 2018-03-21 20:12:06 +01:00
parent 2537677e4c
commit 4f6b2b6429
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
10 changed files with 91 additions and 83 deletions

View file

@ -267,7 +267,7 @@ class binary_reader
case 0x7F: // UTF-8 string (indefinite length) case 0x7F: // UTF-8 string (indefinite length)
{ {
string_t s; string_t s;
return get_cbor_string(s) and sax->string(std::move(s)); return get_cbor_string(s) and sax->string(s);
} }
// array (0x00..0x17 data items follow) // array (0x00..0x17 data items follow)
@ -663,7 +663,7 @@ class binary_reader
case 0xBF: case 0xBF:
{ {
string_t s; string_t s;
return get_msgpack_string(s) and sax->string(std::move(s)); return get_msgpack_string(s) and sax->string(s);
} }
case 0xC0: // nil case 0xC0: // nil
@ -740,7 +740,7 @@ class binary_reader
case 0xDB: // str 32 case 0xDB: // str 32
{ {
string_t s; string_t s;
return get_msgpack_string(s) and sax->string(std::move(s)); return get_msgpack_string(s) and sax->string(s);
} }
case 0xDC: // array 16 case 0xDC: // array 16
@ -1062,13 +1062,13 @@ class binary_reader
return false; return false;
} }
string_t key;
if (len != json_sax_t::no_limit) if (len != json_sax_t::no_limit)
{ {
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
get(); get();
string_t key; if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -1077,14 +1077,14 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
else else
{ {
while (get() != 0xFF) while (get() != 0xFF)
{ {
string_t key; if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -1093,6 +1093,7 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
@ -1214,11 +1215,11 @@ class binary_reader
return false; return false;
} }
string_t key;
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
get(); get();
string_t key; if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -1227,6 +1228,7 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
return sax->end_object(); return sax->end_object();
@ -1485,13 +1487,14 @@ class binary_reader
auto last_token = get_token_string(); auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token)); return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token));
} }
return sax->string(string_t(1, static_cast<char>(current))); string_t s(1, static_cast<char>(current));
return sax->string(s);
} }
case 'S': // string case 'S': // string
{ {
string_t s; string_t s;
return get_ubjson_string(s) and sax->string(std::move(s)); return get_ubjson_string(s) and sax->string(s);
} }
case '[': // array case '[': // array
@ -1581,6 +1584,7 @@ class binary_reader
return false; return false;
} }
string_t key;
if (size_and_type.first != string_t::npos) if (size_and_type.first != string_t::npos)
{ {
if (JSON_UNLIKELY(not sax->start_object(size_and_type.first))) if (JSON_UNLIKELY(not sax->start_object(size_and_type.first)))
@ -1592,8 +1596,7 @@ class binary_reader
{ {
for (std::size_t i = 0; i < size_and_type.first; ++i) for (std::size_t i = 0; i < size_and_type.first; ++i)
{ {
string_t key; if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -1601,14 +1604,14 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
else else
{ {
for (std::size_t i = 0; i < size_and_type.first; ++i) for (std::size_t i = 0; i < size_and_type.first; ++i)
{ {
string_t key; if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -1616,6 +1619,7 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
} }
@ -1628,8 +1632,7 @@ class binary_reader
while (current != '}') while (current != '}')
{ {
string_t key; if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -1638,6 +1641,7 @@ class binary_reader
return false; return false;
} }
get_ignore_noop(); get_ignore_noop();
key.clear();
} }
} }

View file

@ -68,7 +68,7 @@ struct json_sax
@param[in] val string value @param[in] val string value
@return whether parsing should proceed @return whether parsing should proceed
*/ */
virtual bool string(string_t&& val) = 0; virtual bool string(string_t& val) = 0;
/*! /*!
@brief the beginning of an object was read @brief the beginning of an object was read
@ -83,7 +83,7 @@ struct json_sax
@param[in] val object key @param[in] val object key
@return whether parsing should proceed @return whether parsing should proceed
*/ */
virtual bool key(string_t&& val) = 0; virtual bool key(string_t& val) = 0;
/*! /*!
@brief the end of an object was read @brief the end of an object was read
@ -165,7 +165,7 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool string(string_t&& val) override bool string(string_t& val) override
{ {
handle_value(val); handle_value(val);
return true; return true;
@ -184,7 +184,7 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t&& val) override bool key(string_t& val) override
{ {
// add null at given key and store the reference for later // add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val)); object_element = &(ref_stack.back()->m_value.object->operator[](val));
@ -340,7 +340,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool string(string_t&& val) override bool string(string_t& val) override
{ {
handle_value(val); handle_value(val);
return true; return true;
@ -362,9 +362,9 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t&& val) override bool key(string_t& val) override
{ {
BasicJsonType k = BasicJsonType(std::forward < string_t&& > (val)); BasicJsonType k = BasicJsonType(val);
const bool keep = callback(ref_stack.size(), parse_event_t::key, k); const bool keep = callback(ref_stack.size(), parse_event_t::key, k);
// add null at given key and store the reference for later // add null at given key and store the reference for later
@ -531,7 +531,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return true; return true;
} }
bool string(string_t&&) override bool string(string_t&) override
{ {
return true; return true;
} }
@ -541,7 +541,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t&&) override bool key(string_t&) override
{ {
return true; return true;
} }

View file

@ -1131,9 +1131,9 @@ scan_number_done:
} }
/// return current string value (implicitly resets the token; useful only once) /// return current string value (implicitly resets the token; useful only once)
string_t&& move_string() string_t& get_string()
{ {
return std::move(token_buffer); return token_buffer;
} }
///////////////////// /////////////////////

View file

@ -239,7 +239,7 @@ class parser
{ {
return; return;
} }
key = m_lexer.move_string(); key = m_lexer.get_string();
bool keep_tag = false; bool keep_tag = false;
if (keep) if (keep)
@ -375,7 +375,7 @@ class parser
case token_type::value_string: case token_type::value_string:
{ {
result.m_type = value_t::string; result.m_type = value_t::string;
result.m_value = m_lexer.move_string(); result.m_value = m_lexer.get_string();
break; break;
} }
@ -498,7 +498,7 @@ class parser
} }
else else
{ {
if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
{ {
return false; return false;
} }
@ -560,7 +560,7 @@ class parser
} }
else else
{ {
if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.get_string())))
{ {
return false; return false;
} }
@ -606,7 +606,7 @@ class parser
case token_type::value_string: case token_type::value_string:
{ {
if (JSON_UNLIKELY(not sax->string(m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->string(m_lexer.get_string())))
{ {
return false; return false;
} }
@ -706,7 +706,7 @@ class parser
} }
else else
{ {
if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
{ {
return false; return false;
} }

View file

@ -2973,9 +2973,9 @@ scan_number_done:
} }
/// return current string value (implicitly resets the token; useful only once) /// return current string value (implicitly resets the token; useful only once)
string_t&& move_string() string_t& get_string()
{ {
return std::move(token_buffer); return token_buffer;
} }
///////////////////// /////////////////////
@ -3208,7 +3208,7 @@ struct json_sax
@param[in] val string value @param[in] val string value
@return whether parsing should proceed @return whether parsing should proceed
*/ */
virtual bool string(string_t&& val) = 0; virtual bool string(string_t& val) = 0;
/*! /*!
@brief the beginning of an object was read @brief the beginning of an object was read
@ -3223,7 +3223,7 @@ struct json_sax
@param[in] val object key @param[in] val object key
@return whether parsing should proceed @return whether parsing should proceed
*/ */
virtual bool key(string_t&& val) = 0; virtual bool key(string_t& val) = 0;
/*! /*!
@brief the end of an object was read @brief the end of an object was read
@ -3305,7 +3305,7 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool string(string_t&& val) override bool string(string_t& val) override
{ {
handle_value(val); handle_value(val);
return true; return true;
@ -3324,7 +3324,7 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t&& val) override bool key(string_t& val) override
{ {
// add null at given key and store the reference for later // add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val)); object_element = &(ref_stack.back()->m_value.object->operator[](val));
@ -3480,7 +3480,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool string(string_t&& val) override bool string(string_t& val) override
{ {
handle_value(val); handle_value(val);
return true; return true;
@ -3502,9 +3502,9 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t&& val) override bool key(string_t& val) override
{ {
BasicJsonType k = BasicJsonType(std::forward < string_t&& > (val)); BasicJsonType k = BasicJsonType(val);
const bool keep = callback(ref_stack.size(), parse_event_t::key, k); const bool keep = callback(ref_stack.size(), parse_event_t::key, k);
// add null at given key and store the reference for later // add null at given key and store the reference for later
@ -3671,7 +3671,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return true; return true;
} }
bool string(string_t&&) override bool string(string_t&) override
{ {
return true; return true;
} }
@ -3681,7 +3681,7 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t&&) override bool key(string_t&) override
{ {
return true; return true;
} }
@ -3941,7 +3941,7 @@ class parser
{ {
return; return;
} }
key = m_lexer.move_string(); key = m_lexer.get_string();
bool keep_tag = false; bool keep_tag = false;
if (keep) if (keep)
@ -4077,7 +4077,7 @@ class parser
case token_type::value_string: case token_type::value_string:
{ {
result.m_type = value_t::string; result.m_type = value_t::string;
result.m_value = m_lexer.move_string(); result.m_value = m_lexer.get_string();
break; break;
} }
@ -4200,7 +4200,7 @@ class parser
} }
else else
{ {
if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
{ {
return false; return false;
} }
@ -4262,7 +4262,7 @@ class parser
} }
else else
{ {
if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.get_string())))
{ {
return false; return false;
} }
@ -4308,7 +4308,7 @@ class parser
case token_type::value_string: case token_type::value_string:
{ {
if (JSON_UNLIKELY(not sax->string(m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->string(m_lexer.get_string())))
{ {
return false; return false;
} }
@ -4408,7 +4408,7 @@ class parser
} }
else else
{ {
if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
{ {
return false; return false;
} }
@ -5907,7 +5907,7 @@ class binary_reader
case 0x7F: // UTF-8 string (indefinite length) case 0x7F: // UTF-8 string (indefinite length)
{ {
string_t s; string_t s;
return get_cbor_string(s) and sax->string(std::move(s)); return get_cbor_string(s) and sax->string(s);
} }
// array (0x00..0x17 data items follow) // array (0x00..0x17 data items follow)
@ -6303,7 +6303,7 @@ class binary_reader
case 0xBF: case 0xBF:
{ {
string_t s; string_t s;
return get_msgpack_string(s) and sax->string(std::move(s)); return get_msgpack_string(s) and sax->string(s);
} }
case 0xC0: // nil case 0xC0: // nil
@ -6380,7 +6380,7 @@ class binary_reader
case 0xDB: // str 32 case 0xDB: // str 32
{ {
string_t s; string_t s;
return get_msgpack_string(s) and sax->string(std::move(s)); return get_msgpack_string(s) and sax->string(s);
} }
case 0xDC: // array 16 case 0xDC: // array 16
@ -6702,13 +6702,13 @@ class binary_reader
return false; return false;
} }
string_t key;
if (len != json_sax_t::no_limit) if (len != json_sax_t::no_limit)
{ {
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
get(); get();
string_t key; if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -6717,14 +6717,14 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
else else
{ {
while (get() != 0xFF) while (get() != 0xFF)
{ {
string_t key; if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -6733,6 +6733,7 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
@ -6854,11 +6855,11 @@ class binary_reader
return false; return false;
} }
string_t key;
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
get(); get();
string_t key; if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -6867,6 +6868,7 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
return sax->end_object(); return sax->end_object();
@ -7125,13 +7127,14 @@ class binary_reader
auto last_token = get_token_string(); auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token)); return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token));
} }
return sax->string(string_t(1, static_cast<char>(current))); string_t s(1, static_cast<char>(current));
return sax->string(s);
} }
case 'S': // string case 'S': // string
{ {
string_t s; string_t s;
return get_ubjson_string(s) and sax->string(std::move(s)); return get_ubjson_string(s) and sax->string(s);
} }
case '[': // array case '[': // array
@ -7221,6 +7224,7 @@ class binary_reader
return false; return false;
} }
string_t key;
if (size_and_type.first != string_t::npos) if (size_and_type.first != string_t::npos)
{ {
if (JSON_UNLIKELY(not sax->start_object(size_and_type.first))) if (JSON_UNLIKELY(not sax->start_object(size_and_type.first)))
@ -7232,8 +7236,7 @@ class binary_reader
{ {
for (std::size_t i = 0; i < size_and_type.first; ++i) for (std::size_t i = 0; i < size_and_type.first; ++i)
{ {
string_t key; if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -7241,14 +7244,14 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
else else
{ {
for (std::size_t i = 0; i < size_and_type.first; ++i) for (std::size_t i = 0; i < size_and_type.first; ++i)
{ {
string_t key; if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -7256,6 +7259,7 @@ class binary_reader
{ {
return false; return false;
} }
key.clear();
} }
} }
} }
@ -7268,8 +7272,7 @@ class binary_reader
while (current != '}') while (current != '}')
{ {
string_t key; if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(std::move(key))))
{ {
return false; return false;
} }
@ -7278,6 +7281,7 @@ class binary_reader
return false; return false;
} }
get_ignore_noop(); get_ignore_noop();
key.clear();
} }
} }

View file

@ -64,7 +64,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&&) override bool string(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -74,7 +74,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&&) override bool key(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }

View file

@ -67,7 +67,7 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool string(std::string&& val) override bool string(std::string& val) override
{ {
events.push_back("string(" + val + ")"); events.push_back("string(" + val + ")");
return true; return true;
@ -86,7 +86,7 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool key(std::string&& val) override bool key(std::string& val) override
{ {
events.push_back("key(" + val + ")"); events.push_back("key(" + val + ")");
return true; return true;
@ -159,7 +159,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&&) override bool string(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -169,7 +169,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&&) override bool key(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }

View file

@ -66,7 +66,7 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool string(std::string&& val) override bool string(std::string& val) override
{ {
events.push_back("string(" + val + ")"); events.push_back("string(" + val + ")");
return true; return true;
@ -85,7 +85,7 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool key(std::string&& val) override bool key(std::string& val) override
{ {
events.push_back("key(" + val + ")"); events.push_back("key(" + val + ")");
return true; return true;
@ -143,7 +143,7 @@ struct SaxEventLoggerExitAfterStartObject : public SaxEventLogger
struct SaxEventLoggerExitAfterKey : public SaxEventLogger struct SaxEventLoggerExitAfterKey : public SaxEventLogger
{ {
bool key(std::string&& val) override bool key(std::string& val) override
{ {
events.push_back("key(" + val + ")"); events.push_back("key(" + val + ")");
return false; return false;

View file

@ -64,7 +64,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&&) override bool string(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -74,7 +74,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&&) override bool key(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }

View file

@ -64,7 +64,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&&) override bool string(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -74,7 +74,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&&) override bool key(std::string&) override
{ {
return events_left-- > 0; return events_left-- > 0;
} }