Added jsonCompare function

This commit is contained in:
JamesTheMaker 2024-03-07 09:43:36 -05:00
parent f1e3f6791d
commit 14ec64ace7
2 changed files with 46 additions and 0 deletions

View File

@ -1035,4 +1035,34 @@ Json jsonMerge(Json const& base, Json const& merger) {
} }
} }
bool jsonCompare(Json const& base, Json const& compare) {
if (base == compare) {
return true;
} else {
if (base.type() == Json::Type::Object && compare.type() == Json::Type::Object) {
for (auto const& c : compare.toObject()) {
if (!base.contains(c.first) || !jsonCompare(base.get(c.first), c.second))
return false;
}
return true;
}
if (base.type() == Json::Type::Array && compare.type() == Json::Type::Array) {
for (auto const& c : compare.toArray()) {
bool similar = false;
for (auto const& b : base.toArray()) {
if (jsonCompare(c, b)) {
similar = true;
break;
}
}
if (!similar)
return false;
}
return true;
}
return false;
}
}
} }

View File

@ -355,6 +355,22 @@ Json jsonMergeQueryDef(String const& key, Json def, Json const& first, T const&.
return def; return def;
} }
// Compares the two given json values and returns a boolean, by the following
// rules (applied in order): If both values are identical, return true. If both
// values are not equal, check if they are objects. If they are objects,
// iterate through every pair in the comparing object and check if the key is
// in the base object. If the key is in the base object, then jsonCompare is
// called recursively on both values. If the base object does not contain the
// key, or the recursion fails, return false. Otherwise, return true. If they
// are not objects, check if they are arrays. If they are arrays, iterate
// through every value in the comparing object and then recursively call
// jsonCompare on every value in the base object until a match is found. If a
// match is found, break and move on to the next value in the comparing array.
// If a match is found for every value in the comparing array, return true.
// Otherwise, return false. If both values are not identical, and are not
// objects or arrays, return false.
bool jsonCompare(Json const& base, Json const& compare);
} }
template <> struct fmt::formatter<Star::Json> : ostream_formatter {}; template <> struct fmt::formatter<Star::Json> : ostream_formatter {};