mirror of
https://github.com/nlohmann/json.git
synced 2026-07-19 08:54:54 +00:00
🐛 fix invariants
This commit is contained in:
@@ -132,6 +132,7 @@ struct external_constructor<value_t::array>
|
||||
{
|
||||
j.m_type = value_t::array;
|
||||
j.m_value = arr;
|
||||
j.set_parent(j, true);
|
||||
j.assert_invariant();
|
||||
}
|
||||
|
||||
@@ -140,6 +141,7 @@ struct external_constructor<value_t::array>
|
||||
{
|
||||
j.m_type = value_t::array;
|
||||
j.m_value = std::move(arr);
|
||||
j.set_parent(j, true);
|
||||
j.assert_invariant();
|
||||
}
|
||||
|
||||
@@ -152,6 +154,7 @@ struct external_constructor<value_t::array>
|
||||
using std::end;
|
||||
j.m_type = value_t::array;
|
||||
j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
|
||||
j.set_parent(j, true);
|
||||
j.assert_invariant();
|
||||
}
|
||||
|
||||
@@ -164,6 +167,9 @@ struct external_constructor<value_t::array>
|
||||
for (const bool x : arr)
|
||||
{
|
||||
j.m_value.array->push_back(x);
|
||||
#if JSON_DIAGNOSTICS
|
||||
j.m_value.array->back().m_parent = &j;
|
||||
#endif
|
||||
}
|
||||
j.assert_invariant();
|
||||
}
|
||||
@@ -179,6 +185,7 @@ struct external_constructor<value_t::array>
|
||||
{
|
||||
std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
|
||||
}
|
||||
j.set_parent(j, true);
|
||||
j.assert_invariant();
|
||||
}
|
||||
};
|
||||
@@ -191,6 +198,7 @@ struct external_constructor<value_t::object>
|
||||
{
|
||||
j.m_type = value_t::object;
|
||||
j.m_value = obj;
|
||||
j.set_parent(j, true);
|
||||
j.assert_invariant();
|
||||
}
|
||||
|
||||
@@ -199,6 +207,7 @@ struct external_constructor<value_t::object>
|
||||
{
|
||||
j.m_type = value_t::object;
|
||||
j.m_value = std::move(obj);
|
||||
j.set_parent(j, true);
|
||||
j.assert_invariant();
|
||||
}
|
||||
|
||||
@@ -211,6 +220,7 @@ struct external_constructor<value_t::object>
|
||||
|
||||
j.m_type = value_t::object;
|
||||
j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
|
||||
j.set_parent(j, true);
|
||||
j.assert_invariant();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -236,6 +236,7 @@ class json_sax_dom_parser
|
||||
|
||||
bool end_object()
|
||||
{
|
||||
ref_stack.back()->set_parent(*ref_stack.back(), true);
|
||||
ref_stack.pop_back();
|
||||
return true;
|
||||
}
|
||||
@@ -254,6 +255,7 @@ class json_sax_dom_parser
|
||||
|
||||
bool end_array()
|
||||
{
|
||||
ref_stack.back()->set_parent(*ref_stack.back(), true);
|
||||
ref_stack.pop_back();
|
||||
return true;
|
||||
}
|
||||
@@ -298,18 +300,12 @@ 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;
|
||||
}
|
||||
|
||||
@@ -432,10 +428,17 @@ class json_sax_dom_callback_parser
|
||||
|
||||
bool end_object()
|
||||
{
|
||||
if (ref_stack.back() && !callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
|
||||
if (ref_stack.back())
|
||||
{
|
||||
// discard object
|
||||
*ref_stack.back() = discarded;
|
||||
if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
|
||||
{
|
||||
// discard object
|
||||
*ref_stack.back() = discarded;
|
||||
}
|
||||
else
|
||||
{
|
||||
ref_stack.back()->set_parent(*ref_stack.back(), true);
|
||||
}
|
||||
}
|
||||
|
||||
JSON_ASSERT(!ref_stack.empty());
|
||||
@@ -483,7 +486,11 @@ class json_sax_dom_callback_parser
|
||||
if (ref_stack.back())
|
||||
{
|
||||
keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
|
||||
if (!keep)
|
||||
if (keep)
|
||||
{
|
||||
ref_stack.back()->set_parent(*ref_stack.back(), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// discard array
|
||||
*ref_stack.back() = discarded;
|
||||
@@ -582,9 +589,6 @@ class json_sax_dom_callback_parser
|
||||
if (ref_stack.back()->is_array())
|
||||
{
|
||||
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())};
|
||||
}
|
||||
|
||||
@@ -602,9 +606,6 @@ 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};
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,6 @@ class parser
|
||||
{
|
||||
json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
|
||||
sax_parse_internal(&sdp);
|
||||
result.assert_invariant();
|
||||
|
||||
// in strict mode, input must be completely read
|
||||
if (strict && (get_token() != token_type::end_of_input))
|
||||
@@ -119,7 +118,6 @@ class parser
|
||||
{
|
||||
json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
|
||||
sax_parse_internal(&sdp);
|
||||
result.assert_invariant();
|
||||
|
||||
// in strict mode, input must be completely read
|
||||
if (strict && (get_token() != token_type::end_of_input))
|
||||
@@ -137,6 +135,8 @@ class parser
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
result.assert_invariant();
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
Reference in New Issue
Block a user