From b38ecb5ca908a3151c0c53435fe9069787e62fc1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 8 Jul 2017 20:31:13 +0200 Subject: [PATCH] :hammer: simplified binary write Also added some comments and improved the documentation. --- src/json.hpp | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index c312b3e8..bcec446a 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -9897,22 +9897,23 @@ class basic_json /* @brief read a number from the input - @tparam T the type of the number + @tparam NumberType the type of the number - @return number of type @a T + @return number of type @a NumberType @note This function needs to respect the system's endianess, because bytes in CBOR and MessagePack are stored in network order (big endian) and therefore need reordering on little endian systems. - @throw parse_error.110 if input has less than `sizeof(T)` bytes + @throw parse_error.110 if input has less than `sizeof(NumberType)` + bytes */ - template - T get_number() + template + NumberType get_number() { // step 1: read input into array with system's byte order - std::array vec; - for (size_t i = 0; i < sizeof(T); ++i) + std::array vec; + for (size_t i = 0; i < sizeof(NumberType); ++i) { get(); check_eof(); @@ -9920,7 +9921,7 @@ class basic_json // reverse byte order prior to conversion if necessary if (is_little_endian) { - vec[sizeof(T) - i - 1] = static_cast(current); + vec[sizeof(NumberType) - i - 1] = static_cast(current); } else { @@ -9929,8 +9930,8 @@ class basic_json } // step 2: convert array into number of type T and return - T result; - std::memcpy(&result, vec.data(), sizeof(T)); + NumberType result; + std::memcpy(&result, vec.data(), sizeof(NumberType)); return result; } @@ -9939,6 +9940,10 @@ class basic_json @param[in] len number of bytes to read + @note We can not reserve @a len bytes for the result, because @a len + may be too large. Usually, @ref check_eof() detects the end of + the input before we run out of string memory. + @return string created by reading @a len bytes @throw parse_error.110 if input has less than @a len bytes @@ -10658,33 +10663,28 @@ class basic_json /* @brief write a number to output input - @param[in] n number of type @a T - @tparam T the type of the number + @param[in] n number of type @a NumberType + @tparam NumberType the type of the number @note This function needs to respect the system's endianess, because bytes in CBOR and MessagePack are stored in network order (big endian) and therefore need reordering on little endian systems. */ - template - void write_number(T n) + template + void write_number(NumberType n) { - // step 1: write number to array of length T - std::array vec; - std::memcpy(vec.data(), &n, sizeof(T)); + // step 1: write number to array of length NumberType + std::array vec; + std::memcpy(vec.data(), &n, sizeof(NumberType)); // step 2: write array to output (with possible reordering) - for (size_t i = 0; i < sizeof(T); ++i) + if (is_little_endian) { // reverse byte order prior to conversion if necessary - if (is_little_endian) - { - oa->write_character(vec[sizeof(T) - i - 1]); - } - else - { - oa->write_character(vec[i]); // LCOV_EXCL_LINE - } + std::reverse(vec.begin(), vec.end()); } + + oa->write_characters(vec.data(), sizeof(NumberType)); } private: