Skip to content
Merged
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
18 changes: 18 additions & 0 deletions Common/Cpp/Json/JsonArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ class JsonArray{
public:
JsonArray clone() const;

// Structurally compare two arrays.
friend bool operator==(const JsonArray& x, const JsonArray& y){
if (x.m_data.size() != y.m_data.size()){
return false;
}
auto itx = x.m_data.cbegin();
auto ity = y.m_data.cbegin();
for (; itx != x.m_data.cend(); ++itx, ++ity){
if (!(*itx == *ity)){
return false;
}
}
return true;
}
friend bool operator!=(const JsonArray& x, const JsonArray& y){
return !(x == y);
}

public:
std::string dump(int indent = 4) const;
void dump(const std::string& filename, int indent = 4) const;
Expand Down
14 changes: 12 additions & 2 deletions Common/Cpp/Json/JsonObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ class JsonObject{
JsonObject(JsonObject&& x) = default;
JsonObject& operator=(JsonObject&& x) = default;

// TODO: implement == properly. For JsonValue as well.
// Structurally compare two objects.
friend bool operator==(const JsonObject& x, const JsonObject& y){
return x.dump() == y.dump();
if (x.m_data.size() != y.m_data.size()){
return false;
}
auto itx = x.m_data.cbegin();
auto ity = y.m_data.cbegin();
for (; itx != x.m_data.cend(); ++itx, ++ity){
if (itx->first != ity->first || !(itx->second == ity->second)){
return false;
}
}
return true;
}
friend bool operator!=(const JsonObject& x, const JsonObject& y){
return !(x == y);
Expand Down
30 changes: 30 additions & 0 deletions Common/Cpp/Json/JsonValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,36 @@ bool JsonValue::read_string(std::string& value) const{



// Equality.

bool operator==(const JsonValue& x, const JsonValue& y){
if (x.m_type != y.m_type){
return false;
}
switch (x.m_type){
case JsonType::EMPTY:
return true;
case JsonType::BOOLEAN:
return x.u.m_bool == y.u.m_bool;
case JsonType::INTEGER:
return x.u.m_integer == y.u.m_integer;
case JsonType::FLOAT:
return x.u.m_float == y.u.m_float;
case JsonType::STRING:
return *x.u.m_string == *y.u.m_string;
case JsonType::ARRAY:
return *x.u.m_array == *y.u.m_array;
case JsonType::OBJECT:
return *x.u.m_object == *y.u.m_object;
}
return false;
}
bool operator!=(const JsonValue& x, const JsonValue& y){
return !(x == y);
}



JsonValue parse_json(const std::string& str){
return from_nlohmann(nlohmann::json::parse(str, nullptr, false));
}
Expand Down
5 changes: 5 additions & 0 deletions Common/Cpp/Json/JsonValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class JsonValue{
public:
JsonValue clone() const;

// Structurally compare two values.
// (Defined in JsonValue.cpp; requires the complete types of JsonArray/JsonObject.)
friend bool operator==(const JsonValue& x, const JsonValue& y);
friend bool operator!=(const JsonValue& x, const JsonValue& y);

public:
JsonValue() = default;
JsonValue(bool x);
Expand Down
Loading