1
0
mirror of https://github.com/nlohmann/json.git synced 2025-08-07 18:02:57 +03:00

🚧 implement more parent relations

This commit is contained in:
Niels Lohmann
2021-01-02 21:36:11 +01:00
parent 7323a8eb4e
commit ec0b1798bc
6 changed files with 219 additions and 17 deletions

View File

@@ -298,12 +298,18 @@ class json_sax_dom_parser
if (ref_stack.back()->is_array())
{
ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
#if JSON_DIAGNOSTICS
ref_stack.back()->m_value.array->back().m_parent = ref_stack.back();
#endif
return &(ref_stack.back()->m_value.array->back());
}
JSON_ASSERT(ref_stack.back()->is_object());
JSON_ASSERT(object_element);
*object_element = BasicJsonType(std::forward<Value>(v));
#if JSON_DIAGNOSTICS
object_element->m_parent = ref_stack.back();
#endif
return object_element;
}
@@ -574,7 +580,10 @@ class json_sax_dom_callback_parser
// array
if (ref_stack.back()->is_array())
{
ref_stack.back()->m_value.array->push_back(std::move(value));
ref_stack.back()->m_value.array->emplace_back(std::move(value));
#if JSON_DIAGNOSTICS
ref_stack.back()->m_value.array->back().m_parent = ref_stack.back();
#endif
return {true, &(ref_stack.back()->m_value.array->back())};
}
@@ -592,6 +601,9 @@ class json_sax_dom_callback_parser
JSON_ASSERT(object_element);
*object_element = std::move(value);
#if JSON_DIAGNOSTICS
object_element->m_parent = ref_stack.back();
#endif
return {true, object_element};
}

View File

@@ -1872,6 +1872,12 @@ class basic_json
: m_type(value_t::array)
{
m_value.array = create<array_t>(cnt, val);
#if JSON_DIAGNOSTICS
for (auto& entry : *m_value.array)
{
entry.m_parent = this;
}
#endif
assert_invariant();
}
@@ -2004,6 +2010,12 @@ class basic_json
{
m_value.object = create<object_t>(first.m_it.object_iterator,
last.m_it.object_iterator);
#if JSON_DIAGNOSTICS
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
#endif
break;
}
@@ -2011,6 +2023,12 @@ class basic_json
{
m_value.array = create<array_t>(first.m_it.array_iterator,
last.m_it.array_iterator);
#if JSON_DIAGNOSTICS
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
break;
}
@@ -2074,12 +2092,24 @@ class basic_json
case value_t::object:
{
m_value = *other.m_value.object;
#if JSON_DIAGNOSTICS
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
#endif
break;
}
case value_t::array:
{
m_value = *other.m_value.array;
#if JSON_DIAGNOSTICS
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
break;
}
@@ -2205,6 +2235,9 @@ class basic_json
using std::swap;
swap(m_type, other.m_type);
swap(m_value, other.m_value);
#if JSON_DIAGNOSTICS
m_parent = other.m_parent;
#endif
assert_invariant();
return *this;
@@ -2229,6 +2262,9 @@ class basic_json
{
assert_invariant();
m_value.destroy(m_type);
#if JSON_DIAGNOSTICS
m_parent = nullptr;
#endif
}
/// @}
@@ -2751,7 +2787,7 @@ class basic_json
return "";
}
return "(" + std::accumulate(tokens.begin(), tokens.end(), std::string{},
return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
[](const std::string & a, const std::string & b)
{
return a + "/" + b;
@@ -3604,7 +3640,10 @@ class basic_json
idx - m_value.array->size() + 1,
basic_json());
#if JSON_DIAGNOSTICS
m_value.array->back().m_parent = this;
for (std::size_t i = idx + 1; i < m_value.array->size(); ++i)
{
m_value.array->operator[](i).m_parent = this;
}
#endif
}
@@ -5609,6 +5648,11 @@ class basic_json
// add element to array (perfect forwarding)
auto res = m_value.object->emplace(std::forward<Args>(args)...);
#if JSON_DIAGNOSTICS
res.first->second.m_parent = this;
#endif
// create result iterator and set iterator to the result of emplace
auto it = begin();
it.m_it.object_iterator = res.first;
@@ -5671,7 +5715,13 @@ class basic_json
}
// insert to array and return iterator
#if JSON_DIAGNOSTICS
iterator result = insert_iterator(pos, val);
result->m_parent = this;
return result;
#else
return insert_iterator(pos, val);
#endif
}
JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name())));
@@ -5722,7 +5772,16 @@ class basic_json
}
// insert to array and return iterator
#if JSON_DIAGNOSTICS
iterator result = insert_iterator(pos, cnt, val);
for (size_type i = 0; i < cnt; ++i)
{
(result + i)->m_parent = this;
}
return result;
#else
return insert_iterator(pos, cnt, val);
#endif
}
JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name())));
@@ -5784,7 +5843,16 @@ class basic_json
}
// insert to array and return iterator
#if JSON_DIAGNOSTICS
iterator result = insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
for (std::size_t i = 0; i < std::distance(first, last); ++i)
{
(result + i)->m_parent = this;
}
return result;
#else
return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
#endif
}
/*!
@@ -5826,7 +5894,17 @@ class basic_json
}
// insert to array and return iterator
#if JSON_DIAGNOSTICS
const auto size = ilist.size();
iterator result = insert_iterator(pos, ilist.begin(), ilist.end());
for (std::size_t i = 0; i < size; ++i)
{
(result + i)->m_parent = this;
}
return result;
#else
return insert_iterator(pos, ilist.begin(), ilist.end());
#endif
}
/*!
@@ -8387,7 +8465,7 @@ class basic_json
if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
{
// avoid undefined behavior
JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range"));
JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range"));
}
// default case: insert add offset
@@ -8548,7 +8626,7 @@ class basic_json
// throw an exception if test fails
if (JSON_HEDLEY_UNLIKELY(!success))
{
JSON_THROW(other_error::create(501, diagnostics() + "unsuccessful: " + val.dump()));
JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump()));
}
break;