Allow allocators for output_vector_adapter (#2989)

* ♻️ allow allocators for vectors

*  add regression tests
This commit is contained in:
Niels Lohmann
2021-09-12 18:55:47 +02:00
committed by GitHub
parent 58b83b71dc
commit 0b345b20c8
3 changed files with 28 additions and 10 deletions

View File

@@ -181,6 +181,13 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser<json>
std::string* sax_no_exception::error_string = nullptr;
/////////////////////////////////////////////////////////////////////
// for #2982
/////////////////////////////////////////////////////////////////////
template<class T>
class my_allocator : public std::allocator<T>
{};
TEST_CASE("regression tests 2")
{
@@ -679,6 +686,15 @@ TEST_CASE("regression tests 2")
test3[json::json_pointer(p)] = json::object();
CHECK(test3.dump() == "{\"/root\":{}}");
}
SECTION("issue #2982 - to_{binary format} does not provide a mechanism for specifying a custom allocator for the returned type")
{
std::vector<std::uint8_t, my_allocator<std::uint8_t>> my_vector;
json j = {1, 2, 3, 4};
json::to_cbor(j, my_vector);
json k = json::from_cbor(my_vector);
CHECK(j == k);
}
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP