Fix stale and copy-pasted comments in basic_json

Several comments no longer match the code: the class invariant and
assert_invariant()'s doc still named the members m_value/m_type
(now m_data.m_value/m_data.m_type) and did not mention the binary
invariant that assert_invariant() already checks; the json_value note
and the get<PointerType>() @tparam list omitted binary_t even though
binary is a variable-length, pointer-stored type like the others; the
key-based value() overload's brief said "via JSON Pointer", which is
the other overload; and swap(binary_t&)/swap(binary_t::container_type&)
both carried "swap only works for strings", copied from swap(string_t&).

Comment-only change; behavior, the public API and the ABI are
unchanged. The private get_impl() doxygen and the emplace() comments
that border #5585's hunk are intentionally left alone.

Part of #5724

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-30 10:42:29 +02:00
parent c2553bac5a
commit b591304d04
2 changed files with 28 additions and 26 deletions
+14 -13
View File
@@ -110,11 +110,12 @@ struct is_std_optional<std::optional<T>> : std::true_type {};
@brief a class to store JSON values @brief a class to store JSON values
@internal @internal
@invariant The member variables @a m_value and @a m_type have the following @invariant The member variables @a m_data.m_value and @a m_data.m_type have
relationship: the following relationship:
- If `m_type == value_t::object`, then `m_value.object != nullptr`. - If `m_data.m_type == value_t::object`, then `m_data.m_value.object != nullptr`.
- If `m_type == value_t::array`, then `m_value.array != nullptr`. - If `m_data.m_type == value_t::array`, then `m_data.m_value.array != nullptr`.
- If `m_type == value_t::string`, then `m_value.string != nullptr`. - If `m_data.m_type == value_t::string`, then `m_data.m_value.string != nullptr`.
- If `m_data.m_type == value_t::binary`, then `m_data.m_value.binary != nullptr`.
The invariants are checked by member function assert_invariant(). The invariants are checked by member function assert_invariant().
@note ObjectType trick from https://stackoverflow.com/a/9860911 @note ObjectType trick from https://stackoverflow.com/a/9860911
@@ -463,9 +464,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
binary | binary | pointer to @ref binary_t binary | binary | pointer to @ref binary_t
null | null | *no value is stored* null | null | *no value is stored*
@note Variable-length types (objects, arrays, and strings) are stored as @note Variable-length types (objects, arrays, strings, and binary
pointers. The size of the union should not exceed 64 bits if the default values) are stored as pointers. The size of the union should not exceed
value types are used. 64 bits if the default value types are used.
@since version 1.0.0 @since version 1.0.0
*/ */
@@ -717,7 +718,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
end of every constructor to make sure that created objects respect the end of every constructor to make sure that created objects respect the
invariant. Furthermore, it has to be called each time the type of a JSON invariant. Furthermore, it has to be called each time the type of a JSON
value is changed, because the invariant expresses a relationship between value is changed, because the invariant expresses a relationship between
@a m_type and @a m_value. @a m_data.m_type and @a m_data.m_value.
Furthermore, the parent relation is checked for arrays and objects: If Furthermore, the parent relation is checked for arrays and objects: If
@a check_parents true and the value is an array or object, then the @a check_parents true and the value is an array or object, then the
@@ -2516,7 +2517,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
@ref number_unsigned_t, or @ref number_float_t. @ref number_unsigned_t, @ref number_float_t, or @ref binary_t.
@return pointer to the internally stored JSON value if the requested @return pointer to the internally stored JSON value if the requested
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
@@ -3042,7 +3043,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this)); JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
} }
/// @brief access specified object element via JSON Pointer with default value /// @brief access specified object element with default value
/// @sa https://json.nlohmann.me/api/basic_json/value/ /// @sa https://json.nlohmann.me/api/basic_json/value/
template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type, template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
detail::enable_if_t < detail::enable_if_t <
@@ -4383,7 +4384,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/swap/ /// @sa https://json.nlohmann.me/api/basic_json/swap/
void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
{ {
// swap only works for strings // swap only works for binary values
if (JSON_HEDLEY_LIKELY(is_binary())) if (JSON_HEDLEY_LIKELY(is_binary()))
{ {
using std::swap; using std::swap;
@@ -4399,7 +4400,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/swap/ /// @sa https://json.nlohmann.me/api/basic_json/swap/
void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape) void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
{ {
// swap only works for strings // swap only works for binary values
if (JSON_HEDLEY_LIKELY(is_binary())) if (JSON_HEDLEY_LIKELY(is_binary()))
{ {
using std::swap; using std::swap;
+14 -13
View File
@@ -26190,11 +26190,12 @@ struct is_std_optional<std::optional<T>> : std::true_type {};
@brief a class to store JSON values @brief a class to store JSON values
@internal @internal
@invariant The member variables @a m_value and @a m_type have the following @invariant The member variables @a m_data.m_value and @a m_data.m_type have
relationship: the following relationship:
- If `m_type == value_t::object`, then `m_value.object != nullptr`. - If `m_data.m_type == value_t::object`, then `m_data.m_value.object != nullptr`.
- If `m_type == value_t::array`, then `m_value.array != nullptr`. - If `m_data.m_type == value_t::array`, then `m_data.m_value.array != nullptr`.
- If `m_type == value_t::string`, then `m_value.string != nullptr`. - If `m_data.m_type == value_t::string`, then `m_data.m_value.string != nullptr`.
- If `m_data.m_type == value_t::binary`, then `m_data.m_value.binary != nullptr`.
The invariants are checked by member function assert_invariant(). The invariants are checked by member function assert_invariant().
@note ObjectType trick from https://stackoverflow.com/a/9860911 @note ObjectType trick from https://stackoverflow.com/a/9860911
@@ -26543,9 +26544,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
binary | binary | pointer to @ref binary_t binary | binary | pointer to @ref binary_t
null | null | *no value is stored* null | null | *no value is stored*
@note Variable-length types (objects, arrays, and strings) are stored as @note Variable-length types (objects, arrays, strings, and binary
pointers. The size of the union should not exceed 64 bits if the default values) are stored as pointers. The size of the union should not exceed
value types are used. 64 bits if the default value types are used.
@since version 1.0.0 @since version 1.0.0
*/ */
@@ -26797,7 +26798,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
end of every constructor to make sure that created objects respect the end of every constructor to make sure that created objects respect the
invariant. Furthermore, it has to be called each time the type of a JSON invariant. Furthermore, it has to be called each time the type of a JSON
value is changed, because the invariant expresses a relationship between value is changed, because the invariant expresses a relationship between
@a m_type and @a m_value. @a m_data.m_type and @a m_data.m_value.
Furthermore, the parent relation is checked for arrays and objects: If Furthermore, the parent relation is checked for arrays and objects: If
@a check_parents true and the value is an array or object, then the @a check_parents true and the value is an array or object, then the
@@ -28596,7 +28597,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
@ref number_unsigned_t, or @ref number_float_t. @ref number_unsigned_t, @ref number_float_t, or @ref binary_t.
@return pointer to the internally stored JSON value if the requested @return pointer to the internally stored JSON value if the requested
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
@@ -29122,7 +29123,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this)); JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
} }
/// @brief access specified object element via JSON Pointer with default value /// @brief access specified object element with default value
/// @sa https://json.nlohmann.me/api/basic_json/value/ /// @sa https://json.nlohmann.me/api/basic_json/value/
template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type, template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
detail::enable_if_t < detail::enable_if_t <
@@ -30463,7 +30464,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/swap/ /// @sa https://json.nlohmann.me/api/basic_json/swap/
void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
{ {
// swap only works for strings // swap only works for binary values
if (JSON_HEDLEY_LIKELY(is_binary())) if (JSON_HEDLEY_LIKELY(is_binary()))
{ {
using std::swap; using std::swap;
@@ -30479,7 +30480,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/swap/ /// @sa https://json.nlohmann.me/api/basic_json/swap/
void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape) void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
{ {
// swap only works for strings // swap only works for binary values
if (JSON_HEDLEY_LIKELY(is_binary())) if (JSON_HEDLEY_LIKELY(is_binary()))
{ {
using std::swap; using std::swap;