1
0
mirror of https://github.com/nlohmann/json synced 2024-11-10 06:20:06 +00:00

Merge pull request #2099 from nlohmann/issue2082

Add tests for binary values
This commit is contained in:
Niels Lohmann 2020-05-13 07:56:11 +02:00 committed by GitHub
commit 9265ca7a08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 745 additions and 310 deletions

View File

@ -1500,11 +1500,6 @@ class binary_reader
*/
bool get_msgpack_binary(internal_binary_t& result)
{
if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "binary")))
{
return false;
}
switch (current)
{
case 0xC4: // bin 8
@ -1582,11 +1577,8 @@ class binary_reader
return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 16, result);
}
default:
{
auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected binary type specification (0xC4-0xC9, 0xD4-0xD8); last byte: 0x" + last_token, "binary")));
}
default: // LCOV_EXCL_LINE
assert(false); // LCOV_EXCL_LINE
}
}
@ -2211,7 +2203,7 @@ class binary_reader
{
success = false;
}
return static_cast<uint8_t>(current);
return static_cast<std::uint8_t>(current);
});
return success;
}

View File

@ -214,7 +214,7 @@ class json_sax_dom_parser
bool binary(binary_t& val)
{
handle_binary(val);
handle_value(BasicJsonType::binary_array(std::move(val)));
return true;
}
@ -327,36 +327,6 @@ class json_sax_dom_parser
return object_element;
}
/*!
@invariant If the ref stack is empty, then the passed value will be the new
root.
@invariant If the ref stack contains a value, then it is an array or an
object to which we can add elements
*/
template<typename BinaryValue>
JSON_HEDLEY_RETURNS_NON_NULL
BasicJsonType* handle_binary(BinaryValue&& v)
{
if (ref_stack.empty())
{
root = BasicJsonType::binary_array(std::forward<BinaryValue>(v));
return &root;
}
assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
if (ref_stack.back()->is_array())
{
ref_stack.back()->m_value.array->emplace_back(BasicJsonType::binary_array(std::forward<BinaryValue>(v)));
return &(ref_stack.back()->m_value.array->back());
}
assert(ref_stack.back()->is_object());
assert(object_element);
*object_element = BasicJsonType::binary_array(std::forward<BinaryValue>(v));
return object_element;
}
/// the parsed JSON value
BasicJsonType& root;
/// stack to model hierarchy of values
@ -434,7 +404,7 @@ class json_sax_dom_callback_parser
bool binary(binary_t& val)
{
handle_value(val);
handle_value(BasicJsonType::binary_array(val));
return true;
}

View File

