Repository navigation
Json: implement structural == for JsonObject/JsonValue/JsonArray - #1560
Merged
Merged
Conversation
Replaces the TODO in JsonObject.h where operator== compared dump()
strings (two full JSON serializations and string allocations per
compare). The new operators compare types and contents recursively:
- JsonValue: type-strict, then primitive/pointer payload compare.
- JsonArray: size, then element-wise.
- JsonObject: size, then key/value pairs (std::map iteration order).
Semantics match the old dump-based == for all well-formed values
(int 1 != 1.0, key order irrelevant), with one deliberate fix:
null != {} — the old compare treated them as equal because
to_nlohmann() serializes an empty object as null (pre-existing
dump bug, tracked separately).
Mysticial
approved these changes
Oct 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves the TODO at
Common/Cpp/Json/JsonObject.h:22("implement == properly. For JsonValue as well.").What
Replaces the
dump()-string comparison with real structural==/!=for all three JSON types:JsonValue(JsonValue.h,JsonValue.cpp) — type-strict, then compares the payload: primitives directly, and recursively for string/array/object.JsonArray(JsonArray.h) — size, then element-wise.JsonObject(JsonObject.h) — size, then key/value pairs (map iteration order).JsonValue::operator==is declared as a friend in the header and defined inJsonValue.cpp, because it needs the complete types ofJsonArray/JsonObject.Why
The old
JsonObject::operator==didx.dump() == y.dump(): two full JSON serializations (nlohmann round-trip + allocation) per comparison. The new operators do a cheap recursive structural compare.Semantics
Matches the old dump-based behavior for all well-formed values — type-strict (
int 1 != 1.0, since they dump as"1"vs"1.0"), key-order independent. One deliberate correction:null != {}. The old compare treated them as equal because of a pre-existing serialization bug:to_nlohmann()(JsonTools.cpp:31) returns a nullnlohmann::jsonfor an empty object, so an empty objectdump()s as"null"instead of"{}". The new==correctly treats them as different. (Thedump()bug itself is left alone — separate concern, happy to fix in a follow-up.)Verification
g++ -std=c++23 -Wall -Wextra -fsyntax-onlyclean on all four Json TUs plus a couple of consumer TUs.!=, and a cross-check of new vs old equality across ~210 pairs — identical everywhere except the twonull/{}cases above, where the new behavior is the correct one.