🚧 further UBJSON

This commit is contained in:
Niels Lohmann 2018-01-08 18:54:17 +01:00
parent c9938ea838
commit 126ce2e56c
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 83 additions and 8 deletions

View file

@ -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<int8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
{
return 'i';
}
else if ((std::numeric_limits<uint8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
{
return 'U';
}
else if ((std::numeric_limits<int16_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
{
return 'I';
}
else if (-(std::numeric_limits<int32_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
{
return 'l';
}
else if ((std::numeric_limits<int64_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)())
{
return 'L';
}
break;
}
case value_t::number_unsigned:
{
if ((std::numeric_limits<int8_t>::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits<int8_t>::max)())
{
return 'i';
}
else if ((std::numeric_limits<uint8_t>::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{
return 'U';
}
else if ((std::numeric_limits<int16_t>::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits<int16_t>::max)())
{
return 'I';
}
else if (-(std::numeric_limits<int32_t>::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits<int32_t>::max)())
{
return 'l';
}
else if ((std::numeric_limits<int64_t>::min)() <= j.m_value.number_unsigned and j.m_value.number_unsigned <= (std::numeric_limits<int64_t>::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<BasicJsonType>::little_endianess();

View file

@ -414,10 +414,8 @@ TEST_CASE("UBJSON")
SECTION("2147483648..9223372036854775807 (int64)")
{
for (uint64_t i :
{
2147483648ul, 9223372036854775807ul
})
std::vector<uint64_t> 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<uint64_t> v = {2147483648ul, 9223372036854775807ul};
for (uint64_t i : v)
{
CAPTURE(i);