@ -251,27 +251,6 @@ class serializer
{
o->write_characters("b[]", 3);
}
else if (pretty_print)
{
o->write_characters("b[", 2);
for (auto i = val.m_value.binary->cbegin();
i != val.m_value.binary->cend() - 1; ++i)
{
dump_integer(*i);
o->write_character(',');
if (std::distance(val.m_value.binary->cbegin(), i) % 16 == 0)
{
o->write_character('\n');
}
else
{
o->write_character(' ');
}
}
dump_integer(val.m_value.binary->back());
o->write_character(']');
}
else
{
o->write_characters("b[", 2);

View File

@ -876,7 +876,6 @@ class basic_json
@since version 3.8.0
*/
using binary_t = BinaryType;
/*!
@ -890,9 +889,25 @@ class basic_json
struct internal_binary_t : public BinaryType
{
using BinaryType::BinaryType;
internal_binary_t() noexcept(noexcept(BinaryType())) : BinaryType() {}
internal_binary_t(BinaryType const& bint) noexcept(noexcept(BinaryType(bint))) : BinaryType(bint) {}
internal_binary_t(BinaryType&& bint) noexcept(noexcept(BinaryType(std::move(bint)))) : BinaryType(std::move(bint)) {}
internal_binary_t() noexcept(noexcept(BinaryType()))
: BinaryType()
{}
internal_binary_t(const BinaryType& bint) noexcept(noexcept(BinaryType(bint)))
: BinaryType(bint)
{}
internal_binary_t(BinaryType&& bint) noexcept(noexcept(BinaryType(std::move(bint))))
: BinaryType(std::move(bint))
{}
internal_binary_t(const BinaryType& bint, std::uint8_t st) noexcept(noexcept(BinaryType(bint)))
: BinaryType(bint)
, subtype(st)
, has_subtype(true)
{}
internal_binary_t(BinaryType&& bint, std::uint8_t st) noexcept(noexcept(BinaryType(std::move(bint))))
: BinaryType(std::move(bint))
, subtype(st)
, has_subtype(true)
{}
// TOOD: If minimum C++ version is ever bumped to C++17, this field
// deserves to be a std::optional
@ -1099,6 +1114,18 @@ class basic_json
binary = create<internal_binary_t>(std::move(value));
}
/// constructor for binary arrays (internal type)
json_value(const internal_binary_t& value)
{
binary = create<internal_binary_t>(value);
}
/// constructor for rvalue binary arrays (internal type)
json_value(internal_binary_t&& value)
{
binary = create<internal_binary_t>(std::move(value));
}
void destroy(value_t t) noexcept
{
// flatten the current json_value to a heap-allocated stack
@ -1204,6 +1231,7 @@ class basic_json
assert(m_type != value_t::object or m_value.object != nullptr);
assert(m_type != value_t::array or m_value.array != nullptr);
assert(m_type != value_t::string or m_value.string != nullptr);
assert(m_type != value_t::binary or m_value.binary != nullptr);
}
public:
@ -1655,7 +1683,7 @@ class basic_json
@since version 3.8.0
*/
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary_array(binary_t const& init)
static basic_json binary_array(const binary_t& init)
{
auto res = basic_json();
res.m_type = value_t::binary;
@ -1663,6 +1691,15 @@ class basic_json
return res;
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary_array(const binary_t& init, std::uint8_t subtype)
{
auto res = basic_json();
res.m_type = value_t::binary;
res.m_value = internal_binary_t(init, subtype);
return res;
}
/*!
@brief explicitly create a binary array from an already constructed rvalue
copy of its base type
@ -1699,6 +1736,15 @@ class basic_json
return res;
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary_array(binary_t&& init, std::uint8_t subtype)
{
auto res = basic_json();
res.m_type = value_t::binary;
res.m_value = internal_binary_t(std::move(init), subtype);
return res;
}
/*!
@brief explicitly create an array from an initializer list
@ -1956,8 +2002,7 @@ class basic_json
case value_t::binary:
{
m_value.binary = create<internal_binary_t>(first.m_it.binary_iterator,
last.m_it.binary_iterator);
m_value = *first.m_object->m_value.binary;
break;
}
@ -2748,7 +2793,7 @@ class basic_json
/// get a pointer to the value (binary)
binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
{
return is_binary() ? m_value.binary : nullptr;
return is_binary() ? reinterpret_cast<binary_t*>(m_value.binary) : nullptr;
}
/// get a pointer to the value (binary)
@ -6015,6 +6060,39 @@ class basic_json
}
}
/*!
@brief exchanges the values
Exchanges the contents of a JSON string with those of @a other. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
invalidated.
@param[in,out] other binary to exchange the contents with
@throw type_error.310 when JSON value is not a string; example: `"cannot
use swap() with boolean"`
@complexity Constant.
@liveexample{The example below shows how strings can be swapped with
`swap()`.,swap__binary_t}
@since version 3.8.0
*/
void swap(binary_t& other)
{
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
}
}
/// @}
public:
@ -6277,7 +6355,7 @@ class basic_json
return (lhs.m_value.number_float) < (rhs.m_value.number_float);
case value_t::binary:
return (lhs.m_value.binary) < (rhs.m_value.binary);
return (*lhs.m_value.binary) < (*rhs.m_value.binary);
default:
return false;

View File

@ -4881,7 +4881,7 @@ class json_sax_dom_parser
bool binary(binary_t& val)
{
handle_binary(val);
handle_value(BasicJsonType::binary_array(std::move(val)));
return true;
}
@ -4994,36 +4994,6 @@ class json_sax_dom_parser
return object_element;
}
/*!
@invariant If the ref stack is empty, then the passed value will be the new
root.
@invariant If the ref stack contains a value, then it is an array or an
object to which we can add elements
*/
template<typename BinaryValue>
JSON_HEDLEY_RETURNS_NON_NULL
BasicJsonType* handle_binary(BinaryValue&& v)
{
if (ref_stack.empty())
{
root = BasicJsonType::binary_array(std::forward<BinaryValue>(v));
return &root;
}
assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
if (ref_stack.back()->is_array())
{
ref_stack.back()->m_value.array->emplace_back(BasicJsonType::binary_array(std::forward<BinaryValue>(v)));
return &(ref_stack.back()->m_value.array->back());
}
assert(ref_stack.back()->is_object());
assert(object_element);
*object_element = BasicJsonType::binary_array(std::forward<BinaryValue>(v));
return object_element;
}
/// the parsed JSON value
BasicJsonType& root;
/// stack to model hierarchy of values
@ -5101,7 +5071,7 @@ class json_sax_dom_callback_parser
bool binary(binary_t& val)
{
handle_value(val);
handle_value(BasicJsonType::binary_array(val));
return true;
}
@ -7057,11 +7027,6 @@ class binary_reader
*/
bool get_msgpack_binary(internal_binary_t& result)
{
if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "binary")))
{
return false;
}
switch (current)
{
case 0xC4: // bin 8
@ -7139,11 +7104,8 @@ class binary_reader
return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 16, result);
}
default:
{
auto last_token = get_token_string();
return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected binary type specification (0xC4-0xC9, 0xD4-0xD8); last byte: 0x" + last_token, "binary")));
}
default: // LCOV_EXCL_LINE
assert(false); // LCOV_EXCL_LINE
}
}
@ -7768,7 +7730,7 @@ class binary_reader
{
success = false;
}
return static_cast<uint8_t>(current);
return static_cast<std::uint8_t>(current);
});
return success;
}
@ -14905,27 +14867,6 @@ class serializer
{
o->write_characters("b[]", 3);
}
else if (pretty_print)
{
o->write_characters("b[", 2);
for (auto i = val.m_value.binary->cbegin();
i != val.m_value.binary->cend() - 1; ++i)
{
dump_integer(*i);
o->write_character(',');
if (std::distance(val.m_value.binary->cbegin(), i) % 16 == 0)
{
o->write_character('\n');
}
else
{
o->write_character(' ');
}
}
dump_integer(val.m_value.binary->back());
o->write_character(']');
}
else
{
o->write_characters("b[", 2);
@ -16383,7 +16324,6 @@ class basic_json
@since version 3.8.0
*/
using binary_t = BinaryType;
/*!
@ -16397,9 +16337,25 @@ class basic_json
struct internal_binary_t : public BinaryType
{
using BinaryType::BinaryType;
internal_binary_t() noexcept(noexcept(BinaryType())) : BinaryType() {}
internal_binary_t(BinaryType const& bint) noexcept(noexcept(BinaryType(bint))) : BinaryType(bint) {}
internal_binary_t(BinaryType&& bint) noexcept(noexcept(BinaryType(std::move(bint)))) : BinaryType(std::move(bint)) {}
internal_binary_t() noexcept(noexcept(BinaryType()))
: BinaryType()
{}
internal_binary_t(const BinaryType& bint) noexcept(noexcept(BinaryType(bint)))
: BinaryType(bint)
{}
internal_binary_t(BinaryType&& bint) noexcept(noexcept(BinaryType(std::move(bint))))
: BinaryType(std::move(bint))
{}
internal_binary_t(const BinaryType& bint, std::uint8_t st) noexcept(noexcept(BinaryType(bint)))
: BinaryType(bint)
, subtype(st)
, has_subtype(true)
{}
internal_binary_t(BinaryType&& bint, std::uint8_t st) noexcept(noexcept(BinaryType(std::move(bint))))
: BinaryType(std::move(bint))
, subtype(st)
, has_subtype(true)
{}
// TOOD: If minimum C++ version is ever bumped to C++17, this field
// deserves to be a std::optional
@ -16606,6 +16562,18 @@ class basic_json
binary = create<internal_binary_t>(std::move(value));
}
/// constructor for binary arrays (internal type)
json_value(const internal_binary_t& value)
{
binary = create<internal_binary_t>(value);
}
/// constructor for rvalue binary arrays (internal type)
json_value(internal_binary_t&& value)
{
binary = create<internal_binary_t>(std::move(value));
}
void destroy(value_t t) noexcept
{
// flatten the current json_value to a heap-allocated stack
@ -16711,6 +16679,7 @@ class basic_json
assert(m_type != value_t::object or m_value.object != nullptr);
assert(m_type != value_t::array or m_value.array != nullptr);
assert(m_type != value_t::string or m_value.string != nullptr);
assert(m_type != value_t::binary or m_value.binary != nullptr);
}
public:
@ -17162,7 +17131,7 @@ class basic_json
@since version 3.8.0
*/
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary_array(binary_t const& init)
static basic_json binary_array(const binary_t& init)
{
auto res = basic_json();
res.m_type = value_t::binary;
@ -17170,6 +17139,15 @@ class basic_json
return res;
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary_array(const binary_t& init, std::uint8_t subtype)
{
auto res = basic_json();
res.m_type = value_t::binary;
res.m_value = internal_binary_t(init, subtype);
return res;
}
/*!
@brief explicitly create a binary array from an already constructed rvalue
copy of its base type
@ -17206,6 +17184,15 @@ class basic_json
return res;
}
JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary_array(binary_t&& init, std::uint8_t subtype)
{
auto res = basic_json();
res.m_type = value_t::binary;
res.m_value = internal_binary_t(std::move(init), subtype);
return res;
}
/*!
@brief explicitly create an array from an initializer list
@ -17463,8 +17450,7 @@ class basic_json
case value_t::binary:
{
m_value.binary = create<internal_binary_t>(first.m_it.binary_iterator,
last.m_it.binary_iterator);
m_value = *first.m_object->m_value.binary;
break;
}
@ -18255,7 +18241,7 @@ class basic_json
/// get a pointer to the value (binary)
binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
{
return is_binary() ? m_value.binary : nullptr;
return is_binary() ? reinterpret_cast<binary_t*>(m_value.binary) : nullptr;
}
/// get a pointer to the value (binary)
@ -21522,6 +21508,39 @@ class basic_json
}
}
/*!
@brief exchanges the values
Exchanges the contents of a JSON string with those of @a other. Does not
invoke any move, copy, or swap operations on individual elements. All
iterators and references remain valid. The past-the-end iterator is
invalidated.
@param[in,out] other binary to exchange the contents with
@throw type_error.310 when JSON value is not a string; example: `"cannot
use swap() with boolean"`
@complexity Constant.
@liveexample{The example below shows how strings can be swapped with
`swap()`.,swap__binary_t}
@since version 3.8.0
*/
void swap(binary_t& other)
{
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
}
}
/// @}
public:
@ -21784,7 +21803,7 @@ class basic_json
return (lhs.m_value.number_float) < (rhs.m_value.number_float);
case value_t::binary:
return (lhs.m_value.binary) < (rhs.m_value.binary);
return (*lhs.m_value.binary) < (*rhs.m_value.binary);
default:
return false;

View File

@ -106,7 +106,7 @@ TEST_CASE("BSON")
SECTION("string length must be at least 1")
{
// from https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11175
std::vector<uint8_t> v =
std::vector<std::uint8_t> v =
{
0x20, 0x20, 0x20, 0x20,
0x02,
@ -123,7 +123,7 @@ TEST_CASE("BSON")
SECTION("empty object")
{
json j = json::object();
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x05, 0x00, 0x00, 0x00, // size (little endian)
// no entries
@ -145,7 +145,7 @@ TEST_CASE("BSON")
{ "entry", true }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean
@ -169,7 +169,7 @@ TEST_CASE("BSON")
{ "entry", false }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean
@ -193,7 +193,7 @@ TEST_CASE("BSON")
{ "entry", 4.2 }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x14, 0x00, 0x00, 0x00, // size (little endian)
0x01, /// entry: double
@ -217,7 +217,7 @@ TEST_CASE("BSON")
{ "entry", "bsonstr" }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x18, 0x00, 0x00, 0x00, // size (little endian)
0x02, /// entry: string (UTF-8)
@ -241,7 +241,7 @@ TEST_CASE("BSON")
{ "entry", nullptr }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x0C, 0x00, 0x00, 0x00, // size (little endian)
0x0A, /// entry: null
@ -264,7 +264,7 @@ TEST_CASE("BSON")
{ "entry", std::int32_t{0x12345678} }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -288,7 +288,7 @@ TEST_CASE("BSON")
{ "entry", std::int64_t{0x1234567804030201} }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x14, 0x00, 0x00, 0x00, // size (little endian)
0x12, /// entry: int64
@ -312,7 +312,7 @@ TEST_CASE("BSON")
{ "entry", std::int32_t{-1} }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -336,7 +336,7 @@ TEST_CASE("BSON")
{ "entry", std::int64_t{-1} }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -361,7 +361,7 @@ TEST_CASE("BSON")
{ "entry", std::uint64_t{0x1234567804030201} }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x14, 0x00, 0x00, 0x00, // size (little endian)
0x12, /// entry: int64
@ -385,7 +385,7 @@ TEST_CASE("BSON")
{ "entry", std::uint64_t{0x42} }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x10, 0x00, 0x00, 0x00, // size (little endian)
0x10, /// entry: int32
@ -409,7 +409,7 @@ TEST_CASE("BSON")
{ "entry", json::object() }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x11, 0x00, 0x00, 0x00, // size (little endian)
0x03, /// entry: embedded document
@ -437,7 +437,7 @@ TEST_CASE("BSON")
{ "entry", json::array() }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x11, 0x00, 0x00, 0x00, // size (little endian)
0x04, /// entry: embedded document
@ -465,7 +465,7 @@ TEST_CASE("BSON")
{ "entry", json::array({1, 2, 3, 4, 5, 6, 7, 8}) }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x49, 0x00, 0x00, 0x00, // size (little endian)
0x04, /// entry: embedded document
@ -496,13 +496,13 @@ TEST_CASE("BSON")
SECTION("non-empty object with binary member")
{
const size_t N = 10;
const auto s = std::vector<uint8_t>(N, 'x');
const auto s = std::vector<std::uint8_t>(N, 'x');
json j =
{
{ "entry", json::binary_array(s) }
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
0x1B, 0x00, 0x00, 0x00, // size (little endian)
0x05, // entry: binary
@ -523,6 +523,36 @@ TEST_CASE("BSON")
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("non-empty object with binary member with subtype")
{
// an MD5 hash
const std::vector<std::uint8_t> md5hash = {0xd7, 0x7e, 0x27, 0x54, 0xbe, 0x12, 0x37, 0xfe, 0xd6, 0x0c, 0x33, 0x98, 0x30, 0x3b, 0x8d, 0xc4};
json j =
{
{ "entry", json::binary_array(md5hash, 5) }
};
std::vector<std::uint8_t> expected =
{
0x21, 0x00, 0x00, 0x00, // size (little endian)
0x05, // entry: binary
'e', 'n', 't', 'r', 'y', '\x00',
0x10, 0x00, 0x00, 0x00, // size of binary (little endian)
0x05, // MD5 binary subtype
0xd7, 0x7e, 0x27, 0x54, 0xbe, 0x12, 0x37, 0xfe, 0xd6, 0x0c, 0x33, 0x98, 0x30, 0x3b, 0x8d, 0xc4,
0x00 // end marker
};
const auto result = json::to_bson(j);
CHECK(result == expected);
// roundtrip
CHECK(json::from_bson(result) == j);
CHECK(json::from_bson(result, true, false) == j);
}
SECTION("Some more complex document")
{
// directly encoding uint64 is not supported in bson (only for timestamp values)
@ -534,7 +564,7 @@ TEST_CASE("BSON")
{"object", {{ "string", "value" }}}
};
std::vector<uint8_t> expected =
std::vector<std::uint8_t> expected =
{
/*size */ 0x4f, 0x00, 0x00, 0x00,
/*entry*/ 0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40,
@ -592,7 +622,7 @@ TEST_CASE("BSON input/output_adapters")
{"object", {{ "string", "value" }}}
};
std::vector<uint8_t> bson_representation =
std::vector<std::uint8_t> bson_representation =
{
/*size */ 0x4f, 0x00, 0x00, 0x00,
/*entry*/ 0x01, 'd', 'o', 'u', 'b', 'l', 'e', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x45, 0x40,
@ -677,7 +707,7 @@ class SaxCountdown
return events_left-- > 0;
}
bool binary(std::vector<uint8_t>&)
bool binary(std::vector<std::uint8_t>&)
{
return events_left-- > 0;
}
@ -721,7 +751,7 @@ TEST_CASE("Incomplete BSON Input")
{
SECTION("Incomplete BSON Input 1")
{
std::vector<uint8_t> incomplete_bson =
std::vector<std::uint8_t> incomplete_bson =
{
0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean
@ -741,7 +771,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("Incomplete BSON Input 2")
{
std::vector<uint8_t> incomplete_bson =
std::vector<std::uint8_t> incomplete_bson =
{
0x0D, 0x00, 0x00, 0x00, // size (little endian)
0x08, // entry: boolean, unexpected EOF
@ -759,7 +789,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("Incomplete BSON Input 3")
{
std::vector<uint8_t> incomplete_bson =
std::vector<std::uint8_t> incomplete_bson =
{
0x41, 0x00, 0x00, 0x00, // size (little endian)
0x04, /// entry: embedded document
@ -783,7 +813,7 @@ TEST_CASE("Incomplete BSON Input")
SECTION("Incomplete BSON Input 4")
{
std::vector<uint8_t> incomplete_bson =
std::vector<std::uint8_t> incomplete_bson =
{
0x0D, 0x00, // size (incomplete), unexpected EOF
};
@ -821,9 +851,28 @@ TEST_CASE("Incomplete BSON Input")
}
}
TEST_CASE("Negative size of binary value")
{
// invalid BSON: the size of the binary value is -1
std::vector<std::uint8_t> input =
{
0x21, 0x00, 0x00, 0x00, // size (little endian)
0x05, // entry: binary
'e', 'n', 't', 'r', 'y', '\x00',
0xFF, 0xFF, 0xFF, 0xFF, // size of binary (little endian)
0x05, // MD5 binary subtype
0xd7, 0x7e, 0x27, 0x54, 0xbe, 0x12, 0x37, 0xfe, 0xd6, 0x0c, 0x33, 0x98, 0x30, 0x3b, 0x8d, 0xc4,
0x00 // end marker
};
CHECK_THROWS_AS(json::from_bson(input), json::parse_error);
CHECK_THROWS_WITH(json::from_bson(input), "[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1");
}
TEST_CASE("Unsupported BSON input")
{
std::vector<uint8_t> bson =
std::vector<std::uint8_t> bson =
{
0x0C, 0x00, 0x00, 0x00, // size (little endian)
0xFF, // entry type: Min key (not supported yet)
@ -876,19 +925,19 @@ TEST_CASE("BSON numerical data")
CHECK(j.at("entry").is_number_integer());
std::uint64_t iu = *reinterpret_cast<std::uint64_t*>(&i);
std::vector<uint8_t> expected_bson =
std::vector<std::uint8_t> expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 7u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u // end marker
};
@ -948,15 +997,15 @@ TEST_CASE("BSON numerical data")
CHECK(j.at("entry").is_number_integer());
std::uint32_t iu = *reinterpret_cast<std::uint32_t*>(&i);
std::vector<uint8_t> expected_bson =
std::vector<std::uint8_t> expected_bson =
{
0x10u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x10u, /// entry: int32
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
0x00u // end marker
};
@ -1001,19 +1050,19 @@ TEST_CASE("BSON numerical data")
CHECK(j.at("entry").is_number_integer());
std::uint64_t iu = *reinterpret_cast<std::uint64_t*>(&i);
std::vector<uint8_t> expected_bson =
std::vector<std::uint8_t> expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 7u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u // end marker
};
@ -1062,15 +1111,15 @@ TEST_CASE("BSON numerical data")
};
auto iu = i;
std::vector<uint8_t> expected_bson =
std::vector<std::uint8_t> expected_bson =
{
0x10u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x10u, /// entry: int32
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
0x00u // end marker
};
@ -1117,19 +1166,19 @@ TEST_CASE("BSON numerical data")
};
auto iu = i;
std::vector<uint8_t> expected_bson =
std::vector<std::uint8_t> expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 7u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u // end marker
};
@ -1167,19 +1216,19 @@ TEST_CASE("BSON numerical data")
};
auto iu = i;
std::vector<uint8_t> expected_bson =
std::vector<std::uint8_t> expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<uint8_t>((iu >> (8u * 7u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 2u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 3u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 4u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 5u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 6u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 7u)) & 0xffu),
0x00u // end marker
};
@ -1208,14 +1257,14 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
CAPTURE(filename)
{
INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
INFO_WITH_TEMP(filename + ": std::vector<std::uint8_t>");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
// parse BSON file
std::ifstream f_bson(filename + ".bson", std::ios::binary);
std::vector<uint8_t> packed(
std::vector<std::uint8_t> packed(
(std::istreambuf_iterator<char>(f_bson)),
std::istreambuf_iterator<char>());
json j2;
@ -1248,7 +1297,7 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
// parse BSON file
std::ifstream f_bson(filename + ".bson", std::ios::binary);
std::vector<uint8_t> packed(
std::vector<std::uint8_t> packed(
(std::istreambuf_iterator<char>(f_bson)),
std::istreambuf_iterator<char>());
json j2;
@ -1266,13 +1315,13 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
// parse BSON file
std::ifstream f_bson(filename + ".bson", std::ios::binary);
std::vector<uint8_t> packed(
std::vector<std::uint8_t> packed(
(std::istreambuf_iterator<char>(f_bson)),
std::istreambuf_iterator<char>());
{
INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
std::vector<uint8_t> vec;
INFO_WITH_TEMP(filename + ": output adapters: std::vector<std::uint8_t>");
std::vector<std::uint8_t> vec;
json::to_bson(j1, vec);
if (vec != packed)

View File

@ -1428,6 +1428,53 @@ TEST_CASE("CBOR")
CHECK(json::from_cbor(result, true, false) == j);
}
}
SECTION("indefinite size")
{
std::vector<std::uint8_t> input = {0x5F, 0x44, 0xaa, 0xbb, 0xcc, 0xdd, 0x43, 0xee, 0xff, 0x99, 0xFF};
auto j = json::from_cbor(input);
CHECK(j.is_binary());
auto k = json::binary_array({0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x99});
CAPTURE(j.dump(0, ' ', false, json::error_handler_t::strict, true));
CHECK(j == k);
}
SECTION("binary in array")
{
// array with three empty byte strings
std::vector<std::uint8_t> input = {0x83, 0x40, 0x40, 0x40};
CHECK_NOTHROW(json::from_cbor(input));
}
SECTION("binary in object")
{
// object mapping "foo" to empty byte string
std::vector<std::uint8_t> input = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0x40};
CHECK_NOTHROW(json::from_cbor(input));
}
SECTION("SAX callback with binary")
{
// object mapping "foo" to byte string
std::vector<std::uint8_t> input = {0xA1, 0x63, 0x66, 0x6F, 0x6F, 0x41, 0x00};
// callback to set binary_seen to true if a binary value was seen
bool binary_seen = false;
auto callback = [&binary_seen](int /*depth*/, json::parse_event_t /*event*/, json & parsed)
{
if (parsed.is_binary())
{
binary_seen = true;
}
return true;
};
json j;
auto cbp = nlohmann::detail::json_sax_dom_callback_parser<json>(j, callback, true);
CHECK(json::sax_parse(input, &cbp, json::input_format_t::cbor));
CHECK(j.at("foo").is_binary());
CHECK(binary_seen);
}
}
}
@ -1508,6 +1555,9 @@ TEST_CASE("CBOR")
CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0X61})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0X61})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x5F})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x5F, 0x00})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_cbor(std::vector<uint8_t>({0x41})), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x18})),
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR number: unexpected end of input");
@ -1557,6 +1607,12 @@ TEST_CASE("CBOR")
"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61})),
"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing CBOR value: unexpected end of input");
CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x5F})),
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR binary: unexpected end of input");
CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x5F, 0x00})),
"[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR binary: expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x00");
CHECK_THROWS_WITH(_ = json::from_cbor(std::vector<uint8_t>({0x41})),
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing CBOR binary: unexpected end of input");
CHECK(json::from_cbor(std::vector<uint8_t>({0x18}), true, false).is_discarded());
CHECK(json::from_cbor(std::vector<uint8_t>({0x19}), true, false).is_discarded());
@ -1582,6 +1638,9 @@ TEST_CASE("CBOR")
CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0xF5}), true, false).is_discarded());
CHECK(json::from_cbor(std::vector<uint8_t>({0xA1, 0x61, 0x61}), true, false).is_discarded());
CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61}), true, false).is_discarded());
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F}), true, false).is_discarded());
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x00}), true, false).is_discarded());
CHECK(json::from_cbor(std::vector<uint8_t>({0x41}), true, false).is_discarded());
}
SECTION("unsupported bytes")

