Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 120 additions & 8 deletions include/crow/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand Down Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion tests/unit_tests/test_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

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");
}
Loading