From 6151b056eb2204cb79576e546619a8ea11689f67 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 10 Aug 2017 23:40:30 +0200 Subject: [PATCH 1/2] :hammer: cleanup --- src/json.hpp | 253 +++++++++------------------------------------------ 1 file changed, 42 insertions(+), 211 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index fdade71fb..a704d864b 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -463,13 +463,9 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept }; // discarded values are not comparable - if (lhs == value_t::discarded or rhs == value_t::discarded) - { - return false; - } + return lhs != value_t::discarded and rhs != value_t::discarded and + order[static_cast(lhs)] < order[static_cast(rhs)]; - return order[static_cast(lhs)] < - order[static_cast(rhs)]; } @@ -1007,10 +1003,9 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) val = static_cast(*j.template get_ptr()); break; } + default: - { JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); - } } } @@ -1198,10 +1193,9 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) val = static_cast(*j.template get_ptr()); break; } + default: - { JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); - } } } @@ -1629,11 +1623,8 @@ class lexer return "end of input"; case token_type::literal_or_value: return "'[', '{', or a literal"; - default: - { - // catch non-enum values + default: // catch non-enum values return "unknown token"; // LCOV_EXCL_LINE - } } } @@ -2339,9 +2330,7 @@ scan_number_zero: } default: - { goto scan_number_done; - } } scan_number_any1: @@ -2377,9 +2366,7 @@ scan_number_any1: } default: - { goto scan_number_done; - } } scan_number_decimal1: @@ -2436,9 +2423,7 @@ scan_number_decimal2: } default: - { goto scan_number_done; - } } scan_number_exponent: @@ -2522,9 +2507,7 @@ scan_number_any2: } default: - { goto scan_number_done; - } } scan_number_done: @@ -2911,12 +2894,8 @@ class parser return false; } - if (strict and get_token() != token_type::end_of_input) - { - return false; - } - - return true; + // strict => last token must be EOF + return not strict or (get_token() == token_type::end_of_input); } private: @@ -3156,10 +3135,7 @@ class parser JSON_THROW(out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); } - else - { - expect(token_type::uninitialized); - } + expect(token_type::uninitialized); } break; } @@ -3297,15 +3273,10 @@ class parser case token_type::value_integer: case token_type::value_string: case token_type::value_unsigned: - { return true; - } - default: - { - // the last token was unexpected + default: // the last token was unexpected return false; - } } } @@ -3740,9 +3711,7 @@ class iter_impl : public std::iteratorm_type) { case value_t::object: - { return (m_it.object_iterator == other.m_it.object_iterator); - } case value_t::array: - { return (m_it.array_iterator == other.m_it.array_iterator); - } default: - { return (m_it.primitive_iterator == other.m_it.primitive_iterator); - } } } @@ -3935,19 +3898,13 @@ class iter_impl : public std::iteratorm_type) { case value_t::object: - { JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators")); - } case value_t::array: - { return (m_it.array_iterator < other.m_it.array_iterator); - } default: - { return (m_it.primitive_iterator < other.m_it.primitive_iterator); - } } } @@ -3989,9 +3946,7 @@ class iter_impl : public std::iteratorm_type) { case value_t::object: - { JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); - } case value_t::array: { @@ -4062,19 +4017,13 @@ class iter_impl : public std::iteratorm_type) { case value_t::object: - { JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); - } case value_t::array: - { return m_it.array_iterator - other.m_it.array_iterator; - } default: - { return m_it.primitive_iterator - other.m_it.primitive_iterator; - } } } @@ -4089,19 +4038,13 @@ class iter_impl : public std::iteratorm_type) { case value_t::object: - { JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators")); - } case value_t::array: - { return *std::next(m_it.array_iterator, n); - } case value_t::null: - { JSON_THROW(invalid_iterator::create(214, "cannot get value")); - } default: { @@ -4193,21 +4136,15 @@ template class iteration_proxy { // use integer array index as key case value_t::array: - { return std::to_string(array_index); - } // use key from the object case value_t::object: - { return anchor.key(); - } // use an empty key for all primitive types default: - { return ""; - } } } @@ -4486,9 +4423,7 @@ class binary_reader { // EOF case std::char_traits::eof(): - { JSON_THROW(parse_error::create(110, chars_read, "unexpected end of input")); - } // Integer 0x00..0x17 (0..23) case 0x00: @@ -4515,29 +4450,19 @@ class binary_reader case 0x15: case 0x16: case 0x17: - { return static_cast(current); - } case 0x18: // Unsigned integer (one-byte uint8_t follows) - { return get_number(); - } case 0x19: // Unsigned integer (two-byte uint16_t follows) - { return get_number(); - } case 0x1a: // Unsigned integer (four-byte uint32_t follows) - { return get_number(); - } case 0x1b: // Unsigned integer (eight-byte uint64_t follows) - { return get_number(); - } // Negative integer -1-0x00..-1-0x17 (-1..-24) case 0x20: @@ -4564,9 +4489,7 @@ class binary_reader case 0x35: case 0x36: case 0x37: - { return static_cast(0x20 - 1 - current); - } case 0x38: // Negative integer (one-byte uint8_t follows) { @@ -4896,9 +4819,7 @@ class binary_reader { // EOF case std::char_traits::eof(): - { JSON_THROW(parse_error::create(110, chars_read, "unexpected end of input")); - } // positive fixint case 0x00: @@ -5029,9 +4950,7 @@ class binary_reader case 0x7d: case 0x7e: case 0x7f: - { return static_cast(current); - } // fixmap case 0x80: @@ -5122,81 +5041,51 @@ class binary_reader case 0xbd: case 0xbe: case 0xbf: - { return get_msgpack_string(); - } case 0xc0: // nil - { return value_t::null; - } case 0xc2: // false - { return false; - } case 0xc3: // true - { return true; - } case 0xca: // float 32 - { return get_number(); - } case 0xcb: // float 64 - { return get_number(); - } case 0xcc: // uint 8 - { return get_number(); - } case 0xcd: // uint 16 - { return get_number(); - } case 0xce: // uint 32 - { return get_number(); - } case 0xcf: // uint 64 - { return get_number(); - } case 0xd0: // int 8 - { return get_number(); - } case 0xd1: // int 16 - { return get_number(); - } case 0xd2: // int 32 - { return get_number(); - } case 0xd3: // int 64 - { return get_number(); - } case 0xd9: // str 8 case 0xda: // str 16 case 0xdb: // str 32 - { return get_msgpack_string(); - } case 0xdc: // array 16 { @@ -5279,9 +5168,7 @@ class binary_reader case 0xfd: case 0xfe: case 0xff: - { return static_cast(current); - } default: // anything else { @@ -5718,9 +5605,8 @@ class binary_writer break; } - case value_t::number_float: + case value_t::number_float: // Double-Precision Float { - // Double-Precision Float oa->write_character(static_cast(0xfb)); write_number(j.m_value.number_float); break; @@ -5844,9 +5730,7 @@ class binary_writer } default: - { break; - } } } @@ -5857,16 +5741,14 @@ class binary_writer { switch (j.type()) { - case value_t::null: + case value_t::null: // nil { - // nil oa->write_character(static_cast(0xc0)); break; } - case value_t::boolean: + case value_t::boolean: // true and false { - // true and false oa->write_character(j.m_value.boolean ? static_cast(0xc3) : static_cast(0xc2)); @@ -5983,9 +5865,8 @@ class binary_writer break; } - case value_t::number_float: + case value_t::number_float: // float 64 { - // float 64 oa->write_character(static_cast(0xcb)); write_number(j.m_value.number_float); break; @@ -6088,9 +5969,7 @@ class binary_writer } default: - { break; - } } } @@ -6481,7 +6360,7 @@ class serializer return res; } - static void escape_codepoint(int codepoint, string_t& result, size_t& pos) + static void escape_codepoint(int codepoint, string_t& result, std::size_t& pos) { // expecting a proper codepoint assert(0x00 <= codepoint and codepoint <= 0x10FFFF); @@ -6563,56 +6442,49 @@ class serializer { switch (s[i]) { - // quotation mark (0x22) - case '"': + case '"': // quotation mark (0x22) { result[pos + 1] = '"'; pos += 2; break; } - // reverse solidus (0x5c) - case '\\': + case '\\': // reverse solidus (0x5c) { // nothing to change pos += 2; break; } - // backspace (0x08) - case '\b': + case '\b': // backspace (0x08) { result[pos + 1] = 'b'; pos += 2; break; } - // formfeed (0x0c) - case '\f': + case '\f': // formfeed (0x0c) { result[pos + 1] = 'f'; pos += 2; break; } - // newline (0x0a) - case '\n': + case '\n': // newline (0x0a) { result[pos + 1] = 'n'; pos += 2; break; } - // carriage return (0x0d) - case '\r': + case '\r': // carriage return (0x0d) { result[pos + 1] = 'r'; pos += 2; break; } - // horizontal tab (0x09) - case '\t': + case '\t': // horizontal tab (0x09) { result[pos + 1] = 't'; pos += 2; @@ -6780,7 +6652,7 @@ class serializer // negative value indicates an error assert(len > 0); // check if buffer was large enough - assert(static_cast(len) < number_buffer.size()); + assert(static_cast(len) < number_buffer.size()); // erase thousands separator if (thousands_sep != '\0') @@ -6805,7 +6677,7 @@ class serializer } } - o->write_characters(number_buffer.data(), static_cast(len)); + o->write_characters(number_buffer.data(), static_cast(len)); // determine if need to append ".0" const bool value_is_int_like = @@ -8710,9 +8582,7 @@ class basic_json } default: - { break; - } } switch (m_type) @@ -8762,10 +8632,8 @@ class basic_json } default: - { JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()))); - } } assert_invariant(); @@ -8851,9 +8719,7 @@ class basic_json } default: - { break; - } } assert_invariant(); @@ -10648,9 +10514,7 @@ class basic_json } default: - { JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); - } } return result; @@ -10758,9 +10622,7 @@ class basic_json } default: - { JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); - } } return result; @@ -11529,9 +11391,7 @@ class basic_json } default: - { break; - } } } @@ -12238,41 +12098,31 @@ class basic_json switch (lhs_type) { case value_t::array: - { return (*lhs.m_value.array == *rhs.m_value.array); - } + case value_t::object: - { return (*lhs.m_value.object == *rhs.m_value.object); - } + case value_t::null: - { return true; - } + case value_t::string: - { return (*lhs.m_value.string == *rhs.m_value.string); - } + case value_t::boolean: - { return (lhs.m_value.boolean == rhs.m_value.boolean); - } + case value_t::number_integer: - { return (lhs.m_value.number_integer == rhs.m_value.number_integer); - } + case value_t::number_unsigned: - { return (lhs.m_value.number_unsigned == rhs.m_value.number_unsigned); - } + case value_t::number_float: - { return (lhs.m_value.number_float == rhs.m_value.number_float); - } + default: - { return false; - } } } else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float) @@ -12402,41 +12252,31 @@ class basic_json switch (lhs_type) { case value_t::array: - { return (*lhs.m_value.array) < (*rhs.m_value.array); - } + case value_t::object: - { return *lhs.m_value.object < *rhs.m_value.object; - } + case value_t::null: - { return false; - } + case value_t::string: - { return *lhs.m_value.string < *rhs.m_value.string; - } + case value_t::boolean: - { return lhs.m_value.boolean < rhs.m_value.boolean; - } + case value_t::number_integer: - { return lhs.m_value.number_integer < rhs.m_value.number_integer; - } + case value_t::number_unsigned: - { return lhs.m_value.number_unsigned < rhs.m_value.number_unsigned; - } + case value_t::number_float: - { return lhs.m_value.number_float < rhs.m_value.number_float; - } + default: - { return false; - } } } else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float) @@ -14105,9 +13945,7 @@ json_pointer::get_and_create(NLOHMANN_BASIC_JSON_TPL& j) const single value; that is, with an empty list of reference tokens. */ default: - { JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); - } } } @@ -14179,9 +14017,7 @@ json_pointer::get_unchecked(NLOHMANN_BASIC_JSON_TPL* ptr) const } default: - { JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); - } } } @@ -14235,9 +14071,7 @@ json_pointer::get_checked(NLOHMANN_BASIC_JSON_TPL* ptr) const } default: - { JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); - } } } @@ -14292,9 +14126,7 @@ json_pointer::get_unchecked(const NLOHMANN_BASIC_JSON_TPL* ptr) const } default: - { JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); - } } } @@ -14348,9 +14180,7 @@ json_pointer::get_checked(const NLOHMANN_BASIC_JSON_TPL* ptr) const } default: - { JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); - } } } @@ -14491,9 +14321,10 @@ struct hash }; /// specialization for std::less +/// @note: do not remove the space after '<', +/// see https://github.com/nlohmann/json/pull/679 template<> -struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', - // see https://github.com/nlohmann/json/pull/679 +struct less< ::nlohmann::detail::value_t> { /*! @brief compare two value_t enum values From 7d19aed56741534c8815d52c7030944f3509f219 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 13 Aug 2017 19:02:58 +0200 Subject: [PATCH 2/2] :hammer: cleanup --- src/json.hpp | 285 +++++++++++++++++++++------------------------------ 1 file changed, 117 insertions(+), 168 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index a704d864b..0ac3e5ff3 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -465,7 +465,6 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept // discarded values are not comparable return lhs != value_t::discarded and rhs != value_t::discarded and order[static_cast(lhs)] < order[static_cast(rhs)]; - } @@ -1075,11 +1074,11 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); } - - for (auto it = j.rbegin(), end = j.rend(); it != end; ++it) + std::transform(j.rbegin(), j.rend(), + std::front_inserter(l), [](const BasicJsonType & i) { - l.push_front(it->template get()); - } + return i.template get(); + }); } template @@ -1673,7 +1672,8 @@ class lexer assert(current == 'u'); int codepoint = 0; - for (int factor = 12; factor >= 0; factor -= 4) + const auto factors = { 12, 8, 4, 0 }; + for (const auto factor : factors) { get(); @@ -2520,11 +2520,12 @@ scan_number_done: add('\0'); --yylen; + char* endptr = nullptr; + errno = 0; + // try to parse integers first and fall back to floats if (number_type == token_type::value_unsigned) { - char* endptr = nullptr; - errno = 0; const auto x = std::strtoull(yytext.data(), &endptr, 10); // we checked the number format before @@ -2541,8 +2542,6 @@ scan_number_done: } else if (number_type == token_type::value_integer) { - char* endptr = nullptr; - errno = 0; const auto x = std::strtoll(yytext.data(), &endptr, 10); // we checked the number format before @@ -2560,7 +2559,11 @@ scan_number_done: // this code is reached if we parse a floating-point number or if an // integer conversion above failed - strtof(value_float, yytext.data(), nullptr); + strtof(value_float, yytext.data(), &endptr); + + // we checked the number format before + assert(endptr == yytext.data() + yylen); + return token_type::value_float; } @@ -2683,7 +2686,7 @@ scan_number_done: else { // add character as is - result.append(1, c); + result.push_back(c); } } @@ -2834,8 +2837,7 @@ class parser explicit parser(detail::input_adapter_t adapter, const parser_callback_t cb = nullptr, const bool allow_exceptions_ = true) - : callback(cb), m_lexer(adapter), - allow_exceptions(allow_exceptions_) + : callback(cb), m_lexer(adapter), allow_exceptions(allow_exceptions_) {} /*! @@ -4573,57 +4575,27 @@ class binary_reader case 0x96: case 0x97: { - BasicJsonType result = value_t::array; - const auto len = static_cast(current & 0x1f); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_cbor()); - } - return result; + return get_cbor_array(current & 0x1f); } case 0x98: // array (one-byte uint8_t for n follows) { - BasicJsonType result = value_t::array; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_cbor()); - } - return result; + return get_cbor_array(get_number()); } case 0x99: // array (two-byte uint16_t for n follow) { - BasicJsonType result = value_t::array; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_cbor()); - } - return result; + return get_cbor_array(get_number()); } case 0x9a: // array (four-byte uint32_t for n follow) { - BasicJsonType result = value_t::array; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_cbor()); - } - return result; + return get_cbor_array(get_number()); } case 0x9b: // array (eight-byte uint64_t for n follow) { - BasicJsonType result = value_t::array; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_cbor()); - } - return result; + return get_cbor_array(get_number()); } case 0x9f: // array (indefinite length) @@ -4662,67 +4634,27 @@ class binary_reader case 0xb6: case 0xb7: { - BasicJsonType result = value_t::object; - const auto len = static_cast(current & 0x1f); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_cbor_string(); - result[key] = parse_cbor(); - } - return result; + return get_cbor_object(current & 0x1f); } case 0xb8: // map (one-byte uint8_t for n follows) { - BasicJsonType result = value_t::object; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_cbor_string(); - result[key] = parse_cbor(); - } - return result; + return get_cbor_object(get_number()); } case 0xb9: // map (two-byte uint16_t for n follow) { - BasicJsonType result = value_t::object; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_cbor_string(); - result[key] = parse_cbor(); - } - return result; + return get_cbor_object(get_number()); } case 0xba: // map (four-byte uint32_t for n follow) { - BasicJsonType result = value_t::object; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_cbor_string(); - result[key] = parse_cbor(); - } - return result; + return get_cbor_object(get_number()); } case 0xbb: // map (eight-byte uint64_t for n follow) { - BasicJsonType result = value_t::object; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_cbor_string(); - result[key] = parse_cbor(); - } - return result; + return get_cbor_object(get_number()); } case 0xbf: // map (indefinite length) @@ -4970,15 +4902,7 @@ class binary_reader case 0x8e: case 0x8f: { - BasicJsonType result = value_t::object; - const auto len = static_cast(current & 0x0f); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_msgpack_string(); - result[key] = parse_msgpack(); - } - return result; + return get_msgpack_object(current & 0x0f); } // fixarray @@ -4999,13 +4923,7 @@ class binary_reader case 0x9e: case 0x9f: { - BasicJsonType result = value_t::array; - const auto len = static_cast(current & 0x0f); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_msgpack()); - } - return result; + return get_msgpack_array(current & 0x0f); } // fixstr @@ -5089,50 +5007,22 @@ class binary_reader case 0xdc: // array 16 { - BasicJsonType result = value_t::array; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_msgpack()); - } - return result; + return get_msgpack_array(get_number()); } case 0xdd: // array 32 { - BasicJsonType result = value_t::array; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - result.push_back(parse_msgpack()); - } - return result; + return get_msgpack_array(get_number()); } case 0xde: // map 16 { - BasicJsonType result = value_t::object; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_msgpack_string(); - result[key] = parse_msgpack(); - } - return result; + return get_msgpack_object(get_number()); } case 0xdf: // map 32 { - BasicJsonType result = value_t::object; - const auto len = static_cast(get_number()); - for (std::size_t i = 0; i < len; ++i) - { - get(); - auto key = get_msgpack_string(); - result[key] = parse_msgpack(); - } - return result; + return get_msgpack_object(get_number()); } // positive fixint @@ -5260,15 +5150,16 @@ class binary_reader @throw parse_error.110 if input has less than @a len bytes */ - std::string get_string(const std::size_t len) + template + std::string get_string(const NumberType len) { std::string result; - for (std::size_t i = 0; i < len; ++i) + std::generate_n(std::back_inserter(result), len, [this]() { get(); check_eof(); - result.append(1, static_cast(current)); - } + return current; + }); return result; } @@ -5316,32 +5207,27 @@ class binary_reader case 0x76: case 0x77: { - const auto len = static_cast(current & 0x1f); - return get_string(len); + return get_string(current & 0x1f); } case 0x78: // UTF-8 string (one-byte uint8_t for n follows) { - const auto len = static_cast(get_number()); - return get_string(len); + return get_string(get_number()); } case 0x79: // UTF-8 string (two-byte uint16_t for n follow) { - const auto len = static_cast(get_number()); - return get_string(len); + return get_string(get_number()); } case 0x7a: // UTF-8 string (four-byte uint32_t for n follow) { - const auto len = static_cast(get_number()); - return get_string(len); + return get_string(get_number()); } case 0x7b: // UTF-8 string (eight-byte uint64_t for n follow) { - const auto len = static_cast(get_number()); - return get_string(len); + return get_string(get_number()); } case 0x7f: // UTF-8 string (indefinite length) @@ -5350,7 +5236,7 @@ class binary_reader while (get() != 0xff) { check_eof(); - result.append(1, static_cast(current)); + result.push_back(static_cast(current)); } return result; } @@ -5364,6 +5250,33 @@ class binary_reader } } + template + BasicJsonType get_cbor_array(const NumberType len) + { + BasicJsonType result = value_t::array; + std::generate_n(std::back_inserter(*result.m_value.array), len, [this]() + { + return parse_cbor(); + }); + return result; + } + + template + BasicJsonType get_cbor_object(const NumberType len) + { + BasicJsonType result = value_t::object; + std::generate_n(std::inserter(*result.m_value.object, + result.m_value.object->end()), + len, [this]() + { + get(); + auto key = get_cbor_string(); + auto val = parse_cbor(); + return std::make_pair(std::move(key), std::move(val)); + }); + return result; + } + /*! @brief reads a MessagePack string @@ -5415,26 +5328,22 @@ class binary_reader case 0xbe: case 0xbf: { - const auto len = static_cast(current & 0x1f); - return get_string(len); + return get_string(current & 0x1f); } case 0xd9: // str 8 { - const auto len = static_cast(get_number()); - return get_string(len); + return get_string(get_number()); } case 0xda: // str 16 { - const auto len = static_cast(get_number()); - return get_string(len); + return get_string(get_number()); } case 0xdb: // str 32 { - const auto len = static_cast(get_number()); - return get_string(len); + return get_string(get_number()); } default: @@ -5447,6 +5356,33 @@ class binary_reader } } + template + BasicJsonType get_msgpack_array(const NumberType len) + { + BasicJsonType result = value_t::array; + std::generate_n(std::back_inserter(*result.m_value.array), len, [this]() + { + return parse_msgpack(); + }); + return result; + } + + template + BasicJsonType get_msgpack_object(const NumberType len) + { + BasicJsonType result = value_t::object; + std::generate_n(std::inserter(*result.m_value.object, + result.m_value.object->end()), + len, [this]() + { + get(); + auto key = get_msgpack_string(); + auto val = parse_msgpack(); + return std::make_pair(std::move(key), std::move(val)); + }); + return result; + } + /*! @brief check if input ended @throw parse_error.110 if input ended @@ -6091,6 +6027,7 @@ class serializer // last element assert(i != val.m_value.object->cend()); + assert(std::next(i) == val.m_value.object->cend()); o->write_characters(indent_string.c_str(), new_indent); o->write_character('\"'); dump_escaped(i->first, ensure_ascii); @@ -6118,6 +6055,7 @@ class serializer // last element assert(i != val.m_value.object->cend()); + assert(std::next(i) == val.m_value.object->cend()); o->write_character('\"'); dump_escaped(i->first, ensure_ascii); o->write_characters("\":", 2); @@ -6338,7 +6276,7 @@ class serializer if (bytes == 3) { // codepoints that need 4 bytes (i.e., 3 additional - // bytes) in UTF-8 needs a surrogate pair when \u + // bytes) in UTF-8 need a surrogate pair when \u // escaping is used: from 4 bytes to \uxxxx\uxxxx // (12 bytes) res += (12 - bytes - 1); @@ -7244,6 +7182,9 @@ class basic_json friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; + template + friend class ::nlohmann::detail::binary_reader; + /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; @@ -12858,6 +12799,10 @@ class basic_json @note The mapping is **complete** in the sense that any JSON value type can be converted to a CBOR value. + @note If NaN or Infinity are stored inside a JSON number, they are + serialized properly. This behavior differs from the @ref dump() + function which serializes NaN or Infinity to `null`. + @note The following CBOR types are not used in the conversion: - byte strings (0x40..0x5f) - UTF-8 strings terminated by "break" (0x7f) @@ -12885,7 +12830,7 @@ class basic_json @sa http://cbor.io @sa @ref from_cbor(const std::vector&, const size_t) for the analogous deserialization - @sa @ref to_msgpack(const basic_json& for the related MessagePack format + @sa @ref to_msgpack(const basic_json&) for the related MessagePack format @since version 2.0.9 */ @@ -12965,6 +12910,10 @@ class basic_json @note Any MessagePack output created @ref to_msgpack can be successfully parsed by @ref from_msgpack. + @note If NaN or Infinity are stored inside a JSON number, they are + serialized properly. This behavior differs from the @ref dump() + function which serializes NaN or Infinity to `null`. + @param[in] j JSON value to serialize @return MessagePack serialization as byte vector @@ -14324,7 +14273,7 @@ struct hash /// @note: do not remove the space after '<', /// see https://github.com/nlohmann/json/pull/679 template<> -struct less< ::nlohmann::detail::value_t> +struct less< ::nlohmann::detail::value_t> { /*! @brief compare two value_t enum values