1# basic_json::operator== 2 3```cpp 4bool operator==(const_reference lhs, const_reference rhs) noexcept; 5 6template<typename ScalarType> 7bool operator==(const_reference lhs, const ScalarType rhs) noexcept; 8 9template<typename ScalarType> 10bool operator==(ScalarType lhs, const const_reference rhs) noexcept; 11``` 12 13Compares two JSON values for equality according to the following rules: 14 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 18 treated as unequal. 19 20## Template parameters 21 22`ScalarType` 23: a scalar type according to `std::is_scalar<ScalarType>::value` 24 25## Parameters 26 27`lhs` (in) 28: first value to consider 29 30`rhs` (in) 31: second value to consider 32 33## Return value 34 35whether the values `lhs` and `rhs` are equal 36 37## Exception safety 38 39No-throw guarantee: this function never throws exceptions. 40 41## Complexity 42 43Linear. 44 45## Notes 46 47!!! note 48 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. 52 53!!! note 54 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 59 60 ```cpp 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 63 { 64 return std::abs(a - b) <= epsilon; 65 } 66 ``` 67 68 Or you can self-defined operator equal function like this: 69 70 ```cpp 71 bool my_equal(const_reference lhs, const_reference rhs) 72 { 73 const auto lhs_type lhs.type(); 74 const auto rhs_type rhs.type(); 75 if (lhs_type == rhs_type) 76 { 77 switch(lhs_type) 78 // self_defined case 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 82 ... 83 } 84 ... 85 } 86 ``` 87 88## Example 89 90??? example 91 92 The example demonstrates comparing several JSON types. 93 94 ```cpp 95 --8<-- "examples/operator__equal.cpp" 96 ``` 97 98 Output: 99 100 ```json 101 --8<-- "examples/operator__equal.output" 102 ``` 103 104## Version history 105 106- Added in version 1.0.0. 107