🐛 fix bug in SAX callback parser

This commit is contained in:
Niels Lohmann 2020-05-08 14:21:11 +02:00
parent cf4a6552f3
commit f0c6ab4d3b
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
5 changed files with 58 additions and 66 deletions

View file

@ -1442,6 +1442,29 @@ TEST_CASE("CBOR")
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);
}
}
}

View file

@ -442,4 +442,33 @@ TEST_CASE("pointer access")
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);
}
}