From 4f6b2b6429c71ffab9cbd2ce8ac16a734a1a562b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 21 Mar 2018 20:12:06 +0100 Subject: [PATCH] :hammer: changed SAX interface --- .../nlohmann/detail/input/binary_reader.hpp | 38 +++++----- include/nlohmann/detail/input/json_sax.hpp | 18 ++--- include/nlohmann/detail/input/lexer.hpp | 4 +- include/nlohmann/detail/input/parser.hpp | 12 ++-- single_include/nlohmann/json.hpp | 72 ++++++++++--------- test/src/unit-cbor.cpp | 4 +- test/src/unit-class_parser.cpp | 8 +-- test/src/unit-deserialization.cpp | 6 +- test/src/unit-msgpack.cpp | 4 +- test/src/unit-ubjson.cpp | 8 +-- 10 files changed, 91 insertions(+), 83 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 0feb6dd49..e4e1f1760 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -267,7 +267,7 @@ class binary_reader case 0x7F: // UTF-8 string (indefinite length) { 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) @@ -663,7 +663,7 @@ class binary_reader case 0xBF: { 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 @@ -740,7 +740,7 @@ class binary_reader case 0xDB: // str 32 { 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 @@ -1062,13 +1062,13 @@ class binary_reader return false; } + string_t key; if (len != json_sax_t::no_limit) { for (std::size_t i = 0; i < len; ++i) { get(); - string_t key; - if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) { return false; } @@ -1077,14 +1077,14 @@ class binary_reader { return false; } + key.clear(); } } else { while (get() != 0xFF) { - string_t key; - if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) { return false; } @@ -1093,6 +1093,7 @@ class binary_reader { return false; } + key.clear(); } } @@ -1214,11 +1215,11 @@ class binary_reader return false; } + string_t key; for (std::size_t i = 0; i < len; ++i) { get(); - string_t key; - if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key))) { return false; } @@ -1227,6 +1228,7 @@ class binary_reader { return false; } + key.clear(); } return sax->end_object(); @@ -1485,13 +1487,14 @@ class binary_reader 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->string(string_t(1, static_cast(current))); + string_t s(1, static_cast(current)); + return sax->string(s); } case 'S': // string { 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 @@ -1581,6 +1584,7 @@ class binary_reader return false; } + string_t key; if (size_and_type.first != string_t::npos) { 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) { - string_t key; - if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) { return false; } @@ -1601,14 +1604,14 @@ class binary_reader { return false; } + key.clear(); } } else { 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(std::move(key)))) + if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) { return false; } @@ -1616,6 +1619,7 @@ class binary_reader { return false; } + key.clear(); } } } @@ -1628,8 +1632,7 @@ class binary_reader while (current != '}') { - string_t key; - if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key))) { return false; } @@ -1638,6 +1641,7 @@ class binary_reader return false; } get_ignore_noop(); + key.clear(); } } diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 98479eb92..eb92731fb 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -68,7 +68,7 @@ struct json_sax @param[in] val string value @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 @@ -83,7 +83,7 @@ struct json_sax @param[in] val object key @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 @@ -165,7 +165,7 @@ class json_sax_dom_parser : public json_sax return true; } - bool string(string_t&& val) override + bool string(string_t& val) override { handle_value(val); return true; @@ -184,7 +184,7 @@ class json_sax_dom_parser : public json_sax 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 object_element = &(ref_stack.back()->m_value.object->operator[](val)); @@ -340,7 +340,7 @@ class json_sax_dom_callback_parser : public json_sax return true; } - bool string(string_t&& val) override + bool string(string_t& val) override { handle_value(val); return true; @@ -362,9 +362,9 @@ class json_sax_dom_callback_parser : public json_sax 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); // add null at given key and store the reference for later @@ -531,7 +531,7 @@ class json_sax_acceptor : public json_sax return true; } - bool string(string_t&&) override + bool string(string_t&) override { return true; } @@ -541,7 +541,7 @@ class json_sax_acceptor : public json_sax return true; } - bool key(string_t&&) override + bool key(string_t&) override { return true; } diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 20c1b3fe1..1ed356173 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -1131,9 +1131,9 @@ scan_number_done: } /// 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; } ///////////////////// diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index a38101c98..d8f1ff348 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -239,7 +239,7 @@ class parser { return; } - key = m_lexer.move_string(); + key = m_lexer.get_string(); bool keep_tag = false; if (keep) @@ -375,7 +375,7 @@ class parser case token_type::value_string: { result.m_type = value_t::string; - result.m_value = m_lexer.move_string(); + result.m_value = m_lexer.get_string(); break; } @@ -498,7 +498,7 @@ class parser } else { - if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) + if (JSON_UNLIKELY(not sax->key(m_lexer.get_string()))) { return false; } @@ -560,7 +560,7 @@ class parser } 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; } @@ -606,7 +606,7 @@ class parser 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; } @@ -706,7 +706,7 @@ class parser } else { - if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) + if (JSON_UNLIKELY(not sax->key(m_lexer.get_string()))) { return false; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 55e1e01b8..de1f6bfee 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2973,9 +2973,9 @@ scan_number_done: } /// 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 @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 @@ -3223,7 +3223,7 @@ struct json_sax @param[in] val object key @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 @@ -3305,7 +3305,7 @@ class json_sax_dom_parser : public json_sax return true; } - bool string(string_t&& val) override + bool string(string_t& val) override { handle_value(val); return true; @@ -3324,7 +3324,7 @@ class json_sax_dom_parser : public json_sax 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 object_element = &(ref_stack.back()->m_value.object->operator[](val)); @@ -3480,7 +3480,7 @@ class json_sax_dom_callback_parser : public json_sax return true; } - bool string(string_t&& val) override + bool string(string_t& val) override { handle_value(val); return true; @@ -3502,9 +3502,9 @@ class json_sax_dom_callback_parser : public json_sax 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); // add null at given key and store the reference for later @@ -3671,7 +3671,7 @@ class json_sax_acceptor : public json_sax return true; } - bool string(string_t&&) override + bool string(string_t&) override { return true; } @@ -3681,7 +3681,7 @@ class json_sax_acceptor : public json_sax return true; } - bool key(string_t&&) override + bool key(string_t&) override { return true; } @@ -3941,7 +3941,7 @@ class parser { return; } - key = m_lexer.move_string(); + key = m_lexer.get_string(); bool keep_tag = false; if (keep) @@ -4077,7 +4077,7 @@ class parser case token_type::value_string: { result.m_type = value_t::string; - result.m_value = m_lexer.move_string(); + result.m_value = m_lexer.get_string(); break; } @@ -4200,7 +4200,7 @@ class parser } else { - if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) + if (JSON_UNLIKELY(not sax->key(m_lexer.get_string()))) { return false; } @@ -4262,7 +4262,7 @@ class parser } 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; } @@ -4308,7 +4308,7 @@ class parser 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; } @@ -4408,7 +4408,7 @@ class parser } else { - if (JSON_UNLIKELY(not sax->key(m_lexer.move_string()))) + if (JSON_UNLIKELY(not sax->key(m_lexer.get_string()))) { return false; } @@ -5907,7 +5907,7 @@ class binary_reader case 0x7F: // UTF-8 string (indefinite length) { 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) @@ -6303,7 +6303,7 @@ class binary_reader case 0xBF: { 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 @@ -6380,7 +6380,7 @@ class binary_reader case 0xDB: // str 32 { 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 @@ -6702,13 +6702,13 @@ class binary_reader return false; } + string_t key; if (len != json_sax_t::no_limit) { for (std::size_t i = 0; i < len; ++i) { get(); - string_t key; - if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) { return false; } @@ -6717,14 +6717,14 @@ class binary_reader { return false; } + key.clear(); } } else { while (get() != 0xFF) { - string_t key; - if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key))) { return false; } @@ -6733,6 +6733,7 @@ class binary_reader { return false; } + key.clear(); } } @@ -6854,11 +6855,11 @@ class binary_reader return false; } + string_t key; for (std::size_t i = 0; i < len; ++i) { get(); - string_t key; - if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key))) { return false; } @@ -6867,6 +6868,7 @@ class binary_reader { return false; } + key.clear(); } return sax->end_object(); @@ -7125,13 +7127,14 @@ class binary_reader 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->string(string_t(1, static_cast(current))); + string_t s(1, static_cast(current)); + return sax->string(s); } case 'S': // string { 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 @@ -7221,6 +7224,7 @@ class binary_reader return false; } + string_t key; if (size_and_type.first != string_t::npos) { 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) { - string_t key; - if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) { return false; } @@ -7241,14 +7244,14 @@ class binary_reader { return false; } + key.clear(); } } else { 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(std::move(key)))) + if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key))) { return false; } @@ -7256,6 +7259,7 @@ class binary_reader { return false; } + key.clear(); } } } @@ -7268,8 +7272,7 @@ class binary_reader while (current != '}') { - string_t key; - if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(std::move(key)))) + if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key))) { return false; } @@ -7278,6 +7281,7 @@ class binary_reader return false; } get_ignore_noop(); + key.clear(); } } diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index cd8fb0db9..29879508a 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -64,7 +64,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool string(std::string&&) override + bool string(std::string&) override { return events_left-- > 0; } @@ -74,7 +74,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool key(std::string&&) override + bool key(std::string&) override { return events_left-- > 0; } diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 1e1005689..e4cfb536b 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -67,7 +67,7 @@ class SaxEventLogger : public nlohmann::json::json_sax_t return true; } - bool string(std::string&& val) override + bool string(std::string& val) override { events.push_back("string(" + val + ")"); return true; @@ -86,7 +86,7 @@ class SaxEventLogger : public nlohmann::json::json_sax_t return true; } - bool key(std::string&& val) override + bool key(std::string& val) override { events.push_back("key(" + val + ")"); return true; @@ -159,7 +159,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool string(std::string&&) override + bool string(std::string&) override { return events_left-- > 0; } @@ -169,7 +169,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool key(std::string&&) override + bool key(std::string&) override { return events_left-- > 0; } diff --git a/test/src/unit-deserialization.cpp b/test/src/unit-deserialization.cpp index 5aa3a8b62..5c9fde5a2 100644 --- a/test/src/unit-deserialization.cpp +++ b/test/src/unit-deserialization.cpp @@ -66,7 +66,7 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t return true; } - bool string(std::string&& val) override + bool string(std::string& val) override { events.push_back("string(" + val + ")"); return true; @@ -85,7 +85,7 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t return true; } - bool key(std::string&& val) override + bool key(std::string& val) override { events.push_back("key(" + val + ")"); return true; @@ -143,7 +143,7 @@ struct SaxEventLoggerExitAfterStartObject : public SaxEventLogger struct SaxEventLoggerExitAfterKey : public SaxEventLogger { - bool key(std::string&& val) override + bool key(std::string& val) override { events.push_back("key(" + val + ")"); return false; diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index 6974c5306..7789ccb70 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -64,7 +64,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool string(std::string&&) override + bool string(std::string&) override { return events_left-- > 0; } @@ -74,7 +74,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool key(std::string&&) override + bool key(std::string&) override { return events_left-- > 0; } diff --git a/test/src/unit-ubjson.cpp b/test/src/unit-ubjson.cpp index 1ccb9e228..380e0f3f6 100644 --- a/test/src/unit-ubjson.cpp +++ b/test/src/unit-ubjson.cpp @@ -64,7 +64,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool string(std::string&&) override + bool string(std::string&) override { return events_left-- > 0; } @@ -74,7 +74,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool key(std::string&&) override + bool key(std::string&) override { return events_left-- > 0; } @@ -1668,12 +1668,12 @@ TEST_CASE("UBJSON") std::vector vST1 = {'{', '$', 'd', '#', 'i', 2, 'i', 1, 'a'}; CHECK_THROWS_AS(json::from_ubjson(vST1), json::parse_error&); CHECK_THROWS_WITH(json::from_ubjson(vST1), "[json.exception.parse_error.110] parse error at 10: unexpected end of input"); - CHECK(json::from_ubjson(vST1, true, false).is_discarded()); + CHECK(json::from_ubjson(vST1, true, false).is_discarded()); std::vector vST2 = {'{', '#', 'i', 2, 'i', 1, 'a'}; CHECK_THROWS_AS(json::from_ubjson(vST2), json::parse_error&); CHECK_THROWS_WITH(json::from_ubjson(vST2), "[json.exception.parse_error.110] parse error at 8: unexpected end of input"); - CHECK(json::from_ubjson(vST2, true, false).is_discarded()); + CHECK(json::from_ubjson(vST2, true, false).is_discarded()); } }