Skip to content

Add comparisons to json_view - #5625

Closed
nlohmann wants to merge 9 commits into
json-view/13-view-dumpfrom
json-view/14-view-compare
Closed

nlohmann wants to merge 9 commits into
json-view/13-view-dumpfrom
json-view/14-view-compare

Conversation

@nlohmann

@nlohmann nlohmann commented Sep 29, 2026 •

Copy link
Copy Markdown
Owner

Part of the stack for the zero-copy view (#5295).

Summary

operator== and operator!= compare two views, or a view and a basic_json value in either order. They answer whether the values parse() would produce are equal by basic_json's operator==:

  • numbers compare by value across their types (1 == 1.0);
  • objects compare by their members, with duplicate keys resolved as parse() resolves them: the last value, at the position of the first key;
  • member order matters where the object type keeps one (ordered_json) and not otherwise, exactly as basic_json compares;
  • discarded views compare as discarded basic_json values do, including JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON.

Nothing is materialized except single scalars, and the walk is iterative. There is no operator<.

Tests

  • Pairs of 1,200 generated documents, also written differently (sorted keys, canonical numbers), give the same result as basic_json, for json and for ordered_json.
  • Numbers, duplicate keys, member order, discarded values, and 100,000 levels of nesting.
  • The legacy comparison configuration.

Public API

No breaking change: new operators of the view.


Written by Claude Code.

🤖 Generated with Claude Code

@nlohmann
nlohmann added this pull request to stack #5636 September 29, 2026 14:20
@nlohmann nlohmann changed the title json view/14 view compare Add comparisons to json_view Sep 29, 2026
@nlohmann

Copy link
Copy Markdown
Owner Author

Besides merging the lower CI fixes (see #5617 to #5623), commit 5ba6ea1 reformats basic_json_view__operator_eq.cpp and basic_json_view__operator_ne.cpp with the pinned astyle 3.4.13. The "check" job runs astyle over the examples once it gets past the amalgamation step (see #5621).

#5626, #5627 and #5628 got only propagation merges. For #5628 (SIMD), I also built the builder test with gcc:16 on linux/amd64 in all three variants (SSE2, JSON_VIEW_NO_SIMD, JSON_VIEW_USE_SSSE3 with -mssse3), and all passed.

— posted by Claude Code on behalf of @nlohmann

@nlohmann
nlohmann removed this pull request from stack #5636 September 30, 2026 13:19
@nlohmann
nlohmann force-pushed the json-view/13-view-dump branch from 3e2b810 to 7c179b3 Compare September 30, 2026 13:20
@nlohmann
nlohmann force-pushed the json-view/14-view-compare branch from 6f7407a to 4f00cac Compare September 30, 2026 13:20
@nlohmann
nlohmann added this pull request to stack #5739 September 30, 2026 13:21
@nlohmann
nlohmann force-pushed the json-view/14-view-compare branch from 4f00cac to adbc848 Compare September 30, 2026 15:34
@nlohmann
nlohmann force-pushed the json-view/13-view-dump branch from 7c179b3 to bd22b6e Compare September 30, 2026 15:34
@nlohmann
nlohmann force-pushed the json-view/14-view-compare branch 2 times, most recently from 1339883 to c2792eb Compare September 30, 2026 18:06
@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label Sep 30, 2026
@nlohmann
nlohmann marked this pull request as ready for review September 30, 2026 18:17
basic_json_view gains operator== and operator!= with other views and with
basic_json values. Two views are equal if the values parse() would
produce for them are equal by basic_json's operator==: numbers compare by
value across their types, and objects by their members, with duplicate
keys resolved as parse() resolves them (the last value, at the position of
the first key). Objects are compared in member order if the object type
keeps an order (ordered_json), by key otherwise, as basic_json does.
Discarded views compare as discarded basic_json values do, which follows
JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON. Nothing is materialized except
single numbers, and the walk is iterative.

Tests compare the results for pairs of 1,200 generated documents (also
written differently: sorted keys, canonical numbers) with those of
basic_json, for json and ordered_json, plus numbers, duplicate keys,
member order, discarded values, and 100,000 levels of nesting.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
- API pages for operator== and operator!= of basic_json_view, linked
  both ways with the basic_json pages
- the feature page and the class overview list the comparisons
- the examples show when the view helps: detecting a changed document
  without building json values

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Separate the comparison of discarded values from the other types, so
that the conditional chain has no repeated branch bodies, and mark
the deliberate comparisons of views with empty containers in the
tests.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
compare.hpp includes <string> (build/include_what_you_use).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
The "check" job runs astyle over the documentation examples once it gets
past the amalgamation step.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
@nlohmann
nlohmann force-pushed the json-view/13-view-dump branch from 48307dc to bc56ac6 Compare September 30, 2026 19:15
@nlohmann
nlohmann force-pushed the json-view/14-view-compare branch from c2792eb to b992e1f Compare September 30, 2026 19:15
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Comment thread include/nlohmann/detail/view/compare.hpp Dismissed
Comment thread single_include/nlohmann/json_view.hpp Dismissed
Conflicts in See also lists (docs/mkdocs/docs/api/basic_json/operator_ne.md), where develop (#5638) and this branch both edited: kept develop's entries and added this branch's basic_json_view links.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
@nlohmann
nlohmann removed this pull request from stack #5739 October 6, 2026 09:26
@nlohmann

nlohmann commented Oct 6, 2026

Copy link
Copy Markdown
Owner Author

The zero-copy view stack (#5295) was rebased onto develop and regrouped from 20 PRs into 10, one squashed commit each. The changes of this PR are now in #5624; its review comments stay here for reference.


Written by Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation L review needed It would be great if someone could review the proposed changes. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants