mirror of
https://github.com/nlohmann/json.git
synced 2026-10-09 18:35:22 +00:00
Add basic_json_view::dump() and the comparison operators, and read floats from the parser's digit layout instead of rescanning the token. dump(indent, indent_char, ensure_ascii, number_format) writes a value the way ordered_json::parse(text).dump() writes it for the same arguments: members in document order, all of them should a key occur more than once; strings escaped by the same rules, using the library's scanning kernels; floats written with the library's to_chars conversion, so the output equals basic_json's byte for byte; integers copied from the source, where they are already canonical, except -0, which parse() reads as 0. There is no error_handler argument, because the view only holds valid UTF-8. number_format::source copies numbers exactly as they appear in the source (e.g. "1.50", "1E2", "-0"), which basic_json cannot provide. operator<< takes the indentation from the stream width, as for basic_json. The writer walks iteratively, so nesting depth is limited by memory only. operator== and operator!= compare two views, or a view and a basic_json value in either order, by the rules basic_json's operator== uses: numbers compare by value across their types, objects compare by their members with duplicate keys resolved as parse() resolves them, member order matters only where the object type keeps one, and discarded views compare as discarded basic_json values do, including under JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON. Nothing is materialized except single scalars. While parsing, the view now records where the integer digits, the fraction digits, and the exponent of a float token are, so floats and doubles with at most 19 digits are read from that layout with the library's decimal_to_float() instead of rescanning the token. Both round correctly, so the values are those of parse(). get<double>(), materialize(), dump(), and the comparisons all use it. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
3.8 KiB
3.8 KiB
nlohmann::basic_json::operator!=
// until C++20
bool operator!=(const_reference lhs, const_reference rhs) noexcept; // (1)
template<typename ScalarType>
bool operator!=(const_reference lhs, const ScalarType rhs) noexcept(/* see below */); // (2)
template<typename ScalarType>
bool operator!=(ScalarType lhs, const const_reference rhs) noexcept(/* see below */); // (2)
-
Compares two JSON values for inequality. Returns
#!cpp !(lhs == rhs).- This means the comparison is simply the logical negation of
operator==, including for special values likeNaNanddiscarded.
- This means the comparison is simply the logical negation of
-
Compares a JSON value and a scalar or a scalar and a JSON value for inequality by converting the scalar to a JSON value and comparing both JSON values according to 1.
Template parameters
ScalarType- a scalar type according to
std::is_scalar<ScalarType>::value
Parameters
lhs(in)- first value to consider
rhs(in)- second value to consider
Return value
whether the values lhs/*this and rhs are not equal
Exception safety
- No-throw guarantee: this function never throws exceptions.
- No-throw guarantee if converting the scalar to a JSON value cannot throw, as for numbers, Booleans, and
#!cpp nullptr; the function is#!cpp noexceptexactly in that case. Otherwise, it throws what the conversion throws, for examplestd::bad_allocwhen converting a string, orout_of_range.410for an enum value not mapped byNLOHMANN_JSON_SERIALIZE_ENUM_STRICT.
Complexity
Linear.
Notes
!!! note "C++20"
Since C++20, `basic_json` declares no `operator!=`. The compiler rewrites `#!cpp a != b` as `#!cpp !(a == b)`
using [`operator==`](operator_eq.md), so the result is the same as described above.
!!! note "Comparing NaN and discarded"
Since `operator!=` is defined as `!(a == b)`, the behavior for special values follows that of `operator==`:
- For `NaN` values: `NaN == NaN` yields `#!cpp false`, so `NaN != NaN` yields `#!cpp true`.
- For `discarded` values: `discarded == x` yields `#!cpp false` for any `x`, so `discarded != x` yields `#!cpp true`.
Examples
??? example "Example: (1) compare JSON values"
The example demonstrates comparing several JSON types.
```cpp
--8<-- "examples/operator__notequal.cpp"
```
Output:
```json
--8<-- "examples/operator__notequal.output"
```
??? example "Example: (2) compare JSON values with #!cpp nullptr"
The example demonstrates comparing several JSON types against the null pointer (JSON `#!json null`).
```cpp
--8<-- "examples/operator__notequal__nullptr_t.cpp"
```
Output:
```json
--8<-- "examples/operator__notequal__nullptr_t.output"
```
See also
- operator== comparison: equal
- operator<=> comparison: 3-way (C++20)
- basic_json_view::operator!= - the same comparison on a zero-copy view, without
building a
basic_jsonvalue for it
Version history
- Added in version 1.0.0. Added a C++20 member function in version 3.11.0. Changed in version 3.13.0 to remove
special-casing for
NaNanddiscardedvalues;operator!=now consistently means!(a == b). Removed the C++20 member function in version 3.13.0; since C++20, the compiler rewritesa != busingoperator==. - Added in version 1.0.0. Changed in version 3.13.0 to remove special-casing for
NaNanddiscardedvalues;operator!=now consistently means!(a == b). Since C++20, the compiler rewritesa != busingoperator==. Made conditionally#!cpp noexceptin version 3.13.0; before, a throwing conversion calledstd::terminate.