mirror of
https://github.com/nlohmann/json.git
synced 2026-10-01 14:35:18 +00:00
* Copy values before inserting an initializer list into an array
insert(pos, {...}) inserted wrong values when the initializer list
contained const references to elements of the array being inserted
into. json_ref stores only a pointer for a const lvalue, so the
initializer_list_t range passed straight to the array's range insert
aliased the array's own storage; std::vector::insert(pos, first, last)
may move or shift elements before copying from that range, so the
source elements were already stale by the time they were read
(different wrong results on libc++ and libstdc++).
Copy the referenced values into a temporary array_t first, then move
that temporary into place, so the source range never aliases the
array being modified.
Fixes #5656.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* Use the reserve_array helper in the initializer_list insert fix
The previous commit called array_t::reserve() directly on the
temporary buffer used to copy an ilist's values before inserting.
std::deque, a documented ArrayType (tests/src/unit-custom-array-type.cpp),
has no reserve(), so insert(pos, initializer_list) no longer compiled
for it. Use the existing detail::reserve_array() SFINAE helper (already
used by the SAX DOM parser) instead, which leaves array types without
reserve() untouched.
Added a regression check that deque_json::insert(pos, {...}) compiles
and handles the aliasing case from #5656.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
167 lines
5.8 KiB
C++
167 lines
5.8 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
|
// | | |__ | | | | | | version 3.12.0
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "doctest_compatibility.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <deque>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
|
|
// std::deque has no capacity() member function, which the library only needs
|
|
// to detect a reallocation for JSON_DIAGNOSTICS
|
|
using deque_json = nlohmann::basic_json<std::map, std::deque>;
|
|
|
|
// a std::vector whose at() is hidden: the library performs its own bounds
|
|
// check and must not fall back to the container's checked accessor
|
|
template<class T, class Allocator = std::allocator<T>>
|
|
class vector_without_at : public std::vector<T, Allocator>
|
|
{
|
|
public:
|
|
vector_without_at() = default;
|
|
|
|
// the array of an initializer list is built from a range
|
|
template<class InputIt>
|
|
vector_without_at(InputIt first, InputIt last) : std::vector<T, Allocator>(first, last) {}
|
|
|
|
void at() = delete;
|
|
};
|
|
|
|
using no_at_json = nlohmann::basic_json<std::map, vector_without_at>;
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("array type without capacity()")
|
|
{
|
|
SECTION("the iterators take their exception specification from the container")
|
|
{
|
|
// basic_json's iterators move exactly as the container iterators do:
|
|
// their move operations are defaulted without a declared noexcept,
|
|
// because an array or object type whose iterator is not nothrow move
|
|
// constructible would otherwise have them deleted (std::deque's is not
|
|
// with libstdc++ before 11, and neither are MSVC's debug iterators)
|
|
CHECK(std::is_nothrow_move_constructible<nlohmann::json::iterator>::value ==
|
|
(std::is_nothrow_move_constructible<nlohmann::json::object_t::iterator>::value
|
|
&& std::is_nothrow_move_constructible<nlohmann::json::array_t::iterator>::value));
|
|
CHECK(std::is_nothrow_move_assignable<nlohmann::json::iterator>::value ==
|
|
(std::is_nothrow_move_assignable<nlohmann::json::object_t::iterator>::value
|
|
&& std::is_nothrow_move_assignable<nlohmann::json::array_t::iterator>::value));
|
|
CHECK(std::is_nothrow_move_constructible<nlohmann::json::const_iterator>::value ==
|
|
(std::is_nothrow_move_constructible<nlohmann::json::object_t::const_iterator>::value
|
|
&& std::is_nothrow_move_constructible<nlohmann::json::array_t::const_iterator>::value));
|
|
|
|
// and they are movable at all, which is what dropping the declared
|
|
// noexcept buys for a std::deque array
|
|
CHECK(std::is_move_constructible<deque_json::iterator>::value);
|
|
CHECK(std::is_move_assignable<deque_json::iterator>::value);
|
|
}
|
|
|
|
SECTION("adding elements")
|
|
{
|
|
deque_json j = deque_json::array();
|
|
j.push_back(1);
|
|
j.push_back("two");
|
|
j.emplace_back(3);
|
|
j += 4;
|
|
|
|
CHECK(j.size() == 4);
|
|
CHECK(j == deque_json({1, "two", 3, 4}));
|
|
CHECK(j.back() == 4);
|
|
CHECK(j.front() == 1);
|
|
}
|
|
|
|
SECTION("accessing and modifying elements")
|
|
{
|
|
auto j = deque_json::parse(R"([1,2,3])");
|
|
|
|
CHECK(j[1] == 2);
|
|
CHECK(j.at(2) == 3);
|
|
|
|
// growing through operator[] fills up with null values
|
|
j[5] = 6;
|
|
CHECK(j.size() == 6);
|
|
CHECK(j[4].is_null());
|
|
CHECK(j[5] == 6);
|
|
|
|
j.erase(0);
|
|
CHECK(j == deque_json({2, 3, nullptr, nullptr, 6}));
|
|
|
|
auto it = j.erase(j.begin());
|
|
CHECK(*it == 3);
|
|
|
|
j.insert(j.begin(), 1);
|
|
CHECK(j.front() == 1);
|
|
}
|
|
|
|
SECTION("serialization and deserialization")
|
|
{
|
|
const auto j = deque_json::parse(R"({"a":[1,[2,3]],"b":[]})");
|
|
CHECK(j.dump() == R"({"a":[1,[2,3]],"b":[]})");
|
|
CHECK(deque_json::parse(j.dump()) == j);
|
|
CHECK(deque_json::from_cbor(deque_json::to_cbor(j)) == j);
|
|
|
|
// empty containers are flattened to null and cannot be restored
|
|
const auto nested = deque_json::parse(R"({"a":[1,[2,3]]})");
|
|
CHECK(nested.flatten().unflatten() == nested);
|
|
}
|
|
|
|
SECTION("insert(pos, initializer_list) compiles and works without reserve()")
|
|
{
|
|
// std::deque has no reserve() either; insert(pos, ilist) must not
|
|
// require it (regression test for #5656, which also covers an ilist
|
|
// that refers to elements of the array being inserted into)
|
|
deque_json j = deque_json::array();
|
|
j.push_back("a");
|
|
j.push_back("b");
|
|
j.push_back("c");
|
|
|
|
const deque_json& cj = j;
|
|
auto it = j.insert(j.begin(), {cj[0], cj[1]});
|
|
CHECK(*it == deque_json("a"));
|
|
CHECK(j == deque_json({"a", "b", "a", "b", "c"}));
|
|
}
|
|
|
|
SECTION("references stay valid while the array grows")
|
|
{
|
|
deque_json j = deque_json::array();
|
|
j.push_back(1);
|
|
auto& first = j[0];
|
|
for (int i = 0; i < 100; ++i)
|
|
{
|
|
j.push_back(i);
|
|
}
|
|
CHECK(&first == &j[0]);
|
|
CHECK(first == 1);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("array type without at()")
|
|
{
|
|
// built in memory rather than parsed, so that the exception message does
|
|
// not gain a byte range with JSON_DIAGNOSTIC_POSITIONS
|
|
no_at_json j = {1, 2, 3};
|
|
const auto& jc = j;
|
|
|
|
CHECK(j.at(0) == 1);
|
|
CHECK(j.at(2) == 3);
|
|
CHECK(jc.at(2) == 3);
|
|
|
|
CHECK_THROWS_WITH_AS(j.at(3), "[json.exception.out_of_range.401] array index 3 is out of range", no_at_json::out_of_range);
|
|
CHECK_THROWS_WITH_AS(jc.at(3), "[json.exception.out_of_range.401] array index 3 is out of range", no_at_json::out_of_range);
|
|
|
|
CHECK(j.at(no_at_json::json_pointer("/1")) == 2);
|
|
CHECK_THROWS_AS(j.at(no_at_json::json_pointer("/3")), no_at_json::out_of_range);
|
|
}
|