View File

@ -56,21 +56,23 @@ TEST_CASE("lexicographical comparison operators")
json::value_t::number_float,
json::value_t::object,
json::value_t::array,
json::value_t::string
json::value_t::string,
json::value_t::binary
};
SECTION("comparison: less")
{
std::vector<std::vector<bool>> expected =
{
{false, true, true, true, true, true, true, true},
{false, false, true, true, true, true, true, true},
{false, false, false, false, false, true, true, true},
{false, false, false, false, false, true, true, true},
{false, false, false, false, false, true, true, true},
{false, false, false, false, false, false, true, true},
{false, false, false, false, false, false, false, true},
{false, false, false, false, false, false, false, false}
{false, true, true, true, true, true, true, true, true},
{false, false, true, true, true, true, true, true, true},
{false, false, false, false, false, true, true, true, true},
{false, false, false, false, false, true, true, true, true},
{false, false, false, false, false, true, true, true, true},
{false, false, false, false, false, false, true, true, true},
{false, false, false, false, false, false, false, true, true},
{false, false, false, false, false, false, false, false, true},
{false, false, false, false, false, false, false, false, false}
};
for (size_t i = 0; i < j_types.size(); ++i)
@ -98,29 +100,32 @@ TEST_CASE("lexicographical comparison operators")
"foo", "bar",
true, false,
{1, 2, 3}, {"one", "two", "three"},
{{"first", 1}, {"second", 2}}, {{"a", "A"}, {"b", {"B"}}}
{{"first", 1}, {"second", 2}}, {{"a", "A"}, {"b", {"B"}}},
json::binary_array({1, 2, 3}), json::binary_array({1, 2, 4})
};
SECTION("comparison: equal")
{
std::vector<std::vector<bool>> expected =
{
{true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}
{true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}
};
for (size_t i = 0; i < j_values.size(); ++i)
@ -176,22 +181,24 @@ TEST_CASE("lexicographical comparison operators")
{
std::vector<std::vector<bool>> expected =
{
{false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true},
{false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true},
{false, false, false, true, true, true, true, true, true, true, false, false, true, true, true, true},
{false, false, false, false, false, false, false, false, true, true, false, false, true, true, true, true},
{false, false, false, true, false, true, false, true, true, true, false, false, true, true, true, true},
{false, false, false, true, false, false, false, true, true, true, false, false, true, true, true, true},
{false, false, false, true, true, true, false, true, true, true, false, false, true, true, true, true},
{false, false, false, true, false, false, false, false, true, true, false, false, true, true, true, true},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
{false, false, true, true, true, true, true, true, true, true, false, false, true, true, true, true},
{false, false, true, true, true, true, true, true, true, true, true, false, true, true, true, true},
{false, false, false, false, false, false, false, false, true, true, false, false, false, true, false, false},
{false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false},
{false, false, false, false, false, false, false, false, true, true, false, false, true, true, false, false},
{false, false, false, false, false, false, false, false, true, true, false, false, true, true, true, false}
{false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true},
{false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true},
{false, false, false, true, true, true, true, true, true, true, false, false, true, true, true, true, true, true},
{false, false, false, false, false, false, false, false, true, true, false, false, true, true, true, true, true, true},
{false, false, false, true, false, true, false, true, true, true, false, false, true, true, true, true, true, true},
{false, false, false, true, false, false, false, true, true, true, false, false, true, true, true, true, true, true},
{false, false, false, true, true, true, false, true, true, true, false, false, true, true, true, true, true, true},
{false, false, false, true, false, false, false, false, true, true, false, false, true, true, true, true, true, true},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true},
{false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, true},
{false, false, true, true, true, true, true, true, true, true, false, false, true, true, true, true, true, true},
{false, false, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true},
{false, false, false, false, false, false, false, false, true, true, false, false, false, true, false, false, true, true},
{false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, true, true},
{false, false, false, false, false, false, false, false, true, true, false, false, true, true, false, false, true, true},
{false, false, false, false, false, false, false, false, true, true, false, false, true, true, true, false, true, true},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true},
{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}
};
for (size_t i = 0; i < j_values.size(); ++i)

View File

@ -115,6 +115,14 @@ TEST_CASE("constructors")
CHECK(j.type() == t);
CHECK(j == 0.0);
}
SECTION("binary")
{
auto t = json::value_t::binary;
json j(t);
CHECK(j.type() == t);
CHECK(j == json::binary_array({}));
}
}
SECTION("create a null object (implicitly)")
@ -1453,6 +1461,20 @@ TEST_CASE("constructors")
CHECK(j == j_new);
}
}
SECTION("binary")
{
{
json j = json::binary_array({1, 2, 3});
json j_new(j.begin(), j.end());
CHECK((j == j_new));
}
{
json j = json::binary_array({1, 2, 3});
json j_new(j.cbegin(), j.cend());
CHECK((j == j_new));
}
}
}
SECTION("construct with two invalid iterators")

View File

@ -91,6 +91,13 @@ TEST_CASE("other constructors and destructor")
json k(j);
CHECK(j == k);
}
SECTION("binary")
{
json j = json::binary_array({1, 2, 3});
json k(j);
CHECK(j == k);
}
}
SECTION("move constructor")
@ -167,6 +174,14 @@ TEST_CASE("other constructors and destructor")
k = j;
CHECK(j == k);
}
SECTION("binary")
{
json j = json::binary_array({1, 2, 3});
json k;
k = j;
CHECK(j == k);
}
}
SECTION("destructor")

