1 # <small>nlohmann::basic_json::</small>operator==
4 bool operator==(const_reference lhs, const_reference rhs) noexcept;
6 template<typename ScalarType>
7 bool operator==(const_reference lhs, const ScalarType rhs) noexcept;
9 template<typename ScalarType>
10 bool operator==(ScalarType lhs, const const_reference rhs) noexcept;
13 Compares two JSON values for equality according to the following rules:
15 - Two JSON values are equal if (1) they are not discarded, (2) they are from the same type, and (3) their stored values
16 are the same according to their respective `operator==`.
17 - Integer and floating-point numbers are automatically converted before comparison. Note that two NaN values are always
20 ## Template parameters
23 : a scalar type according to `std::is_scalar<ScalarType>::value`
28 : first value to consider
31 : second value to consider
35 whether the values `lhs` and `rhs` are equal
39 No-throw guarantee: this function never throws exceptions.
49 - NaN values never compare equal to themselves or to other NaN values.
50 - JSON `#!cpp null` values are all equal.
51 - Discarded values never compare equal to themselves.
55 Floating-point numbers inside JSON values numbers are compared with `json::number_float_t::operator==` which is
56 `double::operator==` by default. To compare floating-point while respecting an epsilon, an alternative
57 [comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39)
58 could be used, for instance
61 template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
62 inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
64 return std::abs(a - b) <= epsilon;
68 Or you can self-defined operator equal function like this:
71 bool my_equal(const_reference lhs, const_reference rhs)
73 const auto lhs_type lhs.type();
74 const auto rhs_type rhs.type();
75 if (lhs_type == rhs_type)
79 case value_t::number_float:
80 return std::abs(lhs - rhs) <= std::numeric_limits<float>::epsilon();
81 // other cases remain the same with the original
92 The example demonstrates comparing several JSON types.
95 --8<-- "examples/operator__equal.cpp"
101 --8<-- "examples/operator__equal.output"
106 The example demonstrates comparing several JSON types against the null pointer (JSON `#!json null`).
109 --8<-- "examples/operator__equal__nullptr_t.cpp"
115 --8<-- "examples/operator__equal__nullptr_t.output"
120 - Added in version 1.0.0.