From 92b58f45adeaf3b6a6674489971649044cc481f3 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Fri, 5 Dec 2025 17:21:12 +0100 Subject: [PATCH] Fix comparison of crow::json::rvalue and add tests --- include/crow/json.h | 128 ++++++++++++++++++++++++++++++--- tests/unit_tests/test_json.cpp | 23 +++++- 2 files changed, 142 insertions(+), 9 deletions(-) diff --git a/include/crow/json.h b/include/crow/json.h index 1077115ec..7846da973 100644 --- a/include/crow/json.h +++ b/include/crow/json.h @@ -162,6 +162,12 @@ namespace crow // NOTE: Already documented in "crow/app.h" return std::string(s_, e_); } + // The whole class should be replaced by std::string_view + operator std::string_view() const + { + return std::string_view(s_, e_ - s_); + } + const char* begin() const { return s_; } const char* end() const { return e_; } @@ -831,47 +837,153 @@ namespace crow // NOTE: Already documented in "crow/app.h" { } - inline bool operator==(const rvalue& l, const std::string& r) + inline bool operator==(const rvalue& l, std::string_view r) + { + return l.s() == r; + } + + inline bool operator==(std::string_view l, const rvalue& r) + { + return l == r.s(); + } + + inline bool operator!=(const rvalue& l, std::string_view r) + { + return l.s() != r; + } + + inline bool operator!=(std::string_view l, const rvalue& r) + { + return l != r.s(); + } + + // we need const char * in addition to std::string_view because otherwise json["string"] == "test" would select operator==(bool) + inline bool operator==(const rvalue& l, const char* r) { return l.s() == r; } - inline bool operator==(const std::string& l, const rvalue& r) + inline bool operator==(const char* l, const rvalue& r) { return l == r.s(); } - inline bool operator!=(const rvalue& l, const std::string& r) + inline bool operator!=(const rvalue& l, const char* r) { return l.s() != r; } - inline bool operator!=(const std::string& l, const rvalue& r) + inline bool operator!=(const char* l, const rvalue& r) { return l != r.s(); } - inline bool operator==(const rvalue& l, const int& r) + + inline bool operator==(const rvalue& l, long long r) { return l.i() == r; } - inline bool operator==(const int& l, const rvalue& r) + inline bool operator==(long long l, const rvalue& r) { return l == r.i(); } - inline bool operator!=(const rvalue& l, const int& r) + inline bool operator!=(const rvalue& l, long long r) { return l.i() != r; } - inline bool operator!=(const int& l, const rvalue& r) + inline bool operator!=(long long l, const rvalue& r) { return l != r.i(); } + inline bool operator==(const rvalue& l, int r) + { + return l.i() == r; + } + + inline bool operator==(int l, const rvalue& r) + { + return l == r.i(); + } + + inline bool operator!=(const rvalue& l, int r) + { + return l.i() != r; + } + + inline bool operator!=(int l, const rvalue& r) + { + return l != r.i(); + } + + + inline bool operator==(const rvalue& l, unsigned int r) + { + return l.u() == r; + } + + inline bool operator==(unsigned int l, const rvalue& r) + { + return l == r.u(); + } + + inline bool operator!=(const rvalue& l, unsigned int r) + { + return l.u() != r; + } + + inline bool operator!=(unsigned int l, const rvalue& r) + { + return l != r.u(); + } + + + inline bool operator==(const rvalue& l, bool r) + { + return l.b() == r; + } + + inline bool operator==(bool l, const rvalue& r) + { + return l == r.b(); + } + + inline bool operator!=(const rvalue& l, bool r) + { + return l.b() != r; + } + + inline bool operator!=(bool l, const rvalue& r) + { + return l != r.b(); + } + + + inline bool operator==(const rvalue& l, double r) + { + return l.d() == r; + } + + inline bool operator==(double l, const rvalue& r) + { + return l == r.d(); + } + + inline bool operator!=(const rvalue& l, double r) + { + return l.d() != r; + } + + inline bool operator!=(double l, const rvalue& r) + { + return l != r.d(); + } + + inline rvalue load_nocopy_internal(char* data, size_t size) { // Defend against excessive recursion diff --git a/tests/unit_tests/test_json.cpp b/tests/unit_tests/test_json.cpp index 2de56917e..35587ec30 100644 --- a/tests/unit_tests/test_json.cpp +++ b/tests/unit_tests/test_json.cpp @@ -650,4 +650,25 @@ TEST_CASE("SmallNumber #1042", "[json]") std::string text = data.dump( 4); const auto expected_text ="{\n \"testValue\": 1e-10\n}"; REQUIRE(text==expected_text); -} \ No newline at end of file +} + +TEST_CASE("compare", "[json]") +{ + auto data = crow::json::load(R"-({ + "int": 4, + "int64": 1099511627776, + "double": 4.5, + "bool": true, + "string": "test" +})-"); + CHECK(data["int"] == 4); + CHECK(data["int"] != 5); + CHECK(data["int64"] == 1099511627776); + CHECK(data["int64"] != 1099511627777); + CHECK(data["double"] == 4.5); + CHECK(data["double"] != 4.7); + CHECK(data["bool"] == true); + CHECK(data["bool"] != false); + CHECK(data["string"] == "test"); + CHECK(data["string"] != "test6"); +}