View File

@ -58,6 +58,7 @@ TEST_CASE("convenience functions")
CHECK(std::string(json(json::value_t::number_integer).type_name()) == "number");
CHECK(std::string(json(json::value_t::number_unsigned).type_name()) == "number");
CHECK(std::string(json(json::value_t::number_float).type_name()) == "number");
CHECK(std::string(json(json::value_t::binary).type_name()) == "binary");
CHECK(std::string(json(json::value_t::boolean).type_name()) == "boolean");
CHECK(std::string(json(json::value_t::string).type_name()) == "string");
CHECK(std::string(json(json::value_t::discarded).type_name()) == "discarded");

View File

@ -694,6 +694,22 @@ TEST_CASE("element access 1")
CHECK(it == j.end());
}
}
SECTION("binary")
{
{
json j = json::binary_array({1, 2, 3});
json::iterator it = j.erase(j.begin());
CHECK(j.type() == json::value_t::null);
CHECK(it == j.end());
}
{
json j = json::binary_array({1, 2, 3});
json::const_iterator it = j.erase(j.cbegin());
CHECK(j.type() == json::value_t::null);
CHECK(it == j.end());
}
}
}
SECTION("erase with one invalid iterator")
@ -876,6 +892,22 @@ TEST_CASE("element access 1")
CHECK(it == j.end());
}
}
SECTION("binary")
{
{
json j = json::binary_array({1, 2, 3});
json::iterator it = j.erase(j.begin(), j.end());
CHECK(j.type() == json::value_t::null);
CHECK(it == j.end());
}
{
json j = json::binary_array({1, 2, 3});
json::const_iterator it = j.erase(j.cbegin(), j.cend());
CHECK(j.type() == json::value_t::null);
CHECK(it == j.end());
}
}
}
SECTION("erase with two invalid iterators")

