1.7: use detail::is_primitive in json.hpp switches (#5891)

has_no_children, set_parents, max_size and the two diff walkers.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-23 10:29:15 +02:00
1 parent d52cfb7221
commit 2e92fe8d26
1 file changed
+34 -94
+34 -94
View File
@@ -732,23 +732,15 @@ private:
// true if v is not an array/object, or is an already-empty one // true if v is not an array/object, or is an already-empty one
static bool has_no_children(const basic_json& v) noexcept static bool has_no_children(const basic_json& v) noexcept
{ {
switch (v.m_data.m_type) if (detail::is_primitive(v.m_data.m_type))
{ {
case value_t::array:
return v.m_data.m_value.array->empty();
case value_t::object:
return v.m_data.m_value.object->empty();
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:
return true; return true;
} }
if (v.m_data.m_type == value_t::array)
{
return v.m_data.m_value.array->empty();
}
return v.m_data.m_value.object->empty();
} }
// true if no child of the non-empty array/object v has children of // true if no child of the non-empty array/object v has children of
@@ -1106,36 +1098,24 @@ public:
void set_parents() void set_parents()
{ {
#if JSON_DIAGNOSTICS #if JSON_DIAGNOSTICS
switch (m_data.m_type) if (detail::is_primitive(m_data.m_type))
{ {
case value_t::array: return;
}
if (m_data.m_type == value_t::array)
{ {
for (auto& element : *m_data.m_value.array) for (auto& element : *m_data.m_value.array)
{ {
element.m_parent = this; element.m_parent = this;
} }
break;
} }
else
case value_t::object:
{ {
for (auto& element : *m_data.m_value.object) for (auto& element : *m_data.m_value.object)
{ {
element.second.m_parent = this; element.second.m_parent = this;
} }
break;
}
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:
break;
} }
#endif #endif
} }
@@ -4602,36 +4582,22 @@ public:
JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_WARN_UNUSED_RESULT
size_type max_size() const noexcept size_type max_size() const noexcept
{ {
switch (m_data.m_type) if (detail::is_primitive(m_data.m_type))
{ {
case value_t::array: // all other types have max_size() == size()
return size();
}
if (m_data.m_type == value_t::array)
{ {
// delegate call to array_t::max_size() // delegate call to array_t::max_size()
return m_data.m_value.array->max_size(); return m_data.m_value.array->max_size();
} }
case value_t::object:
{
// delegate call to object_t::max_size() // delegate call to object_t::max_size()
return m_data.m_value.object->max_size(); return m_data.m_value.object->max_size();
} }
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:
{
// all other types have max_size() == size()
return size();
}
}
}
/// @} /// @}
/////////////// ///////////////
@@ -7825,9 +7791,14 @@ public:
return; return;
} }
switch (source.type()) if (detail::is_primitive(source.type()))
{ {
case value_t::array: // both primitive types: replace value
diff_replace(result, path, target);
return;
}
if (source.is_array())
{ {
const std::size_t path_length = path.size(); const std::size_t path_length = path.size();
@@ -7848,10 +7819,8 @@ public:
// We now reached the end of at least one array // We now reached the end of at least one array
// in a second pass, traverse the remaining elements // in a second pass, traverse the remaining elements
diff_array_tails(result, source, target, path, i); diff_array_tails(result, source, target, path, i);
break;
} }
else
case value_t::object:
{ {
diff_target_values target_values; diff_target_values target_values;
std::vector<const_iterator> added; std::vector<const_iterator> added;
@@ -7885,23 +7854,6 @@ public:
// append the "add" operations for brand-new keys // append the "add" operations for brand-new keys
diff_added(result, added, path); diff_added(result, added, path);
} }
break;
}
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, path, target);
break;
}
} }
} }
@@ -7946,15 +7898,19 @@ public:
return; return;
} }
switch (s.type()) if (detail::is_primitive(s.type()))
{ {
case value_t::array: // both primitive types: replace value
diff_replace(result, current_path, t);
return;
}
if (s.is_array())
{ {
stack.emplace_back(&s, &t, current_path.size()); stack.emplace_back(&s, &t, current_path.size());
return; return;
} }
else
case value_t::object:
{ {
std::vector<const basic_json*> target_values; std::vector<const basic_json*> target_values;
std::vector<const_iterator> added; std::vector<const_iterator> added;
@@ -7970,22 +7926,6 @@ public:
} }
return; 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); enter(source, target);