Address the review of json_view dump() and comparisons

From gregmarr's review of #5624:

- The output buffer takes string literals without a length.
- write_float() names the IEEE single/double condition once, as the
  library's serializer does.
- equal() checks the types first and compares with !=; the reference to
  the top frame is noted as dangling after pop_back().
- dump.md: one paragraph per case in the notes.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-18 10:06:04 +02:00
1 parent be59fb2a57
commit 7b66cdb9d8
3 files changed
+38 -24

No files matched your search

+9 -6
View File
@@ -201,6 +201,11 @@ class json_side
const BasicJsonType* m_json;
};
inline bool is_number(value_t t) noexcept
{
return t == value_t::number_integer || t == value_t::number_unsigned || t == value_t::number_float;
}
/// whether two sides are equal; iterative, so that the nesting depth is
/// limited by memory only
template<typename BasicJsonType, typename A, typename B>
@@ -225,8 +230,6 @@ bool equal(const A& a0, const B& b0)
{
const value_t ta = a.type();
const value_t tb = b.type();
const bool numbers = (ta == value_t::number_integer || ta == value_t::number_unsigned || ta == value_t::number_float)
&& (tb == value_t::number_integer || tb == value_t::number_unsigned || tb == value_t::number_float);
if (ta == value_t::discarded || tb == value_t::discarded)
{
// basic_json decides (JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
@@ -237,13 +240,13 @@ bool equal(const A& a0, const B& b0)
}
else
{
if (!numbers && ta != tb)
if (ta != tb && !(is_number(ta) && is_number(tb))) // numbers of different types can be equal
{
return false;
}
if (ta == value_t::string)
{
if (!(a.string() == b.string()))
if (a.string() != b.string())
{
return false;
}
@@ -272,7 +275,7 @@ bool equal(const A& a0, const B& b0)
}
stack.push_back(std::move(f));
}
else if (!(a.scalar() == b.scalar())) // numbers (also of different types), null, boolean
else if (a.scalar() != b.scalar()) // numbers (also of different types), null, boolean
{
return false;
}
@@ -289,7 +292,7 @@ bool equal(const A& a0, const B& b0)
const std::size_t count = f.object ? f.members_a.size() : f.elements_a.size();
if (f.next == count)
{
stack.pop_back();
stack.pop_back(); // (f dangles now, and is bound again after continue)
continue;
}
if (f.object)