View File

@ -49,6 +49,7 @@ TEST_CASE("object inspection")
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
@ -66,6 +67,7 @@ TEST_CASE("object inspection")
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(j.is_array());
CHECK(not j.is_string());
@ -83,6 +85,7 @@ TEST_CASE("object inspection")
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
@ -100,6 +103,7 @@ TEST_CASE("object inspection")
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
@ -117,6 +121,7 @@ TEST_CASE("object inspection")
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(j.is_string());
@ -134,6 +139,7 @@ TEST_CASE("object inspection")
CHECK(j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
@ -151,6 +157,7 @@ TEST_CASE("object inspection")
CHECK(j.is_number_integer());
CHECK(j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
@ -168,6 +175,25 @@ TEST_CASE("object inspection")
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
CHECK(not j.is_discarded());
CHECK(j.is_primitive());
CHECK(not j.is_structured());
}
SECTION("binary")
{
json j(json::value_t::binary);
CHECK(not j.is_null());
CHECK(not j.is_boolean());
CHECK(not j.is_number());
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
@ -185,6 +211,7 @@ TEST_CASE("object inspection")
CHECK(not j.is_number_integer());
CHECK(not j.is_number_unsigned());
CHECK(not j.is_number_float());
CHECK(not j.is_binary());
CHECK(not j.is_object());
CHECK(not j.is_array());
CHECK(not j.is_string());
@ -439,5 +466,12 @@ TEST_CASE("object inspection")
json::value_t t = j;
CHECK(t == j.type());
}
SECTION("binary")
{
json j = json::binary_array({});
json::value_t t = j;
CHECK(t == j.type());
}
}
}

