From b7cc47089f7fc0333d163a0f728ef78f6983fd01 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 16 Jul 2021 09:27:18 +0200 Subject: [PATCH] :zap: avoid string in case of empty objects --- .../nlohmann/detail/input/binary_reader.hpp | 47 ++++++++++--------- single_include/nlohmann/json.hpp | 47 ++++++++++--------- 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 9b9fedfdfc..f02a15e8f7 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -1082,38 +1082,41 @@ class binary_reader return false; } - string_t key; - if (len != std::size_t(-1)) + if (len != 0) { - for (std::size_t i = 0; i < len; ++i) + string_t key; + if (len != std::size_t(-1)) { - get(); - if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + for (std::size_t i = 0; i < len; ++i) { - return false; - } + get(); + if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + { + return false; + } - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) - { - return false; + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) + { + return false; + } + key.clear(); } - key.clear(); } - } - else - { - while (get() != 0xFF) + else { - if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + while (get() != 0xFF) { - return false; - } + if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + { + return false; + } - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) - { - return false; + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) + { + return false; + } + key.clear(); } - key.clear(); } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 942b600e75..5bcd9308fc 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -9298,38 +9298,41 @@ class binary_reader return false; } - string_t key; - if (len != std::size_t(-1)) + if (len != 0) { - for (std::size_t i = 0; i < len; ++i) + string_t key; + if (len != std::size_t(-1)) { - get(); - if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + for (std::size_t i = 0; i < len; ++i) { - return false; - } + get(); + if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + { + return false; + } - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) - { - return false; + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) + { + return false; + } + key.clear(); } - key.clear(); } - } - else - { - while (get() != 0xFF) + else { - if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + while (get() != 0xFF) { - return false; - } + if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) + { + return false; + } - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) - { - return false; + if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) + { + return false; + } + key.clear(); } - key.clear(); } }