Skip to content

Commit 96576ca

Browse files
Leander SchultenLeander Schulten
authored andcommitted
Fix comparison of crow::json::rvalue and add tests
1 parent 43a7b20 commit 96576ca

File tree

2 files changed

+142
-9
lines changed

2 files changed

+142
-9
lines changed

include/crow/json.h

Lines changed: 120 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ namespace crow // NOTE: Already documented in "crow/app.h"
162162
return std::string(s_, e_);
163163
}
164164

165+
// The whole class should be replaced by std::string_view
166+
operator std::string_view() const
167+
{
168+
return std::string_view(s_, e_ - s_);
169+
}
170+
165171

166172
const char* begin() const { return s_; }
167173
const char* end() const { return e_; }
@@ -831,47 +837,153 @@ namespace crow // NOTE: Already documented in "crow/app.h"
831837
{
832838
}
833839

834-
inline bool operator==(const rvalue& l, const std::string& r)
840+
inline bool operator==(const rvalue& l, std::string_view r)
841+
{
842+
return l.s() == r;
843+
}
844+
845+
inline bool operator==(std::string_view l, const rvalue& r)
846+
{
847+
return l == r.s();
848+
}
849+
850+
inline bool operator!=(const rvalue& l, std::string_view r)
851+
{
852+
return l.s() != r;
853+
}
854+
855+
inline bool operator!=(std::string_view l, const rvalue& r)
856+
{
857+
return l != r.s();
858+
}
859+
860+
// we need const char * in addition to std::string_view because otherwise json["string"] == "test" would select operator==(bool)
861+
inline bool operator==(const rvalue& l, const char* r)
835862
{
836863
return l.s() == r;
837864
}
838865

839-
inline bool operator==(const std::string& l, const rvalue& r)
866+
inline bool operator==(const char* l, const rvalue& r)
840867
{
841868
return l == r.s();
842869
}
843870

844-
inline bool operator!=(const rvalue& l, const std::string& r)
871+
inline bool operator!=(const rvalue& l, const char* r)
845872
{
846873
return l.s() != r;
847874
}
848875

849-
inline bool operator!=(const std::string& l, const rvalue& r)
876+
inline bool operator!=(const char* l, const rvalue& r)
850877
{
851878
return l != r.s();
852879
}
853880

854-
inline bool operator==(const rvalue& l, const int& r)
881+
882+
inline bool operator==(const rvalue& l, int64_t r)
855883
{
856884
return l.i() == r;
857885
}
858886

859-
inline bool operator==(const int& l, const rvalue& r)
887+
inline bool operator==(int64_t l, const rvalue& r)
860888
{
861889
return l == r.i();
862890
}
863891

864-
inline bool operator!=(const rvalue& l, const int& r)
892+
inline bool operator!=(const rvalue& l, int64_t r)
865893
{
866894
return l.i() != r;
867895
}
868896

869-
inline bool operator!=(const int& l, const rvalue& r)
897+
inline bool operator!=(int64_t l, const rvalue& r)
870898
{
871899
return l != r.i();
872900
}
873901

874902

903+
inline bool operator==(const rvalue& l, int r)
904+
{
905+
return l.i() == r;
906+
}
907+
908+
inline bool operator==(int l, const rvalue& r)
909+
{
910+
return l == r.i();
911+
}
912+
913+
inline bool operator!=(const rvalue& l, int r)
914+
{
915+
return l.i() != r;
916+
}
917+
918+
inline bool operator!=(int l, const rvalue& r)
919+
{
920+
return l != r.i();
921+
}
922+
923+
924+
inline bool operator==(const rvalue& l, unsigned int r)
925+
{
926+
return l.u() == r;
927+
}
928+
929+
inline bool operator==(unsigned int l, const rvalue& r)
930+
{
931+
return l == r.u();
932+
}
933+
934+
inline bool operator!=(const rvalue& l, unsigned int r)
935+
{
936+
return l.u() != r;
937+
}
938+
939+
inline bool operator!=(unsigned int l, const rvalue& r)
940+
{
941+
return l != r.u();
942+
}
943+
944+
945+
inline bool operator==(const rvalue& l, bool r)
946+
{
947+
return l.b() == r;
948+
}
949+
950+
inline bool operator==(bool l, const rvalue& r)
951+
{
952+
return l == r.b();
953+
}
954+
955+
inline bool operator!=(const rvalue& l, bool r)
956+
{
957+
return l.b() != r;
958+
}
959+
960+
inline bool operator!=(bool l, const rvalue& r)
961+
{
962+
return l != r.b();
963+
}
964+
965+
966+
inline bool operator==(const rvalue& l, double r)
967+
{
968+
return l.d() == r;
969+
}
970+
971+
inline bool operator==(double l, const rvalue& r)
972+
{
973+
return l == r.d();
974+
}
975+
976+
inline bool operator!=(const rvalue& l, double r)
977+
{
978+
return l.d() != r;
979+
}
980+
981+
inline bool operator!=(double l, const rvalue& r)
982+
{
983+
return l != r.d();
984+
}
985+
986+
875987
inline rvalue load_nocopy_internal(char* data, size_t size)
876988
{
877989
// Defend against excessive recursion

tests/unit_tests/test_json.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,25 @@ TEST_CASE("SmallNumber #1042", "[json]")
650650
std::string text = data.dump( 4);
651651
const auto expected_text ="{\n \"testValue\": 1e-10\n}";
652652
REQUIRE(text==expected_text);
653-
}
653+
}
654+
655+
TEST_CASE("compare", "[json]")
656+
{
657+
auto data = crow::json::load(R"-({
658+
"int": 4,
659+
"int64": 1099511627776,
660+
"double": 4.5,
661+
"bool": true,
662+
"string": "test"
663+
})-");
664+
CHECK(data["int"] == 4);
665+
CHECK(data["int"] != 5);
666+
CHECK(data["int64"] == 1099511627776);
667+
CHECK(data["int64"] != 1099511627777);
668+
CHECK(data["double"] == 4.5);
669+
CHECK(data["double"] != 4.7);
670+
CHECK(data["bool"] == true);
671+
CHECK(data["bool"] != false);
672+
CHECK(data["string"] == "test");
673+
CHECK(data["string"] != "test6");
674+
}

0 commit comments

Comments
 (0)