From 126ce2e56c8084a480937e2ec887ecc19d7872ab Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 8 Jan 2018 18:54:17 +0100 Subject: [PATCH] :construction: further UBJSON --- src/json.hpp | 79 ++++++++++++++++++++++++++++++++++++++++ test/src/unit-ubjson.cpp | 12 ++---- 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index a356bdef4..2aef15948 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -6395,6 +6395,85 @@ class binary_writer } } + char ubjson_prefix(const BasicJsonType& j) + { + switch (j.type()) + { + case value_t::null: + return 'Z'; + + case value_t::boolean: + return j.m_value.boolean ? 'T' : 'F'; + + case value_t::number_integer: + { + if ((std::numeric_limits::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits::max)()) + { + return 'i'; + } + else if ((std::numeric_limits::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits::max)()) + { + return 'U'; + } + else if ((std::numeric_limits::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits::max)()) + { + return 'I'; + } + else if (-(std::numeric_limits::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits::max)()) + { + return 'l'; + } + else if ((std::numeric_limits::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits::max)()) + { + return 'L'; + } + break; + } + + case value_t::number_unsigned: + { + if ((std::numeric_limits::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits::max)()) + { + return 'i'; + } + else if ((std::numeric_limits::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits::max)()) + { + return 'U'; + } + else if ((std::numeric_limits::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits::max)()) + { + return 'I'; + } + else if (-(std::numeric_limits::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits::max)()) + { + return 'l'; + } + else if ((std::numeric_limits::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits::max)()) + { + return 'L'; + } + break; + } + + case value_t::number_float: + return 'D'; + + case value_t::string: + return 'S'; + + case value_t::array: + return '['; + + case value_t::object: + return '{'; + + default: + break; + } + + return '\0'; + } + private: /// whether we can assume little endianess const bool is_little_endian = binary_reader::little_endianess(); diff --git a/test/src/unit-ubjson.cpp b/test/src/unit-ubjson.cpp index ce1b3c386..7e1ffefce 100644 --- a/test/src/unit-ubjson.cpp +++ b/test/src/unit-ubjson.cpp @@ -414,10 +414,8 @@ TEST_CASE("UBJSON") SECTION("2147483648..9223372036854775807 (int64)") { - for (uint64_t i : - { - 2147483648ul, 9223372036854775807ul - }) + std::vector v = {2147483648ul, 9223372036854775807ul}; + for (uint64_t i : v) { CAPTURE(i); @@ -605,10 +603,8 @@ TEST_CASE("UBJSON") SECTION("2147483648..9223372036854775807 (int64)") { - for (uint64_t i : - { - 2147483648ul, 9223372036854775807ul - }) + std::vector v = {2147483648ul, 9223372036854775807ul}; + for (uint64_t i : v) { CAPTURE(i);