mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-30 02:37:35 +02:00
Extract arrayEquals()
and objectEquals()
This commit is contained in:
@ -50,21 +50,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
if (!data_ || !rhs.data_)
|
if (!data_ || !rhs.data_)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
iterator it1 = begin();
|
return arrayEquals(*data_, *rhs.data_);
|
||||||
iterator it2 = rhs.begin();
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
bool end1 = it1 == end();
|
|
||||||
bool end2 = it2 == rhs.end();
|
|
||||||
if (end1 && end2)
|
|
||||||
return true;
|
|
||||||
if (end1 || end2)
|
|
||||||
return false;
|
|
||||||
if (*it1 != *it2)
|
|
||||||
return false;
|
|
||||||
++it1;
|
|
||||||
++it2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the element at the specified index.
|
// Returns the element at the specified index.
|
||||||
|
@ -52,4 +52,9 @@ inline VariantData* collectionToVariant(CollectionData* collection) {
|
|||||||
return reinterpret_cast<VariantData*>(data);
|
return reinterpret_cast<VariantData*>(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool arrayEquals(const detail::CollectionData& lhs,
|
||||||
|
const detail::CollectionData& rhs);
|
||||||
|
bool objectEquals(const detail::CollectionData& lhs,
|
||||||
|
const detail::CollectionData& rhs);
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <ArduinoJson/Collection/CollectionData.hpp>
|
#include <ArduinoJson/Collection/CollectionData.hpp>
|
||||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||||
|
#include <ArduinoJson/Variant/VariantCompare.hpp>
|
||||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
@ -99,4 +100,33 @@ inline void CollectionData::movePointers(ptrdiff_t variantDistance) {
|
|||||||
slot->data()->movePointers(variantDistance);
|
slot->data()->movePointers(variantDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool arrayEquals(const CollectionData& lhs, const CollectionData& rhs) {
|
||||||
|
auto a = lhs.head();
|
||||||
|
auto b = rhs.head();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
if (!a && !b) // both ended
|
||||||
|
return true;
|
||||||
|
if (!a || !b) // one ended
|
||||||
|
return false;
|
||||||
|
if (compare(a->data(), b->data()) != COMPARE_RESULT_EQUAL)
|
||||||
|
return false;
|
||||||
|
a = a->next();
|
||||||
|
b = b->next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool objectEquals(const CollectionData& lhs, const CollectionData& rhs) {
|
||||||
|
size_t count = 0;
|
||||||
|
for (auto a = lhs.head(); a; a = a->next()) {
|
||||||
|
auto b = rhs.get(adaptString(a->key()));
|
||||||
|
if (!b)
|
||||||
|
return false;
|
||||||
|
if (compare(a->data(), b->data()) != COMPARE_RESULT_EQUAL)
|
||||||
|
return false;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
return count == rhs.size();
|
||||||
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -114,13 +114,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
if (!data_ || !rhs.data_)
|
if (!data_ || !rhs.data_)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
size_t count = 0;
|
return objectEquals(*data_, *rhs.data_);
|
||||||
for (iterator it = begin(); it != end(); ++it) {
|
|
||||||
if (it->value() != rhs[it->key()])
|
|
||||||
return false;
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
return count == rhs.size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -12,8 +12,6 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
class CollectionData;
|
|
||||||
|
|
||||||
struct ComparerBase : Visitor<CompareResult> {};
|
struct ComparerBase : Visitor<CompareResult> {};
|
||||||
|
|
||||||
template <typename T, typename Enable = void>
|
template <typename T, typename Enable = void>
|
||||||
@ -86,7 +84,7 @@ struct ArrayComparer : ComparerBase {
|
|||||||
explicit ArrayComparer(const CollectionData& rhs) : rhs_(&rhs) {}
|
explicit ArrayComparer(const CollectionData& rhs) : rhs_(&rhs) {}
|
||||||
|
|
||||||
CompareResult visitArray(const CollectionData& lhs) {
|
CompareResult visitArray(const CollectionData& lhs) {
|
||||||
if (JsonArrayConst(&lhs) == JsonArrayConst(rhs_))
|
if (arrayEquals(lhs, *rhs_))
|
||||||
return COMPARE_RESULT_EQUAL;
|
return COMPARE_RESULT_EQUAL;
|
||||||
else
|
else
|
||||||
return COMPARE_RESULT_DIFFER;
|
return COMPARE_RESULT_DIFFER;
|
||||||
@ -99,7 +97,7 @@ struct ObjectComparer : ComparerBase {
|
|||||||
explicit ObjectComparer(const CollectionData& rhs) : rhs_(&rhs) {}
|
explicit ObjectComparer(const CollectionData& rhs) : rhs_(&rhs) {}
|
||||||
|
|
||||||
CompareResult visitObject(const CollectionData& lhs) {
|
CompareResult visitObject(const CollectionData& lhs) {
|
||||||
if (JsonObjectConst(&lhs) == JsonObjectConst(rhs_))
|
if (objectEquals(lhs, *rhs_))
|
||||||
return COMPARE_RESULT_EQUAL;
|
return COMPARE_RESULT_EQUAL;
|
||||||
else
|
else
|
||||||
return COMPARE_RESULT_DIFFER;
|
return COMPARE_RESULT_DIFFER;
|
||||||
@ -204,4 +202,9 @@ CompareResult compare(ArduinoJson::JsonVariantConst lhs, const T& rhs) {
|
|||||||
return variantAccept(VariantAttorney::getData(lhs), comparer);
|
return variantAccept(VariantAttorney::getData(lhs), comparer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline CompareResult compare(const VariantData* lhs, const VariantData* rhs) {
|
||||||
|
VariantComparer comparer(rhs);
|
||||||
|
return variantAccept(lhs, comparer);
|
||||||
|
}
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
Reference in New Issue
Block a user