Skip to content

Commit

Permalink
Add user self-defined content in README
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed Mar 12, 2020
1 parent 0feea61 commit 530d099
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [Conversions to/from arbitrary types](#arbitrary-types-conversions)
- [Specializing enum conversion](#specializing-enum-conversion)
- [Binary formats (BSON, CBOR, MessagePack, and UBJSON)](#binary-formats-bson-cbor-messagepack-and-ubjson)
- [User self-defined cases](#User-self-defined-cases)
- [Supported compilers](#supported-compilers)
- [License](#license)
- [Contact](#contact)
Expand Down Expand Up @@ -1011,6 +1012,34 @@ std::vector<std::uint8_t> v_ubjson = json::to_ubjson(j);
json j_from_ubjson = json::from_ubjson(v_ubjson);
```

### User self-defined cases

This JSON library is very flexible in usage, you can customize and implement you desired functions in your own projects, and can also self-defined data type that you want to parse, or handle some edge cases by yourselves.

#### Custom comparison function

As we know, the result of comparing two floating point numbers may be uncertain, which
depends on the system architecture, the compiler, and the absolute/relative error value we can acccpt.
For this reason, you can self-defined function according to specific scenarios, like

```cpp
bool is_equal(const_reference lhs, const_reference rhs)
{
const auto lhs_type = lhs.type();
const auto rhs_type = rhs.type();
if (lhs_type == rhs_type)
{
switch (lhs_type)
// self-defined case
case value_t::number_float:
return std::abs(lhs - rhs) <= std::numeric_limits<float>::epsilon()
// other cases remain the same with the original
...
}
...
}
```
then you can use `is_equal` instead of `==` in your own project.
## Supported compilers
Expand Down
7 changes: 4 additions & 3 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5637,13 +5637,14 @@ class basic_json
their stored values are the same according to their respective
`operator==`.
- Integer and floating-point numbers are automatically converted before
comparison. Note than two NaN values are always treated as unequal.
comparison.
- Two NaN values are always treated as unequal.
- Two JSON null values are equal.
@note Floating-point inside JSON values numbers are compared with
`json::number_float_t::operator==` which is `double::operator==` by
default. To compare floating-point while respecting an epsilon, an alternative
[comparison function](https://github.com/mariokonrad/marnav/blob/master/src/marnav/math/floatingpoint.hpp#L34-#L39)
[comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34)
could be used, for instance
@code {.cpp}
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
Expand All @@ -5652,8 +5653,8 @@ class basic_json
return std::abs(a - b) <= epsilon;
}
@endcode
@note NaN values never compare equal to themselves or to other NaN values.
@note For more details, please see [readme](https://github.com/nlohmann/json#User-self-defined-cases)
@param[in] lhs first JSON value to consider
@param[in] rhs second JSON value to consider
Expand Down
7 changes: 4 additions & 3 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20180,13 +20180,14 @@ class basic_json
their stored values are the same according to their respective
`operator==`.
- Integer and floating-point numbers are automatically converted before
comparison. Note than two NaN values are always treated as unequal.
comparison.
- Two NaN values are always treated as unequal.
- Two JSON null values are equal.

@note Floating-point inside JSON values numbers are compared with
`json::number_float_t::operator==` which is `double::operator==` by
default. To compare floating-point while respecting an epsilon, an alternative
[comparison function](https://github.com/mariokonrad/marnav/blob/master/src/marnav/math/floatingpoint.hpp#L34-#L39)
[comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34)
could be used, for instance
@code {.cpp}
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
Expand All @@ -20195,8 +20196,8 @@ class basic_json
return std::abs(a - b) <= epsilon;
}
@endcode

@note NaN values never compare equal to themselves or to other NaN values.
@note For more details, please see [readme](https://github.com/nlohmann/json#User-self-defined-cases)

@param[in] lhs first JSON value to consider
@param[in] rhs second JSON value to consider
Expand Down

0 comments on commit 530d099

Please sign in to comment.