simplify code

parent 1d6ba22f
...@@ -1242,6 +1242,47 @@ class basic_json ...@@ -1242,6 +1242,47 @@ class basic_json
JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr);
} }
reference set_parent(reference j, bool recursive)
{
#if JSON_DIAGNOSTICS
if (recursive)
{
switch (m_type)
{
case value_t::array:
{
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
break;
}
case value_t::object:
{
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
break;
}
default:
break;
}
}
else
{
j.m_parent = this;
}
#else
static_cast<void>(j);
static_cast<void>(recursive);
#endif
return j;
}
public: public:
////////////////////////// //////////////////////////
// JSON parser callback // // JSON parser callback //
...@@ -1651,12 +1692,7 @@ class basic_json ...@@ -1651,12 +1692,7 @@ class basic_json
auto res = m_value.object->emplace( auto res = m_value.object->emplace(
std::move(*((*element.m_value.array)[0].m_value.string)), std::move(*((*element.m_value.array)[0].m_value.string)),
std::move((*element.m_value.array)[1])); std::move((*element.m_value.array)[1]));
set_parent(res.first->second, false);
#if JSON_DIAGNOSTICS
res.first->second.m_parent = this;
#else
static_cast<void>(res); // unused variable - fix warning
#endif
} }
} }
else else
...@@ -1664,12 +1700,7 @@ class basic_json ...@@ -1664,12 +1700,7 @@ class basic_json
// the initializer list describes an array -> create array // the initializer list describes an array -> create array
m_type = value_t::array; m_type = value_t::array;
m_value.array = create<array_t>(init.begin(), init.end()); m_value.array = create<array_t>(init.begin(), init.end());
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
} }
assert_invariant(); assert_invariant();
...@@ -1881,12 +1912,7 @@ class basic_json ...@@ -1881,12 +1912,7 @@ class basic_json
: m_type(value_t::array) : m_type(value_t::array)
{ {
m_value.array = create<array_t>(cnt, val); m_value.array = create<array_t>(cnt, val);
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
assert_invariant(); assert_invariant();
} }
...@@ -2019,12 +2045,7 @@ class basic_json ...@@ -2019,12 +2045,7 @@ class basic_json
{ {
m_value.object = create<object_t>(first.m_it.object_iterator, m_value.object = create<object_t>(first.m_it.object_iterator,
last.m_it.object_iterator); last.m_it.object_iterator);
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
#endif
break; break;
} }
...@@ -2032,12 +2053,7 @@ class basic_json ...@@ -2032,12 +2053,7 @@ class basic_json
{ {
m_value.array = create<array_t>(first.m_it.array_iterator, m_value.array = create<array_t>(first.m_it.array_iterator,
last.m_it.array_iterator); last.m_it.array_iterator);
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
break; break;
} }
...@@ -2100,24 +2116,14 @@ class basic_json ...@@ -2100,24 +2116,14 @@ class basic_json
case value_t::object: case value_t::object:
{ {
m_value = *other.m_value.object; m_value = *other.m_value.object;
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
#endif
break; break;
} }
case value_t::array: case value_t::array:
{ {
m_value = *other.m_value.array; m_value = *other.m_value.array;
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
break; break;
} }
...@@ -2201,32 +2207,7 @@ class basic_json ...@@ -2201,32 +2207,7 @@ class basic_json
other.m_type = value_t::null; other.m_type = value_t::null;
other.m_value = {}; other.m_value = {};
#if JSON_DIAGNOSTICS set_parent(*this, true);
switch (m_type)
{
case value_t::array:
{
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
break;
}
case value_t::object:
{
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
break;
}
default:
break;
}
#endif
assert_invariant(); assert_invariant();
} }
...@@ -3391,13 +3372,7 @@ class basic_json ...@@ -3391,13 +3372,7 @@ class basic_json
{ {
JSON_TRY JSON_TRY
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.array->at(idx), false);
reference result = m_value.array->at(idx);
result.m_parent = this;
return result;
#else
return m_value.array->at(idx);
#endif
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
...@@ -3495,13 +3470,7 @@ class basic_json ...@@ -3495,13 +3470,7 @@ class basic_json
{ {
JSON_TRY JSON_TRY
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.object->at(key), false);
reference result = m_value.object->at(key);
result.m_parent = this;
return result;
#else
return m_value.object->at(key);
#endif
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
...@@ -3699,13 +3668,7 @@ class basic_json ...@@ -3699,13 +3668,7 @@ class basic_json
// operator[] only works for objects // operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.object->operator[](key), false);
reference result = m_value.object->operator[](key);
result.m_parent = this;
return result;
#else
return m_value.object->operator[](key);
#endif
} }
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
...@@ -3795,13 +3758,7 @@ class basic_json ...@@ -3795,13 +3758,7 @@ class basic_json
// at only works for objects // at only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.object->operator[](key), false);
reference result = m_value.object->operator[](key);
result.m_parent = this;
return result;
#else
return m_value.object->operator[](key);
#endif
} }
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
...@@ -5357,9 +5314,7 @@ class basic_json ...@@ -5357,9 +5314,7 @@ class basic_json
// add element to array (move semantics) // add element to array (move semantics)
m_value.array->push_back(std::move(val)); m_value.array->push_back(std::move(val));
#if JSON_DIAGNOSTICS set_parent(m_value.array->back(), false);
m_value.array->back().m_parent = this;
#endif
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor // if val is moved from, basic_json move constructor marks it null so we do not call the destructor
} }
...@@ -5395,9 +5350,7 @@ class basic_json ...@@ -5395,9 +5350,7 @@ class basic_json
// add element to array // add element to array
m_value.array->push_back(val); m_value.array->push_back(val);
#if JSON_DIAGNOSTICS set_parent(m_value.array->back(), false);
m_value.array->back().m_parent = this;
#endif
} }
/*! /*!
...@@ -5447,12 +5400,8 @@ class basic_json ...@@ -5447,12 +5400,8 @@ class basic_json
} }
// add element to object // add element to object
#if JSON_DIAGNOSTICS
auto res = m_value.object->insert(val); auto res = m_value.object->insert(val);
res.first->second.m_parent = this; set_parent(res.first->second, false);
#else
m_value.object->insert(val);
#endif
} }
/*! /*!
...@@ -5556,19 +5505,10 @@ class basic_json ...@@ -5556,19 +5505,10 @@ class basic_json
// add element to array (perfect forwarding) // add element to array (perfect forwarding)
#ifdef JSON_HAS_CPP_17 #ifdef JSON_HAS_CPP_17
#if JSON_DIAGNOSTICS return set_parent(m_value.array->emplace_back(std::forward<Args>(args)...), false);
reference result = m_value.array->emplace_back(std::forward<Args>(args)...);
result.m_parent = this;
return result;
#else
return m_value.array->emplace_back(std::forward<Args>(args)...);
#endif
#else #else
m_value.array->emplace_back(std::forward<Args>(args)...); m_value.array->emplace_back(std::forward<Args>(args)...);
#if JSON_DIAGNOSTICS return set_parent(m_value.array->back(), false);
m_value.array->back().m_parent = this;
#endif
return m_value.array->back();
#endif #endif
} }
...@@ -5618,10 +5558,7 @@ class basic_json ...@@ -5618,10 +5558,7 @@ class basic_json
// add element to array (perfect forwarding) // add element to array (perfect forwarding)
auto res = m_value.object->emplace(std::forward<Args>(args)...); auto res = m_value.object->emplace(std::forward<Args>(args)...);
set_parent(res.first->second, false);
#if JSON_DIAGNOSTICS
res.first->second.m_parent = this;
#endif
// create result iterator and set iterator to the result of emplace // create result iterator and set iterator to the result of emplace
auto it = begin(); auto it = begin();
......
...@@ -17988,6 +17988,47 @@ class basic_json ...@@ -17988,6 +17988,47 @@ class basic_json
JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr);
} }
reference set_parent(reference j, bool recursive)
{
#if JSON_DIAGNOSTICS
if (recursive)
{
switch (m_type)
{
case value_t::array:
{
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
break;
}
case value_t::object:
{
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
break;
}
default:
break;
}
}
else
{
j.m_parent = this;
}
#else
static_cast<void>(j);
static_cast<void>(recursive);
#endif
return j;
}
public: public:
////////////////////////// //////////////////////////
// JSON parser callback // // JSON parser callback //
...@@ -18397,12 +18438,7 @@ class basic_json ...@@ -18397,12 +18438,7 @@ class basic_json
auto res = m_value.object->emplace( auto res = m_value.object->emplace(
std::move(*((*element.m_value.array)[0].m_value.string)), std::move(*((*element.m_value.array)[0].m_value.string)),
std::move((*element.m_value.array)[1])); std::move((*element.m_value.array)[1]));
set_parent(res.first->second, false);
#if JSON_DIAGNOSTICS
res.first->second.m_parent = this;
#else
static_cast<void>(res); // unused variable - fix warning
#endif
} }
} }
else else
...@@ -18410,12 +18446,7 @@ class basic_json ...@@ -18410,12 +18446,7 @@ class basic_json
// the initializer list describes an array -> create array // the initializer list describes an array -> create array
m_type = value_t::array; m_type = value_t::array;
m_value.array = create<array_t>(init.begin(), init.end()); m_value.array = create<array_t>(init.begin(), init.end());
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
} }
assert_invariant(); assert_invariant();
...@@ -18627,12 +18658,7 @@ class basic_json ...@@ -18627,12 +18658,7 @@ class basic_json
: m_type(value_t::array) : m_type(value_t::array)
{ {
m_value.array = create<array_t>(cnt, val); m_value.array = create<array_t>(cnt, val);
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
assert_invariant(); assert_invariant();
} }
...@@ -18765,12 +18791,7 @@ class basic_json ...@@ -18765,12 +18791,7 @@ class basic_json
{ {
m_value.object = create<object_t>(first.m_it.object_iterator, m_value.object = create<object_t>(first.m_it.object_iterator,
last.m_it.object_iterator); last.m_it.object_iterator);
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
#endif
break; break;
} }
...@@ -18778,12 +18799,7 @@ class basic_json ...@@ -18778,12 +18799,7 @@ class basic_json
{ {
m_value.array = create<array_t>(first.m_it.array_iterator, m_value.array = create<array_t>(first.m_it.array_iterator,
last.m_it.array_iterator); last.m_it.array_iterator);
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
break; break;
} }
...@@ -18846,24 +18862,14 @@ class basic_json ...@@ -18846,24 +18862,14 @@ class basic_json
case value_t::object: case value_t::object:
{ {
m_value = *other.m_value.object; m_value = *other.m_value.object;
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
#endif
break; break;
} }
case value_t::array: case value_t::array:
{ {
m_value = *other.m_value.array; m_value = *other.m_value.array;
#if JSON_DIAGNOSTICS set_parent(*this, true);
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
#endif
break; break;
} }
...@@ -18947,32 +18953,7 @@ class basic_json ...@@ -18947,32 +18953,7 @@ class basic_json
other.m_type = value_t::null; other.m_type = value_t::null;
other.m_value = {}; other.m_value = {};
#if JSON_DIAGNOSTICS set_parent(*this, true);
switch (m_type)
{
case value_t::array:
{
for (auto& element : *m_value.array)
{
element.m_parent = this;
}
break;
}
case value_t::object:
{
for (auto& element : *m_value.object)
{
element.second.m_parent = this;
}
break;
}
default:
break;
}
#endif
assert_invariant(); assert_invariant();
} }
...@@ -20137,13 +20118,7 @@ class basic_json ...@@ -20137,13 +20118,7 @@ class basic_json
{ {
JSON_TRY JSON_TRY
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.array->at(idx), false);
reference result = m_value.array->at(idx);
result.m_parent = this;
return result;
#else
return m_value.array->at(idx);
#endif
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
...@@ -20241,13 +20216,7 @@ class basic_json ...@@ -20241,13 +20216,7 @@ class basic_json
{ {
JSON_TRY JSON_TRY
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.object->at(key), false);
reference result = m_value.object->at(key);
result.m_parent = this;
return result;
#else
return m_value.object->at(key);
#endif
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
...@@ -20445,13 +20414,7 @@ class basic_json ...@@ -20445,13 +20414,7 @@ class basic_json
// operator[] only works for objects // operator[] only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.object->operator[](key), false);
reference result = m_value.object->operator[](key);
result.m_parent = this;
return result;
#else
return m_value.object->operator[](key);
#endif
} }
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
...@@ -20541,13 +20504,7 @@ class basic_json ...@@ -20541,13 +20504,7 @@ class basic_json
// at only works for objects // at only works for objects
if (JSON_HEDLEY_LIKELY(is_object())) if (JSON_HEDLEY_LIKELY(is_object()))
{ {
#if JSON_DIAGNOSTICS return set_parent(m_value.object->operator[](key), false);
reference result = m_value.object->operator[](key);
result.m_parent = this;
return result;
#else
return m_value.object->operator[](key);
#endif
} }
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this)));
...@@ -22103,9 +22060,7 @@ class basic_json ...@@ -22103,9 +22060,7 @@ class basic_json
// add element to array (move semantics) // add element to array (move semantics)
m_value.array->push_back(std::move(val)); m_value.array->push_back(std::move(val));
#if JSON_DIAGNOSTICS set_parent(m_value.array->back(), false);
m_value.array->back().m_parent = this;
#endif
// if val is moved from, basic_json move constructor marks it null so we do not call the destructor // if val is moved from, basic_json move constructor marks it null so we do not call the destructor
} }
...@@ -22141,9 +22096,7 @@ class basic_json ...@@ -22141,9 +22096,7 @@ class basic_json
// add element to array // add element to array
m_value.array->push_back(val); m_value.array->push_back(val);
#if JSON_DIAGNOSTICS set_parent(m_value.array->back(), false);
m_value.array->back().m_parent = this;
#endif
} }
/*! /*!
...@@ -22193,12 +22146,8 @@ class basic_json ...@@ -22193,12 +22146,8 @@ class basic_json
} }
// add element to object // add element to object
#if JSON_DIAGNOSTICS
auto res = m_value.object->insert(val); auto res = m_value.object->insert(val);
res.first->second.m_parent = this; set_parent(res.first->second, false);
#else
m_value.object->insert(val);
#endif
} }
/*! /*!
...@@ -22302,19 +22251,10 @@ class basic_json ...@@ -22302,19 +22251,10 @@ class basic_json
// add element to array (perfect forwarding) // add element to array (perfect forwarding)
#ifdef JSON_HAS_CPP_17 #ifdef JSON_HAS_CPP_17
#if JSON_DIAGNOSTICS return set_parent(m_value.array->emplace_back(std::forward<Args>(args)...), false);
reference result = m_value.array->emplace_back(std::forward<Args>(args)...);
result.m_parent = this;
return result;
#else
return m_value.array->emplace_back(std::forward<Args>(args)...);
#endif
#else #else
m_value.array->emplace_back(std::forward<Args>(args)...); m_value.array->emplace_back(std::forward<Args>(args)...);
#if JSON_DIAGNOSTICS return set_parent(m_value.array->back(), false);
m_value.array->back().m_parent = this;
#endif
return m_value.array->back();
#endif #endif
} }
...@@ -22364,10 +22304,7 @@ class basic_json ...@@ -22364,10 +22304,7 @@ class basic_json
// add element to array (perfect forwarding) // add element to array (perfect forwarding)
auto res = m_value.object->emplace(std::forward<Args>(args)...); auto res = m_value.object->emplace(std::forward<Args>(args)...);
set_parent(res.first->second, false);
#if JSON_DIAGNOSTICS
res.first->second.m_parent = this;
#endif
// create result iterator and set iterator to the result of emplace // create result iterator and set iterator to the result of emplace
auto it = begin(); auto it = begin();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment