From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 3 Apr 2026 22:54:14 -0700 Subject: [PATCH 17/25] Add operator== --- json.cpp | 26 ++++++++++++++++++++++++++ json.h | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/json.cpp b/json.cpp index 84712da562ee6b1966bae788fbf32876eaec4cef..839c311017ccbb2f5ea8f85aab4965e9ce7af090 100644 --- a/json.cpp +++ b/json.cpp @@ -1361,4 +1361,30 @@ json::StatusToString(json::Status status) } } +bool +operator==(const json& lhs, const json& rhs) { + if (lhs.type_ != rhs.type_) + return false; + switch (lhs.type_) { + case json::Type::Null: + return true; + case json::Type::String: + return lhs.string_value == rhs.string_value; + case json::Type::Bool: + return lhs.bool_value == rhs.bool_value; + case json::Type::Int: + return lhs.long_value == rhs.long_value; + case json::Type::Float: + return lhs.float_value == rhs.float_value; + case json::Type::Double: + return lhs.double_value == rhs.double_value; + case json::Type::Array: + return lhs.array_value == rhs.array_value; + case json::Type::Object: + return lhs.object_value == rhs.object_value; + default: + ON_LOGIC_ERROR("Unhandled JSON type during equality comparison."); + } +} + } // namespace wpi::util diff --git a/json.h b/json.h index c5f39687e68289adadb988df9cfd81c031266859..2f2bb7b3b0a265299a011e4e8011ea14065dca36 100644 --- a/json.h +++ b/json.h @@ -28,6 +28,7 @@ namespace wpi::util { class json { + friend bool operator==(const json& lhs, const json& rhs); public: using array_t = std::vector; using object_t = wpi::util::StringMap; @@ -241,4 +242,9 @@ class json static Status parse(json&, const char*&, const char*, int, int); }; +bool operator==(const json& lhs, const json& rhs); +inline bool operator!=(const json& lhs, const json& rhs) { + return !(lhs == rhs); +} + } // namespace wpi::util