🚧 support for UBJSON high-precision numbers #2286

This commit is contained in:
Niels Lohmann 2020-07-20 09:42:37 +02:00
parent b1da58b76b
commit 7cf2fe149c
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 1696 additions and 1627 deletions

View file

@ -15,6 +15,7 @@
#include <nlohmann/detail/exceptions.hpp> #include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/input/input_adapters.hpp> #include <nlohmann/detail/input/input_adapters.hpp>
#include <nlohmann/detail/input/json_sax.hpp> #include <nlohmann/detail/input/json_sax.hpp>
#include <nlohmann/detail/input/lexer.hpp>
#include <nlohmann/detail/macro_scope.hpp> #include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/meta/is_sax.hpp> #include <nlohmann/detail/meta/is_sax.hpp>
#include <nlohmann/detail/value_t.hpp> #include <nlohmann/detail/value_t.hpp>
@ -2000,6 +2001,35 @@ class binary_reader
return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast<number_float_t>(number), ""); return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast<number_float_t>(number), "");
} }
case 'H':
{
std::size_t size{};
auto res = get_ubjson_size_value(size);
std::string s;
for (int i = 0; i < size; ++i)
{
get();
s.push_back(current);
}
auto ia = detail::input_adapter(std::forward<std::string>(s));
auto l = detail::lexer<BasicJsonType, decltype(ia)>(std::move(ia), false);
auto result = l.scan();
switch (result)
{
case detail::lexer_base<BasicJsonType>::token_type::value_integer:
return sax->number_integer(l.get_number_integer());
case detail::lexer_base<BasicJsonType>::token_type::value_unsigned:
return sax->number_unsigned(l.get_number_unsigned());
case detail::lexer_base<BasicJsonType>::token_type::value_float:
return sax->number_float(l.get_number_float(), std::move(s));
default:
return sax->parse_error(chars_read, s, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "invalid number", "number")));
}
}
case 'C': // char case 'C': // char
{ {
get(); get();

File diff suppressed because it is too large Load diff

View file

@ -767,6 +767,14 @@ TEST_CASE("UBJSON")
CHECK(json::from_ubjson(result, true, false) == j); CHECK(json::from_ubjson(result, true, false) == j);
} }
} }
SECTION("high-precision number")
{
std::vector<uint8_t> vec = {'H', 'i', 0x16, '3', '.', '1', '4', '1', '5', '9', '2', '6', '5', '3', '5', '8', '9', '7', '9', '3', '2', '3', '8', '4', '6'};
const auto j = json::from_ubjson(vec);
CHECK(j.is_number_float());
CHECK(j.dump() == "3.141592653589793");
}
} }
SECTION("string") SECTION("string")
@ -2369,7 +2377,7 @@ TEST_CASE("all UBJSON first bytes")
// these bytes will fail immediately with exception parse_error.112 // these bytes will fail immediately with exception parse_error.112
std::set<uint8_t> supported = std::set<uint8_t> supported =
{ {
'T', 'F', 'Z', 'U', 'i', 'I', 'l', 'L', 'd', 'D', 'C', 'S', '[', '{', 'N' 'T', 'F', 'Z', 'U', 'i', 'I', 'l', 'L', 'd', 'D', 'C', 'S', '[', '{', 'N', 'H'
}; };
for (auto i = 0; i < 256; ++i) for (auto i = 0; i < 256; ++i)