Skip to content

Commit

Permalink
🔀 merged #415 (fix for #414)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Feb 16, 2017
2 parents 2c17c1b + 6bf93b3 commit 057b1e6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 82 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ I deeply appreciate the help of the following people.
- [Martin Hořeňovský](https://github.com/horenmar) found a way for a 2x speedup for the compilation time of the test suite.
- [ukhegg](https://github.com/ukhegg) found proposed an improvement for the examples section.
- [rswanson-ihi](https://github.com/rswanson-ihi) noted a type in the README.
-
- [Mihai Stan](https://github.com/stanmihai4) fixed a bug in the comparison with `nullptr`s.
Thanks a lot for helping out! Please [let me know](mailto:mail@nlohmann.me) if I forgot someone.
Expand Down Expand Up @@ -857,7 +857,7 @@ $ make json_unit -Ctest
$ ./test/json_unit "*""
===============================================================================
All tests passed (11202562 assertions in 47 test cases)
All tests passed (11202588 assertions in 47 test cases)
```

Alternatively, you can use [CMake](https://cmake.org) and run
Expand Down
60 changes: 20 additions & 40 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5963,34 +5963,24 @@ class basic_json

/*!
@brief comparison: equal
The functions compares the given JSON value against a null pointer. As the
null pointer can be used to initialize a JSON value to null, a comparison
of JSON value @a v with a null pointer should be equivalent to call
`v.is_null()`.
@param[in] v JSON value to consider
@return whether @a v is null
@complexity Constant.
@liveexample{The example compares several JSON types to the null pointer.
,operator__equal__nullptr_t}
@since version 1.0.0
@copydoc operator==(const_reference, const_reference)
*/
friend bool operator==(const_reference v, std::nullptr_t) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
{
return v.is_null();
return (lhs == basic_json(rhs));
}

/*!
@brief comparison: equal
@copydoc operator==(const_reference, std::nullptr_t)
@copydoc operator==(const_reference, const_reference)
*/
friend bool operator==(std::nullptr_t, const_reference v) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
{
return v.is_null();
return (basic_json(lhs) == rhs);
}

/*!
Expand All @@ -6016,34 +6006,24 @@ class basic_json

/*!
@brief comparison: not equal
The functions compares the given JSON value against a null pointer. As the
null pointer can be used to initialize a JSON value to null, a comparison
of JSON value @a v with a null pointer should be equivalent to call
`not v.is_null()`.
@param[in] v JSON value to consider
@return whether @a v is not null
@complexity Constant.
@liveexample{The example compares several JSON types to the null pointer.
,operator__notequal__nullptr_t}
@since version 1.0.0
@copydoc operator!=(const_reference, const_reference)
*/
friend bool operator!=(const_reference v, std::nullptr_t) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
{
return not v.is_null();
return (lhs != basic_json(rhs));
}

/*!
@brief comparison: not equal
@copydoc operator!=(const_reference, std::nullptr_t)
@copydoc operator!=(const_reference, const_reference)
*/
friend bool operator!=(std::nullptr_t, const_reference v) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
{
return not v.is_null();
return (basic_json(lhs) != rhs);
}

/*!
Expand Down
60 changes: 20 additions & 40 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -5963,34 +5963,24 @@ class basic_json

/*!
@brief comparison: equal

The functions compares the given JSON value against a null pointer. As the
null pointer can be used to initialize a JSON value to null, a comparison
of JSON value @a v with a null pointer should be equivalent to call
`v.is_null()`.

@param[in] v JSON value to consider
@return whether @a v is null

@complexity Constant.

@liveexample{The example compares several JSON types to the null pointer.
,operator__equal__nullptr_t}

@since version 1.0.0
@copydoc operator==(const_reference, const_reference)
*/
friend bool operator==(const_reference v, std::nullptr_t) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
{
return v.is_null();
return (lhs == basic_json(rhs));
}

/*!
@brief comparison: equal
@copydoc operator==(const_reference, std::nullptr_t)
@copydoc operator==(const_reference, const_reference)
*/
friend bool operator==(std::nullptr_t, const_reference v) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
{
return v.is_null();
return (basic_json(lhs) == rhs);
}

/*!
Expand All @@ -6016,34 +6006,24 @@ class basic_json

/*!
@brief comparison: not equal

The functions compares the given JSON value against a null pointer. As the
null pointer can be used to initialize a JSON value to null, a comparison
of JSON value @a v with a null pointer should be equivalent to call
`not v.is_null()`.

@param[in] v JSON value to consider
@return whether @a v is not null

@complexity Constant.

@liveexample{The example compares several JSON types to the null pointer.
,operator__notequal__nullptr_t}

@since version 1.0.0
@copydoc operator!=(const_reference, const_reference)
*/
friend bool operator!=(const_reference v, std::nullptr_t) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
{
return not v.is_null();
return (lhs != basic_json(rhs));
}

/*!
@brief comparison: not equal
@copydoc operator!=(const_reference, std::nullptr_t)
@copydoc operator!=(const_reference, const_reference)
*/
friend bool operator!=(std::nullptr_t, const_reference v) noexcept
template<typename ScalarType, typename std::enable_if<
std::is_scalar<ScalarType>::value, int>::type = 0>
friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
{
return not v.is_null();
return (basic_json(lhs) != rhs);
}

/*!
Expand Down
18 changes: 18 additions & 0 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,24 @@ TEST_CASE("regression tests")
CHECK_THROWS_AS(json::from_cbor(vec3), std::out_of_range);
}

SECTION("issue #414 - compare with literal 0)")
{
#define CHECK_TYPE(v) \
CHECK((json(v) == v));\
CHECK((v == json(v)));\
CHECK_FALSE((json(v) != v));\
CHECK_FALSE((v != json(v)));

CHECK_TYPE(nullptr);
CHECK_TYPE(0);
CHECK_TYPE(0u);
CHECK_TYPE(0L);
CHECK_TYPE(0.0);
CHECK_TYPE("");

#undef CHECK_TYPE
}

SECTION("issue #416 - Use-of-uninitialized-value (OSS-Fuzz issue 377)")
{
// original test case
Expand Down

0 comments on commit 057b1e6

Please sign in to comment.