View File

@ -106,6 +106,31 @@ TEST_CASE("modifiers")
}
}
SECTION("binary")
{
SECTION("empty binary")
{
json j = json::binary_array({});
json k = j;
j.clear();
CHECK(not j.empty());
CHECK(j == json(json::value_t::binary));
CHECK(j == json(k.type()));
}
SECTION("filled binary")
{
json j = json::binary_array({1, 2, 3, 4, 5});
json k = j;
j.clear();
CHECK(not j.empty());
CHECK(j == json(json::value_t::binary));
CHECK(j == json(k.type()));
}
}
SECTION("number (integer)")
{
json j = 23;
@ -937,5 +962,31 @@ TEST_CASE("modifiers")
CHECK_THROWS_WITH(j.swap(s), "[json.exception.type_error.310] cannot use swap() with number");
}
}
SECTION("binary_t")
{
SECTION("binary_t type")
{
json j = json::binary_array({1, 2, 3, 4});
json::binary_t s = {1, 2, 3, 4};
j.swap(s);
CHECK(j == json::binary_array({1, 2, 3, 4}));
j.swap(s);
CHECK(j == json::binary_array({1, 2, 3, 4}));
}
SECTION("non-string_t type")
{
json j = 17;
json::binary_t s = {1, 2, 3, 4};
CHECK_THROWS_AS(j.swap(s), json::type_error&);
CHECK_THROWS_WITH(j.swap(s), "[json.exception.type_error.310] cannot use swap() with number");
}
}
}
}

View File

@ -1418,6 +1418,7 @@ TEST_CASE("MessagePack")
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x92, 0x01})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0x81, 0xa1, 0x61})), json::parse_error&);
CHECK_THROWS_AS(_ = json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02})), json::parse_error&);
CHECK_THROWS_WITH(_ = json::from_msgpack(std::vector<uint8_t>({0x87})),
"[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing MessagePack string: unexpected end of input");
@ -1457,6 +1458,8 @@ TEST_CASE("MessagePack")
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack value: unexpected end of input");
CHECK_THROWS_WITH(_ = json::from_msgpack(std::vector<uint8_t>({0x81, 0xa1, 0x61})),
"[json.exception.parse_error.110] parse error at byte 4: syntax error while parsing MessagePack value: unexpected end of input");
CHECK_THROWS_WITH(_ = json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02})),
"[json.exception.parse_error.110] parse error at byte 3: syntax error while parsing MessagePack binary: unexpected end of input");
CHECK(json::from_msgpack(std::vector<uint8_t>({0x87}), true, false).is_discarded());
CHECK(json::from_msgpack(std::vector<uint8_t>({0xcc}), true, false).is_discarded());
@ -1477,6 +1480,8 @@ TEST_CASE("MessagePack")
CHECK(json::from_msgpack(std::vector<uint8_t>({0xa5, 0x68, 0x65}), true, false).is_discarded());
CHECK(json::from_msgpack(std::vector<uint8_t>({0x92, 0x01}), true, false).is_discarded());
CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xA1, 0x61}), true, false).is_discarded());
CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4, 0x02}), true, false).is_discarded());
CHECK(json::from_msgpack(std::vector<uint8_t>({0xc4}), true, false).is_discarded());
}
SECTION("unsupported bytes")

View File

