mirror of
https://github.com/nlohmann/json.git
synced 2026-09-28 18:50:31 +00:00
Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90f85d6d75 | ||
|
|
4aaeb01ea4 | ||
|
|
2038838eea | ||
|
|
aa5084d713 | ||
|
|
33c4dfdc18 | ||
|
|
b41e43fffc | ||
|
|
958e0a906b | ||
|
|
49f038b86a | ||
|
|
722c2bb561 | ||
|
|
f41296276c | ||
|
|
481b8d17fa | ||
|
|
484f644b86 | ||
|
|
08e30eca78 | ||
|
|
582223eb8a | ||
|
|
c14208a8e0 |
@@ -42,10 +42,6 @@ namespace detail
|
|||||||
* j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
|
* j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
|
||||||
* allocated value (e.g., a string). See bug issue
|
* allocated value (e.g., a string). See bug issue
|
||||||
* https://github.com/nlohmann/json/issues/2865 for more information.
|
* https://github.com/nlohmann/json/issues/2865 for more information.
|
||||||
*
|
|
||||||
* A value that has to be allocated is created before the old one is destroyed:
|
|
||||||
* were it the other way around, an exception while creating the new value would
|
|
||||||
* leave j with the type of the new value, but the pointer to the destroyed old one.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<value_t> struct external_constructor;
|
template<value_t> struct external_constructor;
|
||||||
@@ -69,20 +65,18 @@ struct external_constructor<value_t::string>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(s);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::string;
|
j.m_data.m_type = value_t::string;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = s;
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
|
static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(s));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::string;
|
j.m_data.m_type = value_t::string;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = std::move(s);
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,10 +85,9 @@ struct external_constructor<value_t::string>
|
|||||||
int > = 0 >
|
int > = 0 >
|
||||||
static void construct(BasicJsonType& j, const CompatibleStringType& str)
|
static void construct(BasicJsonType& j, const CompatibleStringType& str)
|
||||||
{
|
{
|
||||||
auto* created = j.template create<typename BasicJsonType::string_t>(str);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::string;
|
j.m_data.m_type = value_t::string;
|
||||||
j.m_data.m_value.string = created;
|
j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -105,20 +98,18 @@ struct external_constructor<value_t::binary>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(b);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::binary;
|
j.m_data.m_type = value_t::binary;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = typename BasicJsonType::binary_t(b);
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
|
static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(b));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::binary;
|
j.m_data.m_type = value_t::binary;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -168,10 +159,9 @@ struct external_constructor<value_t::array>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(arr);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = arr;
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -179,10 +169,9 @@ struct external_constructor<value_t::array>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
|
static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(arr));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = std::move(arr);
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -198,10 +187,9 @@ struct external_constructor<value_t::array>
|
|||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
auto* created = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value.array = created;
|
j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -209,17 +197,15 @@ struct external_constructor<value_t::array>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const std::vector<bool>& arr)
|
static void construct(BasicJsonType& j, const std::vector<bool>& arr)
|
||||||
{
|
{
|
||||||
typename BasicJsonType::array_t elements;
|
|
||||||
elements.reserve(arr.size());
|
|
||||||
for (const bool x : arr)
|
|
||||||
{
|
|
||||||
elements.push_back(x);
|
|
||||||
}
|
|
||||||
const typename BasicJsonType::json_value value(std::move(elements));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = value_t::array;
|
||||||
j.set_parents();
|
j.m_data.m_value.array->reserve(arr.size());
|
||||||
|
for (const bool x : arr)
|
||||||
|
{
|
||||||
|
j.m_data.m_value.array->push_back(x);
|
||||||
|
j.set_parent(j.m_data.m_value.array->back());
|
||||||
|
}
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,12 +213,11 @@ struct external_constructor<value_t::array>
|
|||||||
enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
|
enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
|
||||||
static void construct(BasicJsonType& j, const std::valarray<T>& arr)
|
static void construct(BasicJsonType& j, const std::valarray<T>& arr)
|
||||||
{
|
{
|
||||||
typename BasicJsonType::array_t elements(arr.size());
|
|
||||||
std::copy(std::begin(arr), std::end(arr), elements.begin());
|
|
||||||
const typename BasicJsonType::json_value value(std::move(elements));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = value_t::array;
|
||||||
|
j.m_data.m_value.array->resize(arr.size());
|
||||||
|
std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -244,16 +229,14 @@ struct external_constructor<value_t::array>
|
|||||||
enable_if_t<is_compatible_range_view<std::remove_cvref_t<CompatibleArrayType>>::value, int> = 0>
|
enable_if_t<is_compatible_range_view<std::remove_cvref_t<CompatibleArrayType>>::value, int> = 0>
|
||||||
static void construct(BasicJsonType& j, CompatibleArrayType && arr)
|
static void construct(BasicJsonType& j, CompatibleArrayType && arr)
|
||||||
{
|
{
|
||||||
typename BasicJsonType::array_t elements;
|
|
||||||
for (auto&& x : std::forward<CompatibleArrayType>(arr))
|
|
||||||
{
|
|
||||||
elements.push_back(x);
|
|
||||||
}
|
|
||||||
const typename BasicJsonType::json_value value(std::move(elements));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = value_t::array;
|
||||||
j.set_parents();
|
for (auto&& x : std::forward<CompatibleArrayType>(arr))
|
||||||
|
{
|
||||||
|
j.m_data.m_value.array->push_back(x);
|
||||||
|
j.set_parent(j.m_data.m_value.array->back());
|
||||||
|
}
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -265,10 +248,9 @@ struct external_constructor<value_t::object>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(obj);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::object;
|
j.m_data.m_type = value_t::object;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = obj;
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -276,10 +258,9 @@ struct external_constructor<value_t::object>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
|
static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(obj));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::object;
|
j.m_data.m_type = value_t::object;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = std::move(obj);
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -291,10 +272,9 @@ struct external_constructor<value_t::object>
|
|||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
auto* created = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::object;
|
j.m_data.m_type = value_t::object;
|
||||||
j.m_data.m_value.object = created;
|
j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|||||||
+357
-119
@@ -1702,8 +1702,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (is_an_object)
|
if (is_an_object)
|
||||||
{
|
{
|
||||||
// the initializer list is a list of pairs -> create an object
|
// the initializer list is a list of pairs -> create an object
|
||||||
m_data.m_value = value_t::object;
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value = value_t::object;
|
||||||
|
|
||||||
for (auto& element_ref : init)
|
for (auto& element_ref : init)
|
||||||
{
|
{
|
||||||
@@ -1725,8 +1725,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// the initializer list describes an array -> create an array
|
// the initializer list describes an array -> create an array
|
||||||
m_data.m_value.array = create<array_t>(init.begin(), init.end());
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value.array = create<array_t>(init.begin(), init.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
set_parents();
|
set_parents();
|
||||||
@@ -1739,8 +1739,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(const typename binary_t::container_type& init)
|
static basic_json binary(const typename binary_t::container_type& init)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = init;
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = init;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1750,8 +1750,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
|
static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = binary_t(init, subtype);
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = binary_t(init, subtype);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1761,8 +1761,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(typename binary_t::container_type&& init)
|
static basic_json binary(typename binary_t::container_type&& init)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = std::move(init);
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = std::move(init);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1772,8 +1772,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
|
static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = binary_t(std::move(init), subtype);
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = binary_t(std::move(init), subtype);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2819,8 +2819,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty array
|
// implicitly convert a null value to an empty array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.array = create<array_t>();
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value.array = create<array_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2879,8 +2879,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty object
|
// implicitly convert a null value to an empty object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.object = create<object_t>();
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value.object = create<object_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2932,8 +2932,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty object
|
// implicitly convert a null value to an empty object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.object = create<object_t>();
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value.object = create<object_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3868,8 +3868,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an array
|
// transform a null object into an array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::array;
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value = value_t::array;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3901,8 +3901,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an array
|
// transform a null object into an array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::array;
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value = value_t::array;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3933,8 +3933,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an object
|
// transform a null object into an object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::object;
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value = value_t::object;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3989,8 +3989,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an array
|
// transform a null object into an array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::array;
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value = value_t::array;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4014,8 +4014,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an object
|
// transform a null object into an object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::object;
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value = value_t::object;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4196,8 +4196,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty object
|
// implicitly convert a null value to an empty object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.object = create<object_t>();
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value.object = create<object_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6061,68 +6061,112 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{
|
{
|
||||||
// the patch
|
// the patch
|
||||||
basic_json result(value_t::array);
|
basic_json result(value_t::array);
|
||||||
|
diff_recursively(result, source, target, path, 0);
|
||||||
// if the values are the same, return an empty patch
|
|
||||||
if (source == target)
|
|
||||||
{
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source.type() != target.type())
|
private:
|
||||||
|
/// @brief two arrays or two objects @ref diff_iteratively is diffing
|
||||||
|
struct diff_frame
|
||||||
|
{
|
||||||
|
diff_frame(const basic_json* source_, const basic_json* target_, const std::size_t path_length_) noexcept
|
||||||
|
: source(source_), target(target_), path_length(path_length_)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// declared for GCC's -Weffc++, which asks for them in a class with
|
||||||
|
// pointer members and a non-trivial destructor; the exception
|
||||||
|
// specifications are left implicit, as GCC 4.8 rejects explicit ones
|
||||||
|
// that differ from them
|
||||||
|
diff_frame(const diff_frame&) = default;
|
||||||
|
diff_frame(diff_frame&&) = default;
|
||||||
|
diff_frame& operator=(const diff_frame&) = default;
|
||||||
|
diff_frame& operator=(diff_frame&&) = default;
|
||||||
|
~diff_frame() = default;
|
||||||
|
|
||||||
|
/// the values being diffed, both arrays or both objects
|
||||||
|
const basic_json* source;
|
||||||
|
const basic_json* target;
|
||||||
|
/// the length of their path in `current_path`
|
||||||
|
std::size_t path_length;
|
||||||
|
/// arrays: the next index to diff
|
||||||
|
std::size_t index = 0;
|
||||||
|
/// objects: the next member of source to look at
|
||||||
|
const_iterator member{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
/// objects: the keys common to both, in source's order
|
||||||
|
std::vector<typename object_t::key_type> common_keys{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
/// objects: the next entry of common_keys
|
||||||
|
std::size_t next_common = 0;
|
||||||
|
/// objects: the "add" operations for keys only target has
|
||||||
|
basic_json added_ops{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
};
|
||||||
|
|
||||||
|
// The operations of a diff are built by the functions below rather than
|
||||||
|
// where they are needed: building one takes several temporaries, and
|
||||||
|
// unoptimized builds give each temporary a stack slot of its own in the
|
||||||
|
// function it appears in. In diff_recursively, which is on the call stack
|
||||||
|
// once per nesting level, that made every level cost kilobytes of stack.
|
||||||
|
|
||||||
|
/// @brief append a "replace" operation for @a path with @a value to @a result
|
||||||
|
static void diff_replace(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
{
|
{
|
||||||
// different types: replace value
|
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
{"op", "replace"}, {"path", path}, {"value", value}
|
||||||
});
|
});
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (source.type())
|
/// @brief append a "remove" operation for @a path to @a result
|
||||||
{
|
static void diff_remove(basic_json& result, const string_t& path)
|
||||||
case value_t::array:
|
|
||||||
{
|
|
||||||
// first pass: traverse common elements
|
|
||||||
std::size_t i = 0;
|
|
||||||
while (i < source.size() && i < target.size())
|
|
||||||
{
|
|
||||||
// recursive call to compare array values at index i
|
|
||||||
auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We now reached the end of at least one array
|
|
||||||
// in a second pass, traverse the remaining elements
|
|
||||||
|
|
||||||
// remove my remaining elements, highest index first; appending
|
|
||||||
// in that order avoids the quadratic reinsertion done before
|
|
||||||
for (std::size_t j = source.size(); j > i; --j)
|
|
||||||
{
|
{
|
||||||
result.push_back(object(
|
result.push_back(object(
|
||||||
{
|
{
|
||||||
{"op", "remove"},
|
{"op", "remove"}, {"path", path}
|
||||||
{"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1))}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
i = source.size();
|
|
||||||
|
|
||||||
// add other remaining elements
|
/// @brief append an "add" operation for @a path with @a value to @a result
|
||||||
while (i < target.size())
|
static void diff_add(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
{
|
{
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "add"},
|
{"op", "add"}, {"path", path}, {"value", value}
|
||||||
{"path", detail::concat<string_t>(path, "/-")},
|
|
||||||
{"value", target[i]}
|
|
||||||
});
|
});
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
/// @brief append the "remove" operations for the elements of array
|
||||||
|
/// @a source from @a index on, and the "add" operations for the
|
||||||
|
/// elements of array @a target from source's size on, to @a result
|
||||||
|
static void diff_array_tails(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t index)
|
||||||
|
{
|
||||||
|
// remove my remaining elements, highest index first; appending
|
||||||
|
// in that order avoids the quadratic reinsertion done before
|
||||||
|
for (std::size_t j = source.size(); j > index; --j)
|
||||||
|
{
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
case value_t::object:
|
// add other remaining elements
|
||||||
|
for (std::size_t i = source.size(); i < target.size(); ++i)
|
||||||
|
{
|
||||||
|
diff_add(result, detail::concat<string_t>(path, "/-"), target[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief compare the keys of objects @a source and @a target
|
||||||
|
|
||||||
|
If the keys both objects have are in the same order in both, and the keys
|
||||||
|
only @a target has come after them, stores the keys common to both in
|
||||||
|
source's order in @a common_keys, stores the "add" operations for the keys
|
||||||
|
only @a target has in @a added_ops, and returns true: the caller then diffs
|
||||||
|
the objects member by member. Otherwise, appends operations that remove
|
||||||
|
every member of @a source and add every member of @a target to @a result,
|
||||||
|
and returns false.
|
||||||
|
*/
|
||||||
|
static bool diff_object_keys(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, std::vector<typename object_t::key_type>& common_keys,
|
||||||
|
basic_json& added_ops)
|
||||||
{
|
{
|
||||||
// first pass: record, for every source key, whether it is
|
// first pass: record, for every source key, whether it is
|
||||||
// common to both objects (in source's iteration order) or
|
// common to both objects (in source's iteration order) or
|
||||||
@@ -6130,10 +6174,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// a by-product of the target.find() call already needed to
|
// a by-product of the target.find() call already needed to
|
||||||
// tell the two cases apart, so it adds no extra lookups. The
|
// tell the two cases apart, so it adds no extra lookups. The
|
||||||
// "remove" ops themselves are emitted later, interleaved
|
// "remove" ops themselves are emitted later, interleaved
|
||||||
// with the recursive per-key diffs in the fast path below,
|
// with the per-key diffs in the caller's fast path, to match
|
||||||
// to match source's original iteration order (as the
|
// source's original iteration order (as the original,
|
||||||
// original, pre-reordering-aware implementation did) instead
|
// pre-reordering-aware implementation did) instead of
|
||||||
// of grouping all removes before all recursive diffs.
|
// grouping all removes before all per-key diffs.
|
||||||
std::vector<typename object_t::key_type> common_keys_source_order;
|
std::vector<typename object_t::key_type> common_keys_source_order;
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
@@ -6149,20 +6193,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// source.find() call already needed to detect added keys. At
|
// source.find() call already needed to detect added keys. At
|
||||||
// the same time, determine whether every added key comes
|
// the same time, determine whether every added key comes
|
||||||
// after every common key in target's order (a precondition
|
// after every common key in target's order (a precondition
|
||||||
// for the fast path below, which only ever appends new keys
|
// for the fast path, which only ever appends new keys
|
||||||
// at the very end): for an object_t whose iteration order is
|
// at the very end): for an object_t whose iteration order is
|
||||||
// a pure function of the key set (e.g. the default std::map,
|
// a pure function of the key set (e.g. the default std::map,
|
||||||
// which always iterates in sorted key order), the order
|
// which always iterates in sorted key order), the order
|
||||||
// check further below is always true and this whole
|
// check further below is always true and this whole
|
||||||
// mechanism is effectively a no-op; it only matters for a
|
// mechanism is effectively a no-op; it only matters for a
|
||||||
// reorderable object_t such as the one backing `ordered_json`.
|
// reorderable object_t such as the one backing `ordered_json`.
|
||||||
// patch ops for keys that were added (i.e., in target but not
|
// The patch ops for keys that were added (i.e., in target but not
|
||||||
// in source); built here so the fast path below can reuse
|
// in source) are built here so the fast path can reuse
|
||||||
// them without a second source.find() per target key. Only
|
// them without a second source.find() per target key. Only
|
||||||
// used by the fast path -- the slow (reordering) path
|
// used by the fast path -- the slow (reordering) path
|
||||||
// rebuilds "add" ops for every key itself.
|
// rebuilds "add" ops for every key itself.
|
||||||
std::vector<typename object_t::key_type> common_keys_target_order;
|
std::vector<typename object_t::key_type> common_keys_target_order;
|
||||||
basic_json added_ops(value_t::array);
|
|
||||||
bool new_keys_form_suffix = true;
|
bool new_keys_form_suffix = true;
|
||||||
bool seen_new_key = false;
|
bool seen_new_key = false;
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
@@ -6170,12 +6213,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (source.find(it.key()) == source.end())
|
if (source.find(it.key()) == source.end())
|
||||||
{
|
{
|
||||||
seen_new_key = true;
|
seen_new_key = true;
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(added_ops, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
added_ops.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -6191,43 +6229,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{
|
{
|
||||||
// fast path: order of common keys already matches (or the
|
// fast path: order of common keys already matches (or the
|
||||||
// object_t's iteration order does not depend on
|
// object_t's iteration order does not depend on
|
||||||
// insertion history), so a plain per-key recursive diff
|
// insertion history), so a plain per-key diff is correct
|
||||||
// is correct and minimal, as before. common_keys_source_order
|
// and minimal, as before
|
||||||
// is, by construction, the subsequence of source's keys
|
common_keys = std::move(common_keys_source_order);
|
||||||
// that are common to both objects, in source's iteration
|
return true;
|
||||||
// order -- so it can be walked in lockstep with `source`
|
|
||||||
// using a cheap key comparison instead of another lookup.
|
|
||||||
// Deleted keys (those source keys not in common_keys_source_order)
|
|
||||||
// are interleaved here too, in source's original order, to
|
|
||||||
// match the historical (pre-reordering-aware) output order.
|
|
||||||
auto common_it = common_keys_source_order.cbegin();
|
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
|
||||||
{
|
|
||||||
if (common_it != common_keys_source_order.cend() && it.key() == *common_it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
auto temp_diff = diff(it.value(), target[it.key()], path_key);
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++common_it;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// found a key that is not in target -> remove it
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// append the "add" ops for brand-new keys collected above
|
|
||||||
// during the pass over target -- no second source.find()
|
|
||||||
// per target key needed
|
|
||||||
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// slow path: the common keys are in a different relative
|
// slow path: the common keys are in a different relative
|
||||||
// order in source and target (only possible for a
|
// order in source and target (only possible for a
|
||||||
// reorderable object_t like ordered_map). Building a
|
// reorderable object_t like ordered_map). Building a
|
||||||
@@ -6244,11 +6251,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// it moves it to the end, fixing its position.
|
// it moves it to the end, fixing its position.
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add every key that is either common (just removed
|
// add every key that is either common (just removed
|
||||||
@@ -6257,15 +6260,96 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// target exactly
|
// target exactly
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(result, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
result.push_back(
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief @ref diff, for values at nesting level @a depth, appending the
|
||||||
|
operations to @a result
|
||||||
|
|
||||||
|
Diffing two arrays or objects calls this function again, once per nesting
|
||||||
|
level, so values nested deeply enough used to exhaust the call stack and
|
||||||
|
terminate the process. The descent is bounded here: once @ref
|
||||||
|
detail::recursion_depth_limit levels have been entered, @ref
|
||||||
|
diff_iteratively diffs what is left without the call stack.
|
||||||
|
*/
|
||||||
|
static void diff_recursively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t depth)
|
||||||
{
|
{
|
||||||
{"op", "add"}, {"path", path_key},
|
// if the values are the same, there is nothing to do
|
||||||
{"value", it.value()}
|
if (source == target)
|
||||||
});
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(depth >= detail::recursion_depth_limit()))
|
||||||
|
{
|
||||||
|
diff_iteratively(result, source, target, path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (source.type() != target.type())
|
||||||
|
{
|
||||||
|
// different types: replace value
|
||||||
|
diff_replace(result, path, target);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (source.type())
|
||||||
|
{
|
||||||
|
case value_t::array:
|
||||||
|
{
|
||||||
|
// first pass: traverse common elements
|
||||||
|
std::size_t i = 0;
|
||||||
|
while (i < source.size() && i < target.size())
|
||||||
|
{
|
||||||
|
// recursive call to compare array values at index i
|
||||||
|
diff_recursively(result, source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)), depth + 1);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now reached the end of at least one array
|
||||||
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, source, target, path, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::object:
|
||||||
|
{
|
||||||
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
|
basic_json added_ops(value_t::array);
|
||||||
|
if (diff_object_keys(result, source, target, path, common_keys, added_ops))
|
||||||
|
{
|
||||||
|
// fast path: common_keys is, by construction, the
|
||||||
|
// subsequence of source's keys that are common to both
|
||||||
|
// objects, in source's iteration order -- so it can be
|
||||||
|
// walked in lockstep with `source` using a cheap key
|
||||||
|
// comparison instead of another lookup. Deleted keys
|
||||||
|
// (those source keys not in common_keys) are interleaved
|
||||||
|
// here too, in source's original order, to match the
|
||||||
|
// historical (pre-reordering-aware) output order.
|
||||||
|
auto common_it = common_keys.cbegin();
|
||||||
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
|
{
|
||||||
|
if (common_it != common_keys.cend() && it.key() == *common_it)
|
||||||
|
{
|
||||||
|
diff_recursively(result, it.value(), target[it.key()], detail::concat<string_t>(path, '/', detail::escape(it.key())), depth + 1);
|
||||||
|
++common_it;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// found a key that is not in target -> remove it
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append the "add" ops for brand-new keys collected by
|
||||||
|
// diff_object_keys -- no second source.find() per target
|
||||||
|
// key needed
|
||||||
|
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6280,16 +6364,170 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
// both primitive types: replace value
|
// both primitive types: replace value
|
||||||
result.push_back(
|
diff_replace(result, path, target);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief @ref diff without the call stack, appending the operations to
|
||||||
|
@a result
|
||||||
|
|
||||||
|
Produces the same operations as @ref diff_recursively. Only reached for
|
||||||
|
values nested more deeply than @ref detail::recursion_depth_limit.
|
||||||
|
*/
|
||||||
|
static void diff_iteratively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path)
|
||||||
|
{
|
||||||
|
// The arrays and objects being diffed are kept on an explicit stack,
|
||||||
|
// and every pair of elements is still diffed completely before the
|
||||||
|
// next one, so the operations come out in the same order as in
|
||||||
|
// diff_recursively. The path of the values being diffed is kept in
|
||||||
|
// one buffer that grows and shrinks with the stack, rather than in a
|
||||||
|
// new string per level.
|
||||||
|
std::vector<diff_frame> stack;
|
||||||
|
string_t current_path = path;
|
||||||
|
|
||||||
|
// diff `s` against `t`, whose path is current_path: primitives,
|
||||||
|
// values of different types, and objects whose members were reordered
|
||||||
|
// are handled right away; arrays and other objects get a frame
|
||||||
|
const auto enter = [&result, &stack, ¤t_path](const basic_json & s, const basic_json & t)
|
||||||
|
{
|
||||||
|
// if the values are the same, there is nothing to do. Arrays and
|
||||||
|
// objects are not compared up front: comparing them visits
|
||||||
|
// everything below them, so doing that at every level would take
|
||||||
|
// quadratic time in the nesting depth - equal ones yield no
|
||||||
|
// operations anyway.
|
||||||
|
if ((!s.is_structured() || !t.is_structured()) && s == t)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s.type() != t.type())
|
||||||
|
{
|
||||||
|
// different types: replace value
|
||||||
|
diff_replace(result, current_path, t);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (s.type())
|
||||||
|
{
|
||||||
|
case value_t::array:
|
||||||
|
{
|
||||||
|
stack.emplace_back(&s, &t, current_path.size());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::object:
|
||||||
|
{
|
||||||
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
|
basic_json added_ops(value_t::array);
|
||||||
|
if (diff_object_keys(result, s, t, current_path, common_keys, added_ops))
|
||||||
|
{
|
||||||
|
// fast path: the frame walks source in lockstep with
|
||||||
|
// common_keys, as diff_recursively does, and appends
|
||||||
|
// added_ops once all members are done
|
||||||
|
stack.emplace_back(&s, &t, current_path.size());
|
||||||
|
stack.back().member = s.cbegin();
|
||||||
|
stack.back().common_keys = std::move(common_keys);
|
||||||
|
stack.back().added_ops = std::move(added_ops);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::null:
|
||||||
|
case value_t::string:
|
||||||
|
case value_t::boolean:
|
||||||
|
case value_t::number_integer:
|
||||||
|
case value_t::number_unsigned:
|
||||||
|
case value_t::number_float:
|
||||||
|
case value_t::binary:
|
||||||
|
case value_t::discarded:
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
// both primitive types: replace value
|
||||||
|
diff_replace(result, current_path, t);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
enter(source, target);
|
||||||
|
while (!stack.empty())
|
||||||
|
{
|
||||||
|
// the frame is copied out member by member and changed through
|
||||||
|
// stack.back(): enter() may push a frame and the end of the loop
|
||||||
|
// pops it, either of which would invalidate a reference to it
|
||||||
|
const basic_json* const s = stack.back().source;
|
||||||
|
const basic_json* const t = stack.back().target;
|
||||||
|
const std::size_t path_length = stack.back().path_length;
|
||||||
|
const std::size_t depth = stack.size();
|
||||||
|
|
||||||
|
if (s->is_array())
|
||||||
|
{
|
||||||
|
const auto& source_array = *s->m_data.m_value.array;
|
||||||
|
const auto& target_array = *t->m_data.m_value.array;
|
||||||
|
|
||||||
|
// first pass: traverse common elements
|
||||||
|
const std::size_t i = stack.back().index;
|
||||||
|
if (i < source_array.size() && i < target_array.size())
|
||||||
|
{
|
||||||
|
++stack.back().index;
|
||||||
|
detail::concat_into(current_path, '/', detail::to_string<string_t>(i));
|
||||||
|
enter(source_array[i], target_array[i]);
|
||||||
|
if (stack.size() == depth)
|
||||||
|
{
|
||||||
|
current_path.resize(path_length);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now reached the end of at least one array
|
||||||
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, *s, *t, current_path, i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const const_iterator it = stack.back().member;
|
||||||
|
if (it != s->cend())
|
||||||
|
{
|
||||||
|
++stack.back().member;
|
||||||
|
const std::size_t next_common = stack.back().next_common;
|
||||||
|
if (next_common < stack.back().common_keys.size() && it.key() == stack.back().common_keys[next_common])
|
||||||
|
{
|
||||||
|
++stack.back().next_common;
|
||||||
|
const basic_json& target_value = (*t)[it.key()];
|
||||||
|
detail::concat_into(current_path, '/', detail::escape(it.key()));
|
||||||
|
enter(it.value(), target_value);
|
||||||
|
if (stack.size() == depth)
|
||||||
|
{
|
||||||
|
current_path.resize(path_length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// found a key that is not in target -> remove it
|
||||||
|
diff_remove(result, detail::concat<string_t>(current_path, '/', detail::escape(it.key())));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// append the "add" ops for brand-new keys collected when the
|
||||||
|
// object was entered
|
||||||
|
result.insert(result.end(), stack.back().added_ops.begin(), stack.back().added_ops.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
// this array or object is done: continue with the one it is in
|
||||||
|
stack.pop_back();
|
||||||
|
if (!stack.empty())
|
||||||
|
{
|
||||||
|
current_path.resize(stack.back().path_length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|||||||
+384
-166
@@ -6571,10 +6571,6 @@ namespace detail
|
|||||||
* j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
|
* j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
|
||||||
* allocated value (e.g., a string). See bug issue
|
* allocated value (e.g., a string). See bug issue
|
||||||
* https://github.com/nlohmann/json/issues/2865 for more information.
|
* https://github.com/nlohmann/json/issues/2865 for more information.
|
||||||
*
|
|
||||||
* A value that has to be allocated is created before the old one is destroyed:
|
|
||||||
* were it the other way around, an exception while creating the new value would
|
|
||||||
* leave j with the type of the new value, but the pointer to the destroyed old one.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<value_t> struct external_constructor;
|
template<value_t> struct external_constructor;
|
||||||
@@ -6598,20 +6594,18 @@ struct external_constructor<value_t::string>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(s);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::string;
|
j.m_data.m_type = value_t::string;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = s;
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
|
static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(s));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::string;
|
j.m_data.m_type = value_t::string;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = std::move(s);
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6620,10 +6614,9 @@ struct external_constructor<value_t::string>
|
|||||||
int > = 0 >
|
int > = 0 >
|
||||||
static void construct(BasicJsonType& j, const CompatibleStringType& str)
|
static void construct(BasicJsonType& j, const CompatibleStringType& str)
|
||||||
{
|
{
|
||||||
auto* created = j.template create<typename BasicJsonType::string_t>(str);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::string;
|
j.m_data.m_type = value_t::string;
|
||||||
j.m_data.m_value.string = created;
|
j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -6634,20 +6627,18 @@ struct external_constructor<value_t::binary>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(b);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::binary;
|
j.m_data.m_type = value_t::binary;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = typename BasicJsonType::binary_t(b);
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
|
static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(b));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::binary;
|
j.m_data.m_type = value_t::binary;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -6697,10 +6688,9 @@ struct external_constructor<value_t::array>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(arr);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = arr;
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -6708,10 +6698,9 @@ struct external_constructor<value_t::array>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
|
static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(arr));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = std::move(arr);
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -6727,10 +6716,9 @@ struct external_constructor<value_t::array>
|
|||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
auto* created = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value.array = created;
|
j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -6738,17 +6726,15 @@ struct external_constructor<value_t::array>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const std::vector<bool>& arr)
|
static void construct(BasicJsonType& j, const std::vector<bool>& arr)
|
||||||
{
|
{
|
||||||
typename BasicJsonType::array_t elements;
|
|
||||||
elements.reserve(arr.size());
|
|
||||||
for (const bool x : arr)
|
|
||||||
{
|
|
||||||
elements.push_back(x);
|
|
||||||
}
|
|
||||||
const typename BasicJsonType::json_value value(std::move(elements));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = value_t::array;
|
||||||
j.set_parents();
|
j.m_data.m_value.array->reserve(arr.size());
|
||||||
|
for (const bool x : arr)
|
||||||
|
{
|
||||||
|
j.m_data.m_value.array->push_back(x);
|
||||||
|
j.set_parent(j.m_data.m_value.array->back());
|
||||||
|
}
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6756,12 +6742,11 @@ struct external_constructor<value_t::array>
|
|||||||
enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
|
enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
|
||||||
static void construct(BasicJsonType& j, const std::valarray<T>& arr)
|
static void construct(BasicJsonType& j, const std::valarray<T>& arr)
|
||||||
{
|
{
|
||||||
typename BasicJsonType::array_t elements(arr.size());
|
|
||||||
std::copy(std::begin(arr), std::end(arr), elements.begin());
|
|
||||||
const typename BasicJsonType::json_value value(std::move(elements));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = value_t::array;
|
||||||
|
j.m_data.m_value.array->resize(arr.size());
|
||||||
|
std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -6773,16 +6758,14 @@ struct external_constructor<value_t::array>
|
|||||||
enable_if_t<is_compatible_range_view<std::remove_cvref_t<CompatibleArrayType>>::value, int> = 0>
|
enable_if_t<is_compatible_range_view<std::remove_cvref_t<CompatibleArrayType>>::value, int> = 0>
|
||||||
static void construct(BasicJsonType& j, CompatibleArrayType && arr)
|
static void construct(BasicJsonType& j, CompatibleArrayType && arr)
|
||||||
{
|
{
|
||||||
typename BasicJsonType::array_t elements;
|
|
||||||
for (auto&& x : std::forward<CompatibleArrayType>(arr))
|
|
||||||
{
|
|
||||||
elements.push_back(x);
|
|
||||||
}
|
|
||||||
const typename BasicJsonType::json_value value(std::move(elements));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::array;
|
j.m_data.m_type = value_t::array;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = value_t::array;
|
||||||
j.set_parents();
|
for (auto&& x : std::forward<CompatibleArrayType>(arr))
|
||||||
|
{
|
||||||
|
j.m_data.m_value.array->push_back(x);
|
||||||
|
j.set_parent(j.m_data.m_value.array->back());
|
||||||
|
}
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -6794,10 +6777,9 @@ struct external_constructor<value_t::object>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
|
static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(obj);
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::object;
|
j.m_data.m_type = value_t::object;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = obj;
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -6805,10 +6787,9 @@ struct external_constructor<value_t::object>
|
|||||||
template<typename BasicJsonType>
|
template<typename BasicJsonType>
|
||||||
static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
|
static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
|
||||||
{
|
{
|
||||||
const typename BasicJsonType::json_value value(std::move(obj));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::object;
|
j.m_data.m_type = value_t::object;
|
||||||
j.m_data.m_value = value;
|
j.m_data.m_value = std::move(obj);
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -6820,10 +6801,9 @@ struct external_constructor<value_t::object>
|
|||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
auto* created = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
|
||||||
j.m_data.m_value.destroy(j.m_data.m_type);
|
j.m_data.m_value.destroy(j.m_data.m_type);
|
||||||
j.m_data.m_type = value_t::object;
|
j.m_data.m_type = value_t::object;
|
||||||
j.m_data.m_value.object = created;
|
j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
||||||
j.set_parents();
|
j.set_parents();
|
||||||
j.assert_invariant();
|
j.assert_invariant();
|
||||||
}
|
}
|
||||||
@@ -27605,8 +27585,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (is_an_object)
|
if (is_an_object)
|
||||||
{
|
{
|
||||||
// the initializer list is a list of pairs -> create an object
|
// the initializer list is a list of pairs -> create an object
|
||||||
m_data.m_value = value_t::object;
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value = value_t::object;
|
||||||
|
|
||||||
for (auto& element_ref : init)
|
for (auto& element_ref : init)
|
||||||
{
|
{
|
||||||
@@ -27628,8 +27608,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// the initializer list describes an array -> create an array
|
// the initializer list describes an array -> create an array
|
||||||
m_data.m_value.array = create<array_t>(init.begin(), init.end());
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value.array = create<array_t>(init.begin(), init.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
set_parents();
|
set_parents();
|
||||||
@@ -27642,8 +27622,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(const typename binary_t::container_type& init)
|
static basic_json binary(const typename binary_t::container_type& init)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = init;
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = init;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27653,8 +27633,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
|
static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = binary_t(init, subtype);
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = binary_t(init, subtype);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27664,8 +27644,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(typename binary_t::container_type&& init)
|
static basic_json binary(typename binary_t::container_type&& init)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = std::move(init);
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = std::move(init);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27675,8 +27655,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
|
static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
|
||||||
{
|
{
|
||||||
auto res = basic_json();
|
auto res = basic_json();
|
||||||
res.m_data.m_value = binary_t(std::move(init), subtype);
|
|
||||||
res.m_data.m_type = value_t::binary;
|
res.m_data.m_type = value_t::binary;
|
||||||
|
res.m_data.m_value = binary_t(std::move(init), subtype);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28722,8 +28702,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty array
|
// implicitly convert a null value to an empty array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.array = create<array_t>();
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value.array = create<array_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28782,8 +28762,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty object
|
// implicitly convert a null value to an empty object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.object = create<object_t>();
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value.object = create<object_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28835,8 +28815,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty object
|
// implicitly convert a null value to an empty object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.object = create<object_t>();
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value.object = create<object_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29771,8 +29751,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an array
|
// transform a null object into an array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::array;
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value = value_t::array;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29804,8 +29784,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an array
|
// transform a null object into an array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::array;
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value = value_t::array;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29836,8 +29816,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an object
|
// transform a null object into an object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::object;
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value = value_t::object;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29892,8 +29872,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an array
|
// transform a null object into an array
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::array;
|
|
||||||
m_data.m_type = value_t::array;
|
m_data.m_type = value_t::array;
|
||||||
|
m_data.m_value = value_t::array;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29917,8 +29897,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// transform a null object into an object
|
// transform a null object into an object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value = value_t::object;
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value = value_t::object;
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30099,8 +30079,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// implicitly convert a null value to an empty object
|
// implicitly convert a null value to an empty object
|
||||||
if (is_null())
|
if (is_null())
|
||||||
{
|
{
|
||||||
m_data.m_value.object = create<object_t>();
|
|
||||||
m_data.m_type = value_t::object;
|
m_data.m_type = value_t::object;
|
||||||
|
m_data.m_value.object = create<object_t>();
|
||||||
assert_invariant();
|
assert_invariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31964,68 +31944,112 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{
|
{
|
||||||
// the patch
|
// the patch
|
||||||
basic_json result(value_t::array);
|
basic_json result(value_t::array);
|
||||||
|
diff_recursively(result, source, target, path, 0);
|
||||||
// if the values are the same, return an empty patch
|
|
||||||
if (source == target)
|
|
||||||
{
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source.type() != target.type())
|
private:
|
||||||
|
/// @brief two arrays or two objects @ref diff_iteratively is diffing
|
||||||
|
struct diff_frame
|
||||||
|
{
|
||||||
|
diff_frame(const basic_json* source_, const basic_json* target_, const std::size_t path_length_) noexcept
|
||||||
|
: source(source_), target(target_), path_length(path_length_)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// declared for GCC's -Weffc++, which asks for them in a class with
|
||||||
|
// pointer members and a non-trivial destructor; the exception
|
||||||
|
// specifications are left implicit, as GCC 4.8 rejects explicit ones
|
||||||
|
// that differ from them
|
||||||
|
diff_frame(const diff_frame&) = default;
|
||||||
|
diff_frame(diff_frame&&) = default;
|
||||||
|
diff_frame& operator=(const diff_frame&) = default;
|
||||||
|
diff_frame& operator=(diff_frame&&) = default;
|
||||||
|
~diff_frame() = default;
|
||||||
|
|
||||||
|
/// the values being diffed, both arrays or both objects
|
||||||
|
const basic_json* source;
|
||||||
|
const basic_json* target;
|
||||||
|
/// the length of their path in `current_path`
|
||||||
|
std::size_t path_length;
|
||||||
|
/// arrays: the next index to diff
|
||||||
|
std::size_t index = 0;
|
||||||
|
/// objects: the next member of source to look at
|
||||||
|
const_iterator member{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
/// objects: the keys common to both, in source's order
|
||||||
|
std::vector<typename object_t::key_type> common_keys{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
/// objects: the next entry of common_keys
|
||||||
|
std::size_t next_common = 0;
|
||||||
|
/// objects: the "add" operations for keys only target has
|
||||||
|
basic_json added_ops{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
};
|
||||||
|
|
||||||
|
// The operations of a diff are built by the functions below rather than
|
||||||
|
// where they are needed: building one takes several temporaries, and
|
||||||
|
// unoptimized builds give each temporary a stack slot of its own in the
|
||||||
|
// function it appears in. In diff_recursively, which is on the call stack
|
||||||
|
// once per nesting level, that made every level cost kilobytes of stack.
|
||||||
|
|
||||||
|
/// @brief append a "replace" operation for @a path with @a value to @a result
|
||||||
|
static void diff_replace(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
{
|
{
|
||||||
// different types: replace value
|
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
{"op", "replace"}, {"path", path}, {"value", value}
|
||||||
});
|
});
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (source.type())
|
/// @brief append a "remove" operation for @a path to @a result
|
||||||
{
|
static void diff_remove(basic_json& result, const string_t& path)
|
||||||
case value_t::array:
|
|
||||||
{
|
|
||||||
// first pass: traverse common elements
|
|
||||||
std::size_t i = 0;
|
|
||||||
while (i < source.size() && i < target.size())
|
|
||||||
{
|
|
||||||
// recursive call to compare array values at index i
|
|
||||||
auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We now reached the end of at least one array
|
|
||||||
// in a second pass, traverse the remaining elements
|
|
||||||
|
|
||||||
// remove my remaining elements, highest index first; appending
|
|
||||||
// in that order avoids the quadratic reinsertion done before
|
|
||||||
for (std::size_t j = source.size(); j > i; --j)
|
|
||||||
{
|
{
|
||||||
result.push_back(object(
|
result.push_back(object(
|
||||||
{
|
{
|
||||||
{"op", "remove"},
|
{"op", "remove"}, {"path", path}
|
||||||
{"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1))}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
i = source.size();
|
|
||||||
|
|
||||||
// add other remaining elements
|
/// @brief append an "add" operation for @a path with @a value to @a result
|
||||||
while (i < target.size())
|
static void diff_add(basic_json& result, const string_t& path, const basic_json& value)
|
||||||
{
|
{
|
||||||
result.push_back(
|
result.push_back(
|
||||||
{
|
{
|
||||||
{"op", "add"},
|
{"op", "add"}, {"path", path}, {"value", value}
|
||||||
{"path", detail::concat<string_t>(path, "/-")},
|
|
||||||
{"value", target[i]}
|
|
||||||
});
|
});
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
/// @brief append the "remove" operations for the elements of array
|
||||||
|
/// @a source from @a index on, and the "add" operations for the
|
||||||
|
/// elements of array @a target from source's size on, to @a result
|
||||||
|
static void diff_array_tails(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t index)
|
||||||
|
{
|
||||||
|
// remove my remaining elements, highest index first; appending
|
||||||
|
// in that order avoids the quadratic reinsertion done before
|
||||||
|
for (std::size_t j = source.size(); j > index; --j)
|
||||||
|
{
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::to_string<string_t>(j - 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
case value_t::object:
|
// add other remaining elements
|
||||||
|
for (std::size_t i = source.size(); i < target.size(); ++i)
|
||||||
|
{
|
||||||
|
diff_add(result, detail::concat<string_t>(path, "/-"), target[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief compare the keys of objects @a source and @a target
|
||||||
|
|
||||||
|
If the keys both objects have are in the same order in both, and the keys
|
||||||
|
only @a target has come after them, stores the keys common to both in
|
||||||
|
source's order in @a common_keys, stores the "add" operations for the keys
|
||||||
|
only @a target has in @a added_ops, and returns true: the caller then diffs
|
||||||
|
the objects member by member. Otherwise, appends operations that remove
|
||||||
|
every member of @a source and add every member of @a target to @a result,
|
||||||
|
and returns false.
|
||||||
|
*/
|
||||||
|
static bool diff_object_keys(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, std::vector<typename object_t::key_type>& common_keys,
|
||||||
|
basic_json& added_ops)
|
||||||
{
|
{
|
||||||
// first pass: record, for every source key, whether it is
|
// first pass: record, for every source key, whether it is
|
||||||
// common to both objects (in source's iteration order) or
|
// common to both objects (in source's iteration order) or
|
||||||
@@ -32033,10 +32057,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// a by-product of the target.find() call already needed to
|
// a by-product of the target.find() call already needed to
|
||||||
// tell the two cases apart, so it adds no extra lookups. The
|
// tell the two cases apart, so it adds no extra lookups. The
|
||||||
// "remove" ops themselves are emitted later, interleaved
|
// "remove" ops themselves are emitted later, interleaved
|
||||||
// with the recursive per-key diffs in the fast path below,
|
// with the per-key diffs in the caller's fast path, to match
|
||||||
// to match source's original iteration order (as the
|
// source's original iteration order (as the original,
|
||||||
// original, pre-reordering-aware implementation did) instead
|
// pre-reordering-aware implementation did) instead of
|
||||||
// of grouping all removes before all recursive diffs.
|
// grouping all removes before all per-key diffs.
|
||||||
std::vector<typename object_t::key_type> common_keys_source_order;
|
std::vector<typename object_t::key_type> common_keys_source_order;
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
@@ -32052,20 +32076,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// source.find() call already needed to detect added keys. At
|
// source.find() call already needed to detect added keys. At
|
||||||
// the same time, determine whether every added key comes
|
// the same time, determine whether every added key comes
|
||||||
// after every common key in target's order (a precondition
|
// after every common key in target's order (a precondition
|
||||||
// for the fast path below, which only ever appends new keys
|
// for the fast path, which only ever appends new keys
|
||||||
// at the very end): for an object_t whose iteration order is
|
// at the very end): for an object_t whose iteration order is
|
||||||
// a pure function of the key set (e.g. the default std::map,
|
// a pure function of the key set (e.g. the default std::map,
|
||||||
// which always iterates in sorted key order), the order
|
// which always iterates in sorted key order), the order
|
||||||
// check further below is always true and this whole
|
// check further below is always true and this whole
|
||||||
// mechanism is effectively a no-op; it only matters for a
|
// mechanism is effectively a no-op; it only matters for a
|
||||||
// reorderable object_t such as the one backing `ordered_json`.
|
// reorderable object_t such as the one backing `ordered_json`.
|
||||||
// patch ops for keys that were added (i.e., in target but not
|
// The patch ops for keys that were added (i.e., in target but not
|
||||||
// in source); built here so the fast path below can reuse
|
// in source) are built here so the fast path can reuse
|
||||||
// them without a second source.find() per target key. Only
|
// them without a second source.find() per target key. Only
|
||||||
// used by the fast path -- the slow (reordering) path
|
// used by the fast path -- the slow (reordering) path
|
||||||
// rebuilds "add" ops for every key itself.
|
// rebuilds "add" ops for every key itself.
|
||||||
std::vector<typename object_t::key_type> common_keys_target_order;
|
std::vector<typename object_t::key_type> common_keys_target_order;
|
||||||
basic_json added_ops(value_t::array);
|
|
||||||
bool new_keys_form_suffix = true;
|
bool new_keys_form_suffix = true;
|
||||||
bool seen_new_key = false;
|
bool seen_new_key = false;
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
@@ -32073,12 +32096,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
if (source.find(it.key()) == source.end())
|
if (source.find(it.key()) == source.end())
|
||||||
{
|
{
|
||||||
seen_new_key = true;
|
seen_new_key = true;
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(added_ops, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
added_ops.push_back(
|
|
||||||
{
|
|
||||||
{"op", "add"}, {"path", path_key},
|
|
||||||
{"value", it.value()}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -32094,43 +32112,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
{
|
{
|
||||||
// fast path: order of common keys already matches (or the
|
// fast path: order of common keys already matches (or the
|
||||||
// object_t's iteration order does not depend on
|
// object_t's iteration order does not depend on
|
||||||
// insertion history), so a plain per-key recursive diff
|
// insertion history), so a plain per-key diff is correct
|
||||||
// is correct and minimal, as before. common_keys_source_order
|
// and minimal, as before
|
||||||
// is, by construction, the subsequence of source's keys
|
common_keys = std::move(common_keys_source_order);
|
||||||
// that are common to both objects, in source's iteration
|
return true;
|
||||||
// order -- so it can be walked in lockstep with `source`
|
|
||||||
// using a cheap key comparison instead of another lookup.
|
|
||||||
// Deleted keys (those source keys not in common_keys_source_order)
|
|
||||||
// are interleaved here too, in source's original order, to
|
|
||||||
// match the historical (pre-reordering-aware) output order.
|
|
||||||
auto common_it = common_keys_source_order.cbegin();
|
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
|
||||||
{
|
|
||||||
if (common_it != common_keys_source_order.cend() && it.key() == *common_it)
|
|
||||||
{
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
auto temp_diff = diff(it.value(), target[it.key()], path_key);
|
|
||||||
result.insert(result.end(), temp_diff.begin(), temp_diff.end());
|
|
||||||
++common_it;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// found a key that is not in target -> remove it
|
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// append the "add" ops for brand-new keys collected above
|
|
||||||
// during the pass over target -- no second source.find()
|
|
||||||
// per target key needed
|
|
||||||
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// slow path: the common keys are in a different relative
|
// slow path: the common keys are in a different relative
|
||||||
// order in source and target (only possible for a
|
// order in source and target (only possible for a
|
||||||
// reorderable object_t like ordered_map). Building a
|
// reorderable object_t like ordered_map). Building a
|
||||||
@@ -32147,11 +32134,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// it moves it to the end, fixing its position.
|
// it moves it to the end, fixing its position.
|
||||||
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
result.push_back(object(
|
|
||||||
{
|
|
||||||
{"op", "remove"}, {"path", path_key}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add every key that is either common (just removed
|
// add every key that is either common (just removed
|
||||||
@@ -32160,15 +32143,96 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// target exactly
|
// target exactly
|
||||||
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
for (auto it = target.cbegin(); it != target.cend(); ++it)
|
||||||
{
|
{
|
||||||
const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
|
diff_add(result, detail::concat<string_t>(path, '/', detail::escape(it.key())), it.value());
|
||||||
result.push_back(
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief @ref diff, for values at nesting level @a depth, appending the
|
||||||
|
operations to @a result
|
||||||
|
|
||||||
|
Diffing two arrays or objects calls this function again, once per nesting
|
||||||
|
level, so values nested deeply enough used to exhaust the call stack and
|
||||||
|
terminate the process. The descent is bounded here: once @ref
|
||||||
|
detail::recursion_depth_limit levels have been entered, @ref
|
||||||
|
diff_iteratively diffs what is left without the call stack.
|
||||||
|
*/
|
||||||
|
static void diff_recursively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path, const std::size_t depth)
|
||||||
{
|
{
|
||||||
{"op", "add"}, {"path", path_key},
|
// if the values are the same, there is nothing to do
|
||||||
{"value", it.value()}
|
if (source == target)
|
||||||
});
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(depth >= detail::recursion_depth_limit()))
|
||||||
|
{
|
||||||
|
diff_iteratively(result, source, target, path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (source.type() != target.type())
|
||||||
|
{
|
||||||
|
// different types: replace value
|
||||||
|
diff_replace(result, path, target);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (source.type())
|
||||||
|
{
|
||||||
|
case value_t::array:
|
||||||
|
{
|
||||||
|
// first pass: traverse common elements
|
||||||
|
std::size_t i = 0;
|
||||||
|
while (i < source.size() && i < target.size())
|
||||||
|
{
|
||||||
|
// recursive call to compare array values at index i
|
||||||
|
diff_recursively(result, source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)), depth + 1);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now reached the end of at least one array
|
||||||
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, source, target, path, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::object:
|
||||||
|
{
|
||||||
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
|
basic_json added_ops(value_t::array);
|
||||||
|
if (diff_object_keys(result, source, target, path, common_keys, added_ops))
|
||||||
|
{
|
||||||
|
// fast path: common_keys is, by construction, the
|
||||||
|
// subsequence of source's keys that are common to both
|
||||||
|
// objects, in source's iteration order -- so it can be
|
||||||
|
// walked in lockstep with `source` using a cheap key
|
||||||
|
// comparison instead of another lookup. Deleted keys
|
||||||
|
// (those source keys not in common_keys) are interleaved
|
||||||
|
// here too, in source's original order, to match the
|
||||||
|
// historical (pre-reordering-aware) output order.
|
||||||
|
auto common_it = common_keys.cbegin();
|
||||||
|
for (auto it = source.cbegin(); it != source.cend(); ++it)
|
||||||
|
{
|
||||||
|
if (common_it != common_keys.cend() && it.key() == *common_it)
|
||||||
|
{
|
||||||
|
diff_recursively(result, it.value(), target[it.key()], detail::concat<string_t>(path, '/', detail::escape(it.key())), depth + 1);
|
||||||
|
++common_it;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// found a key that is not in target -> remove it
|
||||||
|
diff_remove(result, detail::concat<string_t>(path, '/', detail::escape(it.key())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append the "add" ops for brand-new keys collected by
|
||||||
|
// diff_object_keys -- no second source.find() per target
|
||||||
|
// key needed
|
||||||
|
result.insert(result.end(), added_ops.begin(), added_ops.end());
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32183,16 +32247,170 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
// both primitive types: replace value
|
// both primitive types: replace value
|
||||||
result.push_back(
|
diff_replace(result, path, target);
|
||||||
{
|
|
||||||
{"op", "replace"}, {"path", path}, {"value", target}
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief @ref diff without the call stack, appending the operations to
|
||||||
|
@a result
|
||||||
|
|
||||||
|
Produces the same operations as @ref diff_recursively. Only reached for
|
||||||
|
values nested more deeply than @ref detail::recursion_depth_limit.
|
||||||
|
*/
|
||||||
|
static void diff_iteratively(basic_json& result, const basic_json& source, const basic_json& target,
|
||||||
|
const string_t& path)
|
||||||
|
{
|
||||||
|
// The arrays and objects being diffed are kept on an explicit stack,
|
||||||
|
// and every pair of elements is still diffed completely before the
|
||||||
|
// next one, so the operations come out in the same order as in
|
||||||
|
// diff_recursively. The path of the values being diffed is kept in
|
||||||
|
// one buffer that grows and shrinks with the stack, rather than in a
|
||||||
|
// new string per level.
|
||||||
|
std::vector<diff_frame> stack;
|
||||||
|
string_t current_path = path;
|
||||||
|
|
||||||
|
// diff `s` against `t`, whose path is current_path: primitives,
|
||||||
|
// values of different types, and objects whose members were reordered
|
||||||
|
// are handled right away; arrays and other objects get a frame
|
||||||
|
const auto enter = [&result, &stack, ¤t_path](const basic_json & s, const basic_json & t)
|
||||||
|
{
|
||||||
|
// if the values are the same, there is nothing to do. Arrays and
|
||||||
|
// objects are not compared up front: comparing them visits
|
||||||
|
// everything below them, so doing that at every level would take
|
||||||
|
// quadratic time in the nesting depth - equal ones yield no
|
||||||
|
// operations anyway.
|
||||||
|
if ((!s.is_structured() || !t.is_structured()) && s == t)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s.type() != t.type())
|
||||||
|
{
|
||||||
|
// different types: replace value
|
||||||
|
diff_replace(result, current_path, t);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (s.type())
|
||||||
|
{
|
||||||
|
case value_t::array:
|
||||||
|
{
|
||||||
|
stack.emplace_back(&s, &t, current_path.size());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::object:
|
||||||
|
{
|
||||||
|
std::vector<typename object_t::key_type> common_keys;
|
||||||
|
basic_json added_ops(value_t::array);
|
||||||
|
if (diff_object_keys(result, s, t, current_path, common_keys, added_ops))
|
||||||
|
{
|
||||||
|
// fast path: the frame walks source in lockstep with
|
||||||
|
// common_keys, as diff_recursively does, and appends
|
||||||
|
// added_ops once all members are done
|
||||||
|
stack.emplace_back(&s, &t, current_path.size());
|
||||||
|
stack.back().member = s.cbegin();
|
||||||
|
stack.back().common_keys = std::move(common_keys);
|
||||||
|
stack.back().added_ops = std::move(added_ops);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case value_t::null:
|
||||||
|
case value_t::string:
|
||||||
|
case value_t::boolean:
|
||||||
|
case value_t::number_integer:
|
||||||
|
case value_t::number_unsigned:
|
||||||
|
case value_t::number_float:
|
||||||
|
case value_t::binary:
|
||||||
|
case value_t::discarded:
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
// both primitive types: replace value
|
||||||
|
diff_replace(result, current_path, t);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
enter(source, target);
|
||||||
|
while (!stack.empty())
|
||||||
|
{
|
||||||
|
// the frame is copied out member by member and changed through
|
||||||
|
// stack.back(): enter() may push a frame and the end of the loop
|
||||||
|
// pops it, either of which would invalidate a reference to it
|
||||||
|
const basic_json* const s = stack.back().source;
|
||||||
|
const basic_json* const t = stack.back().target;
|
||||||
|
const std::size_t path_length = stack.back().path_length;
|
||||||
|
const std::size_t depth = stack.size();
|
||||||
|
|
||||||
|
if (s->is_array())
|
||||||
|
{
|
||||||
|
const auto& source_array = *s->m_data.m_value.array;
|
||||||
|
const auto& target_array = *t->m_data.m_value.array;
|
||||||
|
|
||||||
|
// first pass: traverse common elements
|
||||||
|
const std::size_t i = stack.back().index;
|
||||||
|
if (i < source_array.size() && i < target_array.size())
|
||||||
|
{
|
||||||
|
++stack.back().index;
|
||||||
|
detail::concat_into(current_path, '/', detail::to_string<string_t>(i));
|
||||||
|
enter(source_array[i], target_array[i]);
|
||||||
|
if (stack.size() == depth)
|
||||||
|
{
|
||||||
|
current_path.resize(path_length);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now reached the end of at least one array
|
||||||
|
// in a second pass, traverse the remaining elements
|
||||||
|
diff_array_tails(result, *s, *t, current_path, i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const const_iterator it = stack.back().member;
|
||||||
|
if (it != s->cend())
|
||||||
|
{
|
||||||
|
++stack.back().member;
|
||||||
|
const std::size_t next_common = stack.back().next_common;
|
||||||
|
if (next_common < stack.back().common_keys.size() && it.key() == stack.back().common_keys[next_common])
|
||||||
|
{
|
||||||
|
++stack.back().next_common;
|
||||||
|
const basic_json& target_value = (*t)[it.key()];
|
||||||
|
detail::concat_into(current_path, '/', detail::escape(it.key()));
|
||||||
|
enter(it.value(), target_value);
|
||||||
|
if (stack.size() == depth)
|
||||||
|
{
|
||||||
|
current_path.resize(path_length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// found a key that is not in target -> remove it
|
||||||
|
diff_remove(result, detail::concat<string_t>(current_path, '/', detail::escape(it.key())));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// append the "add" ops for brand-new keys collected when the
|
||||||
|
// object was entered
|
||||||
|
result.insert(result.end(), stack.back().added_ops.begin(), stack.back().added_ops.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
// this array or object is done: continue with the one it is in
|
||||||
|
stack.pop_back();
|
||||||
|
if (!stack.empty())
|
||||||
|
{
|
||||||
|
current_path.resize(stack.back().path_length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|||||||
@@ -12,11 +12,6 @@
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
#include <valarray>
|
|
||||||
#if JSON_HAS_RANGES
|
|
||||||
#include <ranges>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// special test case to check if memory is leaked if constructor throws
|
// special test case to check if memory is leaked if constructor throws
|
||||||
@@ -399,173 +394,3 @@ TEST_CASE("bad my_allocator::construct")
|
|||||||
j["test"].push_back("should not leak");
|
j["test"].push_back("should not leak");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// the no-exceptions CI job skips every CHECK_THROWS_AS, which would leave
|
|
||||||
// next_construct_fails set for the next allocation outside a check
|
|
||||||
#if !defined(JSON_NOEXCEPTION)
|
|
||||||
TEST_CASE("a failed allocation leaves the value unchanged")
|
|
||||||
{
|
|
||||||
// create JSON type using the throwing allocator
|
|
||||||
using my_json = nlohmann::basic_json<std::map,
|
|
||||||
std::vector,
|
|
||||||
std::string,
|
|
||||||
bool,
|
|
||||||
std::int64_t,
|
|
||||||
std::uint64_t,
|
|
||||||
double,
|
|
||||||
my_allocator>;
|
|
||||||
|
|
||||||
// Each of these creates a string, array, object, or binary value. The
|
|
||||||
// value must be created before the type is changed: otherwise, a failed
|
|
||||||
// creation left a value of the new type without anything behind it (an
|
|
||||||
// assertion in its destructor, a null pointer everywhere else) or, when
|
|
||||||
// an old value was destroyed first, with a pointer to that destroyed one.
|
|
||||||
|
|
||||||
SECTION("creating a binary value")
|
|
||||||
{
|
|
||||||
const std::vector<std::uint8_t> bytes = {1, 2, 3};
|
|
||||||
my_json _;
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(_ = my_json::binary(bytes), std::bad_alloc&);
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(_ = my_json::binary(bytes, 42), std::bad_alloc&);
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(_ = my_json::binary(std::vector<std::uint8_t>(bytes)), std::bad_alloc&);
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(_ = my_json::binary(std::vector<std::uint8_t>(bytes), 42), std::bad_alloc&);
|
|
||||||
next_construct_fails = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("turning a null value into an array or object")
|
|
||||||
{
|
|
||||||
my_json j;
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j[0], std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j["key"], std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j[std::string_view("key")], std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j.push_back(my_json(1)), std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
const my_json one = 1;
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j.push_back(one), std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j.push_back(my_json::object_t::value_type("key", 1)), std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j.emplace_back(1), std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j.emplace("key", 1), std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
const my_json object = {{"key", 1}};
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(j.update(object), std::bad_alloc&);
|
|
||||||
CHECK(j.is_null());
|
|
||||||
|
|
||||||
next_construct_fails = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("converting into an existing value")
|
|
||||||
{
|
|
||||||
// to_json replaces the value it is given; the old one must survive a
|
|
||||||
// failed creation of the new one
|
|
||||||
my_json j = "old";
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, std::string("new")), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, std::vector<int> {1, 2}), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
// with iterator debugging, the default constructor of VS 2015's
|
|
||||||
// std::vector is noexcept but constructs a proxy with the allocator,
|
|
||||||
// which terminates when this allocator throws
|
|
||||||
#if !(defined(_MSC_VER) && _MSC_VER < 1910 && defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL > 0)
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, std::vector<bool> {true, false}), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, std::map<std::string, int> {{"a", 1}}), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, my_json::binary_t({1, 2})), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
// the overloads for lvalues of the value types, for the value types
|
|
||||||
// themselves, and for the remaining compatible types
|
|
||||||
const std::string string = "new";
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, string), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, "new"), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
// to_json only moves a binary value that it converted from another
|
|
||||||
// container type, which my_json's std::vector<std::uint8_t> is not
|
|
||||||
using binary_constructor = nlohmann::detail::external_constructor<nlohmann::detail::value_t::binary>;
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(binary_constructor::construct(j, my_json::binary_t({1, 2})), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
my_json::array_t array = {1, 2};
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, array), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, std::move(array)), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
my_json::object_t object = {{"a", 1}};
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, object), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, std::move(object)), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, std::valarray<int> {1, 2}), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
|
|
||||||
#if JSON_HAS_RANGES && !defined(__MINGW32__)
|
|
||||||
const std::vector<int> numbers = {1, 2};
|
|
||||||
next_construct_fails = true;
|
|
||||||
CHECK_THROWS_AS(nlohmann::to_json(j, numbers | std::views::filter([](int /*unused*/)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
})), std::bad_alloc&);
|
|
||||||
CHECK(j == "old");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
next_construct_fails = false;
|
|
||||||
nlohmann::to_json(j, std::vector<int> {1, 2});
|
|
||||||
CHECK(j == my_json({1, 2}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|||||||
@@ -15,8 +15,65 @@ using nlohmann::json;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
// alternating objects and arrays nested `depth` levels deep, with members that
|
||||||
|
// depend on `variant` at some levels, so diffing two variants yields
|
||||||
|
// operations on many levels: replacing the innermost value, adding, removing,
|
||||||
|
// and (for ordered_json) reordering members, and changing array lengths
|
||||||
|
template<typename BasicJsonType>
|
||||||
|
BasicJsonType nested(const std::size_t depth, const int variant)
|
||||||
|
{
|
||||||
|
BasicJsonType value = variant;
|
||||||
|
for (std::size_t i = 0; i < depth; ++i)
|
||||||
|
{
|
||||||
|
if (i % 2 == 0)
|
||||||
|
{
|
||||||
|
BasicJsonType object = BasicJsonType::object();
|
||||||
|
if ((i + static_cast<std::size_t>(variant)) % 7 == 0)
|
||||||
|
{
|
||||||
|
object["x"] = i;
|
||||||
|
}
|
||||||
|
if (variant == 2 && i % 11 == 0)
|
||||||
|
{
|
||||||
|
object["z"] = "z";
|
||||||
|
}
|
||||||
|
object["a"] = std::move(value);
|
||||||
|
if (variant == 1 && i % 5 == 0)
|
||||||
|
{
|
||||||
|
object["y"] = 1;
|
||||||
|
}
|
||||||
|
value = std::move(object);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BasicJsonType array = BasicJsonType::array({std::move(value)});
|
||||||
|
if ((i + static_cast<std::size_t>(variant)) % 3 == 0)
|
||||||
|
{
|
||||||
|
array.push_back(i);
|
||||||
|
}
|
||||||
|
value = std::move(array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a path of `depth` reference tokens, as nested() nests its values
|
||||||
|
std::string nested_path(const std::size_t depth)
|
||||||
|
{
|
||||||
|
std::string path;
|
||||||
|
for (std::size_t i = depth; i > 0; --i)
|
||||||
|
{
|
||||||
|
path += (i - 1) % 2 == 0 ? "/a" : "/0";
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
TEST_CASE("JSON patch")
|
TEST_CASE("JSON patch")
|
||||||
{
|
{
|
||||||
SECTION("examples from RFC 6902")
|
SECTION("examples from RFC 6902")
|
||||||
@@ -1752,6 +1809,102 @@ TEST_CASE("JSON patch - diff emits array removals in descending index order")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("JSON patch: diff of deeply nested values")
|
||||||
|
{
|
||||||
|
SECTION("the diff reproduces the target at every depth")
|
||||||
|
{
|
||||||
|
// depths on either side of the nesting depth up to which diff()
|
||||||
|
// recurses (detail::recursion_depth_limit(), 128); not every depth up
|
||||||
|
// to 300, as the test would then time out under Valgrind
|
||||||
|
std::vector<std::size_t> depths;
|
||||||
|
for (std::size_t depth = 0; depth <= 16; ++depth)
|
||||||
|
{
|
||||||
|
depths.push_back(depth);
|
||||||
|
}
|
||||||
|
for (std::size_t depth = 120; depth <= 136; ++depth)
|
||||||
|
{
|
||||||
|
depths.push_back(depth);
|
||||||
|
}
|
||||||
|
depths.push_back(300);
|
||||||
|
|
||||||
|
for (const auto depth : depths)
|
||||||
|
{
|
||||||
|
CAPTURE(depth);
|
||||||
|
for (int from = 0; from < 3; ++from)
|
||||||
|
{
|
||||||
|
for (int to = 0; to < 3; ++to)
|
||||||
|
{
|
||||||
|
CAPTURE(from);
|
||||||
|
CAPTURE(to);
|
||||||
|
const auto source = nested<json>(depth, from);
|
||||||
|
const auto target = nested<json>(depth, to);
|
||||||
|
const auto patch = json::diff(source, target);
|
||||||
|
CHECK(source.patch(patch) == target);
|
||||||
|
CHECK(patch.empty() == (from == to));
|
||||||
|
|
||||||
|
const auto ordered_source = nested<nlohmann::ordered_json>(depth, from);
|
||||||
|
const auto ordered_target = nested<nlohmann::ordered_json>(depth, to);
|
||||||
|
CHECK(ordered_source.patch(nlohmann::ordered_json::diff(ordered_source, ordered_target)) == ordered_target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a difference only in the innermost value is one replace operation")
|
||||||
|
{
|
||||||
|
for (std::size_t depth = 0; depth <= 300; ++depth)
|
||||||
|
{
|
||||||
|
CAPTURE(depth);
|
||||||
|
json source = 1;
|
||||||
|
json target = 2;
|
||||||
|
for (std::size_t i = 0; i < depth; ++i)
|
||||||
|
{
|
||||||
|
source = i % 2 == 0 ? json::object({{"a", std::move(source)}}) : json::array({std::move(source)});
|
||||||
|
target = i % 2 == 0 ? json::object({{"a", std::move(target)}}) : json::array({std::move(target)});
|
||||||
|
}
|
||||||
|
CHECK(json::diff(source, target, "/root") == json::array({{{"op", "replace"}, {"path", "/root" + nested_path(depth)}, {"value", 2}}}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("values nested too deeply for the call stack (#5393)")
|
||||||
|
{
|
||||||
|
// diff() used to recurse once per nesting level, and compared the
|
||||||
|
// values with operator== on every level. The values are only
|
||||||
|
// parsed and diffed, never copied or compared, since those recurse
|
||||||
|
// too.
|
||||||
|
const std::size_t depth = 100000;
|
||||||
|
for (const bool objects :
|
||||||
|
{
|
||||||
|
false, true
|
||||||
|
})
|
||||||
|
{
|
||||||
|
CAPTURE(objects);
|
||||||
|
std::string source_text;
|
||||||
|
std::string target_text;
|
||||||
|
std::string equal_text;
|
||||||
|
std::string path;
|
||||||
|
for (std::size_t i = 0; i < depth; ++i)
|
||||||
|
{
|
||||||
|
source_text += objects ? "{\"a\":" : "[";
|
||||||
|
path += objects ? "/a" : "/0";
|
||||||
|
}
|
||||||
|
target_text = source_text + "2";
|
||||||
|
equal_text = source_text + "1";
|
||||||
|
source_text += "1";
|
||||||
|
const std::string closing(depth, objects ? '}' : ']');
|
||||||
|
const auto source = json::parse(source_text + closing);
|
||||||
|
|
||||||
|
const auto patch = json::diff(source, json::parse(target_text + closing));
|
||||||
|
REQUIRE(patch.size() == 1);
|
||||||
|
CHECK(patch[0]["op"] == "replace");
|
||||||
|
CHECK(patch[0]["path"] == path);
|
||||||
|
CHECK(patch[0]["value"] == 2);
|
||||||
|
|
||||||
|
CHECK(json::diff(source, json::parse(equal_text + closing)).empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("JSON patch - every operation on ordered_json")
|
TEST_CASE("JSON patch - every operation on ordered_json")
|
||||||
{
|
{
|
||||||
using nlohmann::ordered_json;
|
using nlohmann::ordered_json;
|
||||||
|
|||||||
Reference in New Issue
Block a user