Files
json/api/basic_json_view/operator_ne/index.md
T
2026-10-21 10:31:00 +00:00

4.6 KiB

nlohmann::basic_json_view::operator!=

// (1)
bool operator!=(const basic_json_view& lhs, const basic_json_view& rhs);

// (2)
bool operator!=(const basic_json_view& lhs, const BasicJsonType& rhs);
bool operator!=(const BasicJsonType& lhs, const basic_json_view& rhs);
  1. Compares two views for inequality. Returns !(lhs == rhs), see operator==.
  2. Compares a view and a BasicJsonType value for inequality, in either order. Returns !(lhs == rhs) (or, for the reversed order, !(rhs == lhs)), see operator==.

Since operator!= is defined as the negation of operator==, it follows the same rules for special cases: for instance, since a discarded view is never equal to anything by default (see operator=='s Notes), it is never unequal to anything either -- discarded != discarded is also false, exactly as for a discarded BasicJsonType value.

Parameters

lhs (in) : first value to consider

rhs (in) : second value to consider

Return value

whether the values lhs and rhs are not equal

Exception safety

Strong exception safety: if an exception is thrown, there are no changes to either operand, or to the document(s) a view refers to.

Exceptions

May throw std::bad_alloc, propagated from operator==. Unlike most other basic_json_view functions, operator!= is not noexcept.

Complexity

Linear, as operator==.

Notes

See the Notes of operator== -- in particular for how an object's members are compared (order matters for ordered_json_view but not for json_view) and for how discarded views compare.

No ordering comparison (operator<) is provided for basic_json_view; materialize() is the way to get a BasicJsonType value that supports it.

Examples

Example

The example below asserts, as a test would, that a received document differs from an unwanted value, and shows that -- as for json/ordered_json -- reordering an object's members is detected as a difference for an ordered_json_view but not for a json_view.

#include <iostream>
#include <nlohmann/json_view.hpp>

using json_document = nlohmann::json_document;
using ordered_json_document = nlohmann::ordered_json_document;
using json = nlohmann::json;

int main()
{
    // assert, as a test would, that a received document differs from an
    // unwanted shape -- without ever materializing it into a json value just
    // to compare
    const json_document received = json_document::parse(
                                       R"({"status": "ok", "code": 200})");
    const json unwanted = {{"status", "error"}, {"code", 500}};
    std::cout << std::boolalpha << (received.root() != unwanted) << '\n';

    // json (std::map) compares object members regardless of order ...
    const json_document a = json_document::parse(R"({"a": 1, "b": 2})");
    const json_document b = json_document::parse(R"({"b": 2, "a": 1})");
    std::cout << (a.root() != b.root()) << '\n';

    // ... but ordered_json (ordered_map) compares them in the order they
    // appear, so the very same reordering is detected as a difference
    const ordered_json_document oa = ordered_json_document::parse(R"({"a": 1, "b": 2})");
    const ordered_json_document ob = ordered_json_document::parse(R"({"b": 2, "a": 1})");
    std::cout << (oa.root() != ob.root()) << '\n';
}

Output:

true
false
true

See also

Version history

  • Added in version 3.13.0 unreleased.