@ -34,22 +34,6 @@ using nlohmann::json;
TEST_CASE("pointer access")
{
// create a JSON value with different types
json json_types =
{
{"boolean", true},
{
"number", {
{"integer", 42},
{"unsigned", 42u},
{"floating-point", 17.23}
}
},
{"string", "Hello, world!"},
{"array", {1, 2, 3, 4, 5}},
{"null", nullptr}
};
SECTION("pointer access to object_t")
{
using test_type = json::object_t;
@ -61,11 +45,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -76,6 +60,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to const object_t")
@ -89,11 +75,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -104,6 +90,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to array_t")
@ -117,11 +105,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -132,6 +120,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to const array_t")
@ -145,11 +135,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -160,6 +150,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to string_t")
@ -173,11 +165,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -188,6 +180,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to const string_t")
@ -201,11 +195,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -216,6 +210,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to boolean_t")
@ -229,11 +225,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -244,6 +240,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to const boolean_t")
@ -257,11 +255,11 @@ TEST_CASE("pointer access")
//CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -272,6 +270,9 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to number_integer_t")
@ -285,11 +286,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -300,6 +301,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() != nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to const number_integer_t")
@ -313,11 +316,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -328,6 +331,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_t*>() != nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to number_unsigned_t")
@ -341,11 +346,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -356,6 +361,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() != nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() != nullptr);
CHECK(value.get_ptr<json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to const number_unsigned_t")
@ -369,11 +376,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
@ -384,6 +391,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_t*>() != nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() != nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
CHECK(value.get_ptr<const json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to number_float_t")
@ -397,11 +406,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == Approx(value.get<test_type>()));
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == Approx(value.get<test_type>()));
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == Approx(value.get<test_type>()));
// check if null pointers are returned correctly
@ -412,6 +421,8 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<json::number_float_t*>() != nullptr);
CHECK(value.get_ptr<json::binary_t*>() == nullptr);
CHECK(value.get_ptr<json::internal_binary_t*>() == nullptr);
}
SECTION("pointer access to const number_float_t")
@ -425,11 +436,11 @@ TEST_CASE("pointer access")
CHECK(*p1 == Approx(value.get<test_type>()));
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p1 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get_ptr<const test_type*>());
CHECK(*p2 == Approx(value.get<test_type>()));
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p1 == value.get_ptr<const test_type* const>());
CHECK(p3 == value.get_ptr<const test_type* const>());
CHECK(*p3 == Approx(value.get<test_type>()));
// check if null pointers are returned correctly
@ -440,5 +451,64 @@ TEST_CASE("pointer access")
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() != nullptr);
CHECK(value.get_ptr<const json::binary_t*>() == nullptr);
}
SECTION("pointer access to const binary_t")
{
using test_type = const json::binary_t;
const json value = json::binary_array({});
// check if pointers are returned correctly
test_type* p1 = value.get_ptr<test_type*>();
CHECK(p1 == value.get_ptr<test_type*>());
//CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p2 == value.get_ptr<const test_type*>());
//CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p3 == value.get_ptr<const test_type* const>());
//CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
CHECK(value.get_ptr<const json::object_t*>() == nullptr);
CHECK(value.get_ptr<const json::array_t*>() == nullptr);
CHECK(value.get_ptr<const json::string_t*>() == nullptr);
CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::binary_t*>() != nullptr);
}
SECTION("pointer access to const binary_t")
{
using test_type = const json::internal_binary_t;
const json value = json::binary_array({});
// check if pointers are returned correctly
test_type* p1 = value.get_ptr<test_type*>();
CHECK(p1 == value.get_ptr<test_type*>());
//CHECK(*p1 == value.get<test_type>());
const test_type* p2 = value.get_ptr<const test_type*>();
CHECK(p2 == value.get_ptr<const test_type*>());
//CHECK(*p2 == value.get<test_type>());
const test_type* const p3 = value.get_ptr<const test_type* const>();
CHECK(p3 == value.get_ptr<const test_type* const>());
//CHECK(*p3 == value.get<test_type>());
// check if null pointers are returned correctly
CHECK(value.get_ptr<const json::object_t*>() == nullptr);
CHECK(value.get_ptr<const json::array_t*>() == nullptr);
CHECK(value.get_ptr<const json::string_t*>() == nullptr);
CHECK(value.get_ptr<const json::boolean_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_integer_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_unsigned_t*>() == nullptr);
CHECK(value.get_ptr<const json::number_float_t*>() == nullptr);
CHECK(value.get_ptr<const json::internal_binary_t*>() != nullptr);
}
}

View File

@ -206,3 +206,55 @@ TEST_CASE_TEMPLATE("serialization for extreme integer values", T, int32_t, uint3
CHECK(j.dump() == std::to_string(maximum));
}
}
TEST_CASE("dump with binary values")
{
SECTION("serialize_binary = false")
{
auto binary = json::binary_array({1, 2, 3, 4});
auto binary_empty = json::binary_array({});
json object = {{"key", binary}};
json array = {"value", 1, binary};
CHECK_THROWS_AS(binary.dump(), json::type_error);
CHECK_THROWS_AS(binary_empty.dump(), json::type_error);
CHECK_THROWS_AS(object.dump(), json::type_error);
CHECK_THROWS_AS(array.dump(), json::type_error);
CHECK_THROWS_WITH(binary.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
CHECK_THROWS_WITH(binary_empty.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
CHECK_THROWS_WITH(object.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
CHECK_THROWS_WITH(array.dump(), "[json.exception.type_error.317] cannot serialize binary data to text JSON");
}
SECTION("serialize_binary = true")
{
auto binary = json::binary_array({1, 2, 3, 4});
auto binary_empty = json::binary_array({});
json object = {{"key", binary}};
json array = {"value", 1, binary};
CHECK(binary.dump(-1, ' ', false, json::error_handler_t::strict, true) == "b[1,2,3,4]");
CHECK(binary_empty.dump(-1, ' ', false, json::error_handler_t::strict, true) == "b[]");
CHECK(object.dump(-1, ' ', false, json::error_handler_t::strict, true) == "{\"key\":b[1,2,3,4]}");
CHECK(array.dump(-1, ' ', false, json::error_handler_t::strict, true) == "[\"value\",1,b[1,2,3,4]]");
}
SECTION("serialize_binary = true, pretty-printed")
{
auto binary = json::binary_array({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20});
auto binary_empty = json::binary_array({});
json object = {{"key", binary}};
json array = {"value", 1, binary};
CHECK(binary.dump(4, ' ', false, json::error_handler_t::strict, true) == "b[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]");
CHECK(binary_empty.dump(4, ' ', false, json::error_handler_t::strict, true) == "b[]");
CHECK(object.dump(4, ' ', false, json::error_handler_t::strict, true) == "{\n"
" \"key\": b[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n"
"}");
CHECK(array.dump(4, ' ', false, json::error_handler_t::strict, true) == "[\n"
" \"value\",\n"
" 1,\n"
" b[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n"
"]");
}
}