:white_check_mark: added more tests

parent 31bfabc4
...@@ -4549,7 +4549,7 @@ class binary_reader ...@@ -4549,7 +4549,7 @@ class binary_reader
const auto res = parse_ubjson_internal(); const auto res = parse_ubjson_internal();
if (strict) if (strict)
{ {
get(); get_ignore_noop();
check_eof(true); check_eof(true);
} }
return res; return res;
...@@ -5591,11 +5591,9 @@ class binary_reader ...@@ -5591,11 +5591,9 @@ class binary_reader
get_ignore_noop(); get_ignore_noop();
switch (current) if (current == '$')
{ {
case '$': get(); // must not ignore 'N', because 'N' maybe the type
{
get_ignore_noop();
check_eof(); check_eof();
tc = current; tc = current;
...@@ -5605,21 +5603,13 @@ class binary_reader ...@@ -5605,21 +5603,13 @@ class binary_reader
assert(false); assert(false);
} }
sz = parse_ubjson_internal(); sz = parse_ubjson_internal();
break;
} }
else if (current == '#')
case '#':
{ {
sz = parse_ubjson_internal(); sz = parse_ubjson_internal();
break;
}
default:
break;
} }
return std::make_pair(sz, tc); return std::make_pair(sz, tc);
} }
BasicJsonType get_ubjson_value(const int prefix) BasicJsonType get_ubjson_value(const int prefix)
...@@ -5656,6 +5646,7 @@ class binary_reader ...@@ -5656,6 +5646,7 @@ class binary_reader
{ {
get(); get();
check_eof(); check_eof();
assert(0 <= current and current <= 127);
return std::string(1, static_cast<char>(current)); return std::string(1, static_cast<char>(current));
} }
...@@ -5682,17 +5673,23 @@ class binary_reader ...@@ -5682,17 +5673,23 @@ class binary_reader
const auto size_and_type = get_ubjson_size_type(); const auto size_and_type = get_ubjson_size_type();
if (size_and_type.first != std::string::npos) if (size_and_type.first != std::string::npos)
{
for (std::size_t i = 0; i < size_and_type.first; ++i)
{ {
if (size_and_type.second != 0) if (size_and_type.second != 0)
{ {
result.push_back(get_ubjson_value(size_and_type.second)); if (size_and_type.second != 'N')
std::generate_n(std::back_inserter(*result.m_value.array),
size_and_type.first, [this, size_and_type]()
{
return get_ubjson_value(size_and_type.second);
});
} }
else else
{ {
result.push_back(parse_ubjson_internal()); std::generate_n(std::back_inserter(*result.m_value.array),
} size_and_type.first, [this]()
{
return parse_ubjson_internal();
});
} }
} }
else else
...@@ -5714,17 +5711,28 @@ class binary_reader ...@@ -5714,17 +5711,28 @@ class binary_reader
if (size_and_type.first != std::string::npos) if (size_and_type.first != std::string::npos)
{ {
for (std::size_t i = 0; i < size_and_type.first; ++i)
{
auto key = get_ubjson_string();
if (size_and_type.second != 0) if (size_and_type.second != 0)
{ {
result[std::move(key)] = get_ubjson_value(size_and_type.second); if (size_and_type.second != 'N')
std::generate_n(std::inserter(*result.m_value.object,
result.m_value.object->end()),
size_and_type.first, [this, size_and_type]()
{
auto key = get_ubjson_string();
auto val = get_ubjson_value(size_and_type.second);
return std::make_pair(std::move(key), std::move(val));
});
} }
else else
{ {
result[std::move(key)] = parse_ubjson_internal(); std::generate_n(std::inserter(*result.m_value.object,
} result.m_value.object->end()),
size_and_type.first, [this]()
{
auto key = get_ubjson_string();
auto val = parse_ubjson_internal();
return std::make_pair(std::move(key), std::move(val));
});
} }
} }
else else
...@@ -6282,10 +6290,8 @@ class binary_writer ...@@ -6282,10 +6290,8 @@ class binary_writer
@param[in] use_type whether to use '$' prefixes (optimized format) @param[in] use_type whether to use '$' prefixes (optimized format)
@param[in] add_prefix whether prefixes need to be used for this value @param[in] add_prefix whether prefixes need to be used for this value
*/ */
void write_ubjson(const BasicJsonType& j, void write_ubjson(const BasicJsonType& j, const bool use_count,
const bool use_count = false, const bool use_type, const bool add_prefix = true)
const bool use_type = false,
const bool add_prefix = true)
{ {
switch (j.type()) switch (j.type())
{ {
...@@ -6348,6 +6354,7 @@ class binary_writer ...@@ -6348,6 +6354,7 @@ class binary_writer
bool prefix_required = true; bool prefix_required = true;
if (use_type and not j.m_value.array->empty()) if (use_type and not j.m_value.array->empty())
{ {
assert(use_count);
const char first_prefix = ubjson_prefix(j.front()); const char first_prefix = ubjson_prefix(j.front());
const bool same_prefix = std::all_of(j.begin() + 1, j.end(), const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
[this, first_prefix](const BasicJsonType & v) [this, first_prefix](const BasicJsonType & v)
...@@ -6392,6 +6399,7 @@ class binary_writer ...@@ -6392,6 +6399,7 @@ class binary_writer
bool prefix_required = true; bool prefix_required = true;
if (use_type and not j.m_value.object->empty()) if (use_type and not j.m_value.object->empty())
{ {
assert(use_count);
const char first_prefix = ubjson_prefix(j.front()); const char first_prefix = ubjson_prefix(j.front());
const bool same_prefix = std::all_of(j.begin(), j.end(), const bool same_prefix = std::all_of(j.begin(), j.end(),
[this, first_prefix](const BasicJsonType & v) [this, first_prefix](const BasicJsonType & v)
...@@ -6463,7 +6471,8 @@ class binary_writer ...@@ -6463,7 +6471,8 @@ class binary_writer
} }
template<typename NumberType> template<typename NumberType>
void write_number_with_ubjson_prefix(const NumberType n, const bool add_prefix) void write_number_with_ubjson_prefix(const NumberType n,
const bool add_prefix)
{ {
if (std::is_floating_point<NumberType>::value) if (std::is_floating_point<NumberType>::value)
{ {
...@@ -6608,23 +6617,23 @@ class binary_writer ...@@ -6608,23 +6617,23 @@ class binary_writer
case value_t::number_unsigned: 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)()) if (j.m_value.number_unsigned <= (std::numeric_limits<int8_t>::max)())
{ {
return 'i'; 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)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
{ {
return 'U'; 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)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<int16_t>::max)())
{ {
return 'I'; 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)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<int32_t>::max)())
{ {
return 'l'; 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)()) else if (j.m_value.number_unsigned <= (std::numeric_limits<int64_t>::max)())
{ {
return 'L'; return 'L';
} }
...@@ -14103,21 +14112,24 @@ class basic_json ...@@ -14103,21 +14112,24 @@ class basic_json
binary_writer<char>(o).write_msgpack(j); binary_writer<char>(o).write_msgpack(j);
} }
static std::vector<uint8_t> to_ubjson(const basic_json& j) static std::vector<uint8_t> to_ubjson(const basic_json& j,
const bool use_size = false, const bool use_type = false)
{ {
std::vector<uint8_t> result; std::vector<uint8_t> result;
to_ubjson(j, result); to_ubjson(j, result, use_size, use_type);
return result; return result;
} }
static void to_ubjson(const basic_json& j, detail::output_adapter<uint8_t> o) static void to_ubjson(const basic_json& j, detail::output_adapter<uint8_t> o,
const bool use_size = false, const bool use_type = false)
{ {
binary_writer<uint8_t>(o).write_ubjson(j); binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
} }
static void to_ubjson(const basic_json& j, detail::output_adapter<char> o) static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
const bool use_size = false, const bool use_type = false)
{ {
binary_writer<char>(o).write_ubjson(j); binary_writer<char>(o).write_ubjson(j, use_size, use_type);
} }
/*! /*!
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment