mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-30 18:57:32 +02:00
Add ResourceManager*
in CollectionIterator
+80
This commit is contained in:
@ -15,7 +15,7 @@ inline VariantImpl::iterator VariantImpl::at(size_t index) const {
|
|||||||
|
|
||||||
auto it = createIterator();
|
auto it = createIterator();
|
||||||
while (!it.done() && index) {
|
while (!it.done() && index) {
|
||||||
it.next(resources_);
|
it.next();
|
||||||
--index;
|
--index;
|
||||||
}
|
}
|
||||||
return it;
|
return it;
|
||||||
@ -34,7 +34,7 @@ inline VariantData* VariantImpl::addElement() {
|
|||||||
inline VariantData* VariantImpl::getOrAddElement(size_t index) {
|
inline VariantData* VariantImpl::getOrAddElement(size_t index) {
|
||||||
auto it = createIterator();
|
auto it = createIterator();
|
||||||
while (!it.done() && index > 0) {
|
while (!it.done() && index > 0) {
|
||||||
it.next(resources_);
|
it.next();
|
||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
if (it.done())
|
if (it.done())
|
||||||
|
@ -73,7 +73,7 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
|||||||
// Returns an iterator to the first element of the array.
|
// Returns an iterator to the first element of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarray/begin/
|
// https://arduinojson.org/v7/api/jsonarray/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
return iterator(impl_.createIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last element of the array.
|
// Returns an iterator following the last element of the array.
|
||||||
|
@ -24,7 +24,7 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
|||||||
// Returns an iterator to the first element of the array.
|
// Returns an iterator to the first element of the array.
|
||||||
// https://arduinojson.org/v7/api/jsonarrayconst/begin/
|
// https://arduinojson.org/v7/api/jsonarrayconst/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
return iterator(impl_.createIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator to the element following the last element of the array.
|
// Returns an iterator to the element following the last element of the array.
|
||||||
|
@ -30,12 +30,11 @@ class JsonArrayIterator {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
JsonArrayIterator() {}
|
JsonArrayIterator() {}
|
||||||
explicit JsonArrayIterator(detail::VariantImpl::iterator iterator,
|
explicit JsonArrayIterator(const detail::VariantImpl::iterator& iterator)
|
||||||
detail::ResourceManager* resources)
|
: iterator_(iterator) {}
|
||||||
: iterator_(iterator), resources_(resources) {}
|
|
||||||
|
|
||||||
JsonVariant operator*() {
|
JsonVariant operator*() {
|
||||||
return JsonVariant(iterator_.value(resources_));
|
return JsonVariant(iterator_.value());
|
||||||
}
|
}
|
||||||
Ptr<JsonVariant> operator->() {
|
Ptr<JsonVariant> operator->() {
|
||||||
return operator*();
|
return operator*();
|
||||||
@ -50,13 +49,12 @@ class JsonArrayIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayIterator& operator++() {
|
JsonArrayIterator& operator++() {
|
||||||
iterator_.next(resources_);
|
iterator_.next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::VariantImpl::iterator iterator_;
|
detail::VariantImpl::iterator iterator_;
|
||||||
detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonArrayConstIterator {
|
class JsonArrayConstIterator {
|
||||||
@ -64,12 +62,11 @@ class JsonArrayConstIterator {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
JsonArrayConstIterator() {}
|
JsonArrayConstIterator() {}
|
||||||
explicit JsonArrayConstIterator(detail::VariantImpl::iterator iterator,
|
explicit JsonArrayConstIterator(const detail::VariantImpl::iterator& iterator)
|
||||||
detail::ResourceManager* resources)
|
: iterator_(iterator) {}
|
||||||
: iterator_(iterator), resources_(resources) {}
|
|
||||||
|
|
||||||
JsonVariantConst operator*() const {
|
JsonVariantConst operator*() const {
|
||||||
return JsonVariantConst(iterator_.value(resources_));
|
return JsonVariantConst(iterator_.value());
|
||||||
}
|
}
|
||||||
Ptr<JsonVariantConst> operator->() {
|
Ptr<JsonVariantConst> operator->() {
|
||||||
return operator*();
|
return operator*();
|
||||||
@ -84,13 +81,12 @@ class JsonArrayConstIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayConstIterator& operator++() {
|
JsonArrayConstIterator& operator++() {
|
||||||
iterator_.next(resources_);
|
iterator_.next();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable detail::VariantImpl::iterator iterator_;
|
mutable detail::VariantImpl::iterator iterator_;
|
||||||
mutable detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -12,23 +12,23 @@
|
|||||||
|
|
||||||
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
|
||||||
|
|
||||||
inline void CollectionIterator::next(const ResourceManager* resources) {
|
inline void CollectionIterator::next() {
|
||||||
ARDUINOJSON_ASSERT(slot_);
|
ARDUINOJSON_ASSERT(slot_);
|
||||||
auto nextId = slot_->next;
|
auto nextId = slot_->next;
|
||||||
slot_ = resources->getVariant(nextId);
|
slot_ = resources_->getVariant(nextId);
|
||||||
currentId_ = nextId;
|
currentId_ = nextId;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantImpl CollectionIterator::value(ResourceManager* resources) const {
|
inline VariantImpl CollectionIterator::value() const {
|
||||||
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
ARDUINOJSON_ASSERT(slot_ != nullptr);
|
||||||
return VariantImpl(slot_, resources);
|
return VariantImpl(slot_, resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VariantImpl::iterator VariantImpl::createIterator() const {
|
inline VariantImpl::iterator VariantImpl::createIterator() const {
|
||||||
if (!data_ || !data_->isCollection())
|
if (!data_ || !data_->isCollection())
|
||||||
return iterator();
|
return iterator();
|
||||||
auto coll = getCollectionData();
|
auto coll = getCollectionData();
|
||||||
return iterator(getVariant(coll->head), coll->head);
|
return iterator(coll->head, resources_);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void VariantImpl::appendOne(Slot<VariantData> slot) {
|
inline void VariantImpl::appendOne(Slot<VariantData> slot) {
|
||||||
@ -111,8 +111,8 @@ inline size_t VariantImpl::nesting() const {
|
|||||||
if (!data_ || !data_->isCollection())
|
if (!data_ || !data_->isCollection())
|
||||||
return 0;
|
return 0;
|
||||||
size_t maxChildNesting = 0;
|
size_t maxChildNesting = 0;
|
||||||
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
for (auto it = createIterator(); !it.done(); it.next()) {
|
||||||
auto childNesting = it.value(resources_).nesting();
|
auto childNesting = it.value().nesting();
|
||||||
if (childNesting > maxChildNesting)
|
if (childNesting > maxChildNesting)
|
||||||
maxChildNesting = childNesting;
|
maxChildNesting = childNesting;
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,11 @@ class CollectionIterator {
|
|||||||
friend class VariantImpl;
|
friend class VariantImpl;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CollectionIterator() : slot_(nullptr), currentId_(NULL_SLOT) {}
|
CollectionIterator() {}
|
||||||
|
|
||||||
void next(const ResourceManager* resources);
|
void next();
|
||||||
|
|
||||||
VariantImpl value(ResourceManager* resources) const;
|
VariantImpl value() const;
|
||||||
|
|
||||||
bool done() const {
|
bool done() const {
|
||||||
return slot_ == nullptr;
|
return slot_ == nullptr;
|
||||||
@ -46,11 +46,14 @@ class CollectionIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CollectionIterator(VariantData* slot, SlotId slotId)
|
CollectionIterator(SlotId slotId, ResourceManager* resources)
|
||||||
: slot_(slot), currentId_(slotId) {}
|
: slot_(resources->getVariant(slotId)),
|
||||||
|
currentId_(slotId),
|
||||||
|
resources_(resources) {}
|
||||||
|
|
||||||
VariantData* slot_;
|
VariantData* slot_ = nullptr;
|
||||||
SlotId currentId_;
|
SlotId currentId_ = NULL_SLOT;
|
||||||
|
ResourceManager* resources_ = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
ARDUINOJSON_END_PRIVATE_NAMESPACE
|
||||||
|
@ -26,8 +26,8 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
nesting_++;
|
nesting_++;
|
||||||
while (!it.done()) {
|
while (!it.done()) {
|
||||||
indent();
|
indent();
|
||||||
it.value(base::resources_).accept(*this);
|
it.value().accept(*this);
|
||||||
it.next(base::resources_);
|
it.next();
|
||||||
base::write(it.done() ? "\r\n" : ",\r\n");
|
base::write(it.done() ? "\r\n" : ",\r\n");
|
||||||
}
|
}
|
||||||
nesting_--;
|
nesting_--;
|
||||||
@ -48,8 +48,8 @@ class PrettyJsonSerializer : public JsonSerializer<TWriter> {
|
|||||||
while (!it.done()) {
|
while (!it.done()) {
|
||||||
if (isKey)
|
if (isKey)
|
||||||
indent();
|
indent();
|
||||||
it.value(base::resources_).accept(*this);
|
it.value().accept(*this);
|
||||||
it.next(base::resources_);
|
it.next();
|
||||||
if (isKey)
|
if (isKey)
|
||||||
base::write(": ");
|
base::write(": ");
|
||||||
else
|
else
|
||||||
|
@ -64,7 +64,7 @@ class JsonObject : public detail::VariantOperators<JsonObject> {
|
|||||||
// Returns an iterator to the first key-value pair of the object.
|
// Returns an iterator to the first key-value pair of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobject/begin/
|
// https://arduinojson.org/v7/api/jsonobject/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
return iterator(impl_.createIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
|
@ -55,7 +55,7 @@ class JsonObjectConst : public detail::VariantOperators<JsonObjectConst> {
|
|||||||
// Returns an iterator to the first key-value pair of the object.
|
// Returns an iterator to the first key-value pair of the object.
|
||||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin/
|
// https://arduinojson.org/v7/api/jsonobjectconst/begin/
|
||||||
iterator begin() const {
|
iterator begin() const {
|
||||||
return iterator(impl_.createIterator(), impl_.getResourceManager());
|
return iterator(impl_.createIterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an iterator following the last key-value pair of the object.
|
// Returns an iterator following the last key-value pair of the object.
|
||||||
|
@ -14,12 +14,11 @@ class JsonObjectIterator {
|
|||||||
public:
|
public:
|
||||||
JsonObjectIterator() {}
|
JsonObjectIterator() {}
|
||||||
|
|
||||||
explicit JsonObjectIterator(detail::VariantImpl::iterator iterator,
|
explicit JsonObjectIterator(const detail::VariantImpl::iterator& iterator)
|
||||||
detail::ResourceManager* resources)
|
: iterator_(iterator) {}
|
||||||
: iterator_(iterator), resources_(resources) {}
|
|
||||||
|
|
||||||
JsonPair operator*() const {
|
JsonPair operator*() const {
|
||||||
return JsonPair(iterator_, resources_);
|
return JsonPair(iterator_);
|
||||||
}
|
}
|
||||||
Ptr<JsonPair> operator->() {
|
Ptr<JsonPair> operator->() {
|
||||||
return operator*();
|
return operator*();
|
||||||
@ -34,14 +33,13 @@ class JsonObjectIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectIterator& operator++() {
|
JsonObjectIterator& operator++() {
|
||||||
iterator_.next(resources_); // key
|
iterator_.next(); // key
|
||||||
iterator_.next(resources_); // value
|
iterator_.next(); // value
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::VariantImpl::iterator iterator_;
|
detail::VariantImpl::iterator iterator_;
|
||||||
detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonObjectConstIterator {
|
class JsonObjectConstIterator {
|
||||||
@ -50,12 +48,12 @@ class JsonObjectConstIterator {
|
|||||||
public:
|
public:
|
||||||
JsonObjectConstIterator() {}
|
JsonObjectConstIterator() {}
|
||||||
|
|
||||||
explicit JsonObjectConstIterator(detail::VariantImpl::iterator iterator,
|
explicit JsonObjectConstIterator(
|
||||||
detail::ResourceManager* resources)
|
const detail::VariantImpl::iterator& iterator)
|
||||||
: iterator_(iterator), resources_(resources) {}
|
: iterator_(iterator) {}
|
||||||
|
|
||||||
JsonPairConst operator*() const {
|
JsonPairConst operator*() const {
|
||||||
return JsonPairConst(iterator_, resources_);
|
return JsonPairConst(iterator_);
|
||||||
}
|
}
|
||||||
Ptr<JsonPairConst> operator->() {
|
Ptr<JsonPairConst> operator->() {
|
||||||
return operator*();
|
return operator*();
|
||||||
@ -70,14 +68,13 @@ class JsonObjectConstIterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonObjectConstIterator& operator++() {
|
JsonObjectConstIterator& operator++() {
|
||||||
iterator_.next(resources_); // key
|
iterator_.next(); // key
|
||||||
iterator_.next(resources_); // value
|
iterator_.next(); // value
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
detail::VariantImpl::iterator iterator_;
|
detail::VariantImpl::iterator iterator_;
|
||||||
detail::ResourceManager* resources_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||||
|
@ -15,12 +15,11 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
|
|||||||
class JsonPair {
|
class JsonPair {
|
||||||
public:
|
public:
|
||||||
// INTERNAL USE ONLY
|
// INTERNAL USE ONLY
|
||||||
JsonPair(detail::VariantImpl::iterator iterator,
|
JsonPair(detail::VariantImpl::iterator iterator) {
|
||||||
detail::ResourceManager* resources) {
|
|
||||||
if (!iterator.done()) {
|
if (!iterator.done()) {
|
||||||
key_ = iterator.value(resources).asString();
|
key_ = iterator.value().asString();
|
||||||
iterator.next(resources);
|
iterator.next();
|
||||||
value_ = JsonVariant(iterator.value(resources));
|
value_ = JsonVariant(iterator.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,12 +42,11 @@ class JsonPair {
|
|||||||
// https://arduinojson.org/v7/api/jsonobjectconst/begin_end/
|
// https://arduinojson.org/v7/api/jsonobjectconst/begin_end/
|
||||||
class JsonPairConst {
|
class JsonPairConst {
|
||||||
public:
|
public:
|
||||||
JsonPairConst(detail::VariantImpl::iterator iterator,
|
JsonPairConst(detail::VariantImpl::iterator iterator) {
|
||||||
detail::ResourceManager* resources) {
|
|
||||||
if (!iterator.done()) {
|
if (!iterator.done()) {
|
||||||
key_ = iterator.value(resources).asString();
|
key_ = iterator.value().asString();
|
||||||
iterator.next(resources);
|
iterator.next();
|
||||||
value_ = JsonVariantConst(iterator.value(resources));
|
value_ = JsonVariantConst(iterator.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ inline VariantData* VariantImpl::getMember(TAdaptedString key) const {
|
|||||||
auto it = findKey(key);
|
auto it = findKey(key);
|
||||||
if (it.done())
|
if (it.done())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
it.next(resources_);
|
it.next();
|
||||||
return it.data();
|
return it.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,9 +33,8 @@ inline VariantImpl::iterator VariantImpl::findKey(TAdaptedString key) const {
|
|||||||
if (key.isNull())
|
if (key.isNull())
|
||||||
return iterator();
|
return iterator();
|
||||||
bool isKey = true;
|
bool isKey = true;
|
||||||
for (auto it = createIterator(); !it.done(); it.next(resources_)) {
|
for (auto it = createIterator(); !it.done(); it.next()) {
|
||||||
if (isKey &&
|
if (isKey && stringEquals(key, adaptString(it.value().asString())))
|
||||||
stringEquals(key, adaptString(it.value(resources_).asString())))
|
|
||||||
return it;
|
return it;
|
||||||
isKey = !isKey;
|
isKey = !isKey;
|
||||||
}
|
}
|
||||||
|
@ -461,7 +461,7 @@ class VariantImpl {
|
|||||||
|
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
|
|
||||||
for (auto it = createIterator(); !it.done(); it.next(resources_))
|
for (auto it = createIterator(); !it.done(); it.next())
|
||||||
count++;
|
count++;
|
||||||
|
|
||||||
if (data_->type == VariantType::Object)
|
if (data_->type == VariantType::Object)
|
||||||
|
Reference in New Issue
Block a user