forked from bblanchon/ArduinoJson
Merged JsonArrayIterator and JsonObjectIterator into a one template class
This commit is contained in:
@ -8,13 +8,16 @@
|
|||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonArray;
|
class JsonArray;
|
||||||
class JsonArrayIterator;
|
|
||||||
class JsonArrayConstIterator;
|
|
||||||
class JsonBuffer;
|
class JsonBuffer;
|
||||||
class JsonObject;
|
class JsonObject;
|
||||||
class JsonObjectIterator;
|
class JsonObjectIterator;
|
||||||
class JsonObjectKeyValue;
|
class JsonObjectKeyValue;
|
||||||
class JsonValue;
|
class JsonValue;
|
||||||
|
template <typename T>
|
||||||
|
class JsonIterator;
|
||||||
|
template <typename T>
|
||||||
|
class JsonConstIterator;
|
||||||
|
|
||||||
namespace Internals {
|
namespace Internals {
|
||||||
class IndentedPrint;
|
class IndentedPrint;
|
||||||
class JsonNode;
|
class JsonNode;
|
||||||
|
@ -7,13 +7,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonContainer.hpp"
|
#include "JsonContainer.hpp"
|
||||||
#include "JsonArrayIterator.hpp"
|
#include "JsonIterator.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonArray : public JsonContainer {
|
class JsonArray : public JsonContainer {
|
||||||
public:
|
public:
|
||||||
typedef JsonArrayIterator iterator;
|
typedef JsonIterator<JsonValue> iterator;
|
||||||
typedef JsonArrayConstIterator const_iterator;
|
typedef JsonConstIterator<JsonValue> const_iterator;
|
||||||
|
|
||||||
JsonArray() {}
|
JsonArray() {}
|
||||||
|
|
||||||
|
@ -11,50 +11,52 @@
|
|||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
|
|
||||||
class JsonArrayIterator {
|
template <typename T>
|
||||||
|
class JsonIterator {
|
||||||
friend class JsonArray;
|
friend class JsonArray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit JsonArrayIterator(Internals::JsonNode *node) : _value(node) {}
|
explicit JsonIterator(Internals::JsonNode *node) : _value(node) {}
|
||||||
|
|
||||||
JsonValue operator*() const { return _value; }
|
T operator*() const { return _value; }
|
||||||
JsonValue *operator->() { return &_value; }
|
T *operator->() { return &_value; }
|
||||||
|
|
||||||
bool operator==(const JsonArrayIterator &other) const {
|
bool operator==(const JsonIterator &other) const {
|
||||||
return _value._node == other._value._node;
|
return _value._node == other._value._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonArrayIterator &other) const {
|
bool operator!=(const JsonIterator &other) const {
|
||||||
return _value._node != other._value._node;
|
return _value._node != other._value._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayIterator &operator++() {
|
JsonIterator &operator++() {
|
||||||
_value._node = _value._node->next;
|
_value._node = _value._node->next;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
JsonValue _value;
|
T _value;
|
||||||
};
|
};
|
||||||
|
|
||||||
class JsonArrayConstIterator {
|
template <typename T>
|
||||||
|
class JsonConstIterator {
|
||||||
friend class JsonArray;
|
friend class JsonArray;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit JsonArrayConstIterator(Internals::JsonNode *node) : _value(node) {}
|
explicit JsonConstIterator(Internals::JsonNode *node) : _value(node) {}
|
||||||
|
|
||||||
const JsonValue operator*() const { return _value; }
|
const JsonValue operator*() const { return _value; }
|
||||||
const JsonValue *operator->() { return &_value; }
|
const JsonValue *operator->() { return &_value; }
|
||||||
|
|
||||||
bool operator==(const JsonArrayConstIterator &other) const {
|
bool operator==(const JsonConstIterator &other) const {
|
||||||
return _value._node == other._value._node;
|
return _value._node == other._value._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const JsonArrayConstIterator &other) const {
|
bool operator!=(const JsonConstIterator &other) const {
|
||||||
return _value._node != other._value._node;
|
return _value._node != other._value._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayConstIterator &operator++() {
|
JsonConstIterator &operator++() {
|
||||||
_value._node = _value._node->next;
|
_value._node = _value._node->next;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
@ -7,11 +7,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonContainer.hpp"
|
#include "JsonContainer.hpp"
|
||||||
#include "JsonObjectIterator.hpp"
|
#include "JsonIterator.hpp"
|
||||||
|
#include "JsonObjectKeyValue.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonObject : public JsonContainer {
|
class JsonObject : public JsonContainer {
|
||||||
public:
|
public:
|
||||||
|
typedef JsonIterator<JsonObjectKeyValue> iterator;
|
||||||
|
typedef JsonConstIterator<JsonObjectKeyValue> const_iterator;
|
||||||
|
|
||||||
JsonObject() {}
|
JsonObject() {}
|
||||||
|
|
||||||
explicit JsonObject(Internals::JsonNode *node) : JsonContainer(node) {}
|
explicit JsonObject(Internals::JsonNode *node) : JsonContainer(node) {}
|
||||||
@ -24,9 +28,11 @@ class JsonObject : public JsonContainer {
|
|||||||
|
|
||||||
bool success() { return _node && _node->isObject(); }
|
bool success() { return _node && _node->isObject(); }
|
||||||
|
|
||||||
JsonObjectIterator begin() { return JsonObjectIterator(firstChild()); }
|
iterator begin() { return iterator(firstChild()); }
|
||||||
|
iterator end() { return iterator(0); }
|
||||||
|
|
||||||
JsonObjectIterator end() { return JsonObjectIterator(0); }
|
const_iterator begin() const { return const_iterator(firstChild()); }
|
||||||
|
const_iterator end() const { return const_iterator(0); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internals::JsonNode *getOrCreateNodeAt(const char *key);
|
Internals::JsonNode *getOrCreateNodeAt(const char *key);
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
// Copyright Benoit Blanchon 2014
|
|
||||||
// MIT License
|
|
||||||
//
|
|
||||||
// Arduino JSON library
|
|
||||||
// https://github.com/bblanchon/ArduinoJson
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "ForwardDeclarations.hpp"
|
|
||||||
#include "JsonObjectKeyValue.hpp"
|
|
||||||
|
|
||||||
namespace ArduinoJson {
|
|
||||||
|
|
||||||
class JsonObjectIterator {
|
|
||||||
friend class JsonObject;
|
|
||||||
|
|
||||||
public:
|
|
||||||
JsonObjectKeyValue operator*() const { return _keyValue; }
|
|
||||||
JsonObjectKeyValue *operator->() { return &_keyValue; }
|
|
||||||
|
|
||||||
bool operator==(const JsonObjectIterator &other) const {
|
|
||||||
return _keyValue._node == other._keyValue._node;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator!=(const JsonObjectIterator &other) const {
|
|
||||||
return _keyValue._node != other._keyValue._node;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonObjectIterator &operator++() {
|
|
||||||
_keyValue._node = _keyValue._node->next;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
explicit JsonObjectIterator(Internals::JsonNode *node) : _keyValue(node) {}
|
|
||||||
|
|
||||||
JsonObjectKeyValue _keyValue;
|
|
||||||
};
|
|
||||||
}
|
|
@ -12,7 +12,8 @@
|
|||||||
namespace ArduinoJson {
|
namespace ArduinoJson {
|
||||||
class JsonObjectKeyValue {
|
class JsonObjectKeyValue {
|
||||||
friend class JsonObject;
|
friend class JsonObject;
|
||||||
friend class JsonObjectIterator;
|
template <typename T>
|
||||||
|
friend class JsonIterator;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const char *key() const { return _node->getAsObjectKey(); }
|
const char *key() const { return _node->getAsObjectKey(); }
|
||||||
|
@ -13,8 +13,10 @@ namespace ArduinoJson {
|
|||||||
|
|
||||||
class JsonValue : public Internals::JsonNodeWrapper {
|
class JsonValue : public Internals::JsonNodeWrapper {
|
||||||
friend class JsonArray;
|
friend class JsonArray;
|
||||||
friend class JsonArrayIterator;
|
template <typename T>
|
||||||
friend class JsonArrayConstIterator;
|
friend class JsonIterator;
|
||||||
|
template <typename T>
|
||||||
|
friend class JsonConstIterator;
|
||||||
friend class JsonBuffer;
|
friend class JsonBuffer;
|
||||||
friend class JsonObject;
|
friend class JsonObject;
|
||||||
friend class JsonObjectKeyValue;
|
friend class JsonObjectKeyValue;
|
||||||
|
@ -22,7 +22,7 @@ JsonValue JsonObject::operator[](char const *key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void JsonObject::remove(char const *key) {
|
void JsonObject::remove(char const *key) {
|
||||||
for (JsonObjectIterator it = begin(); it != end(); ++it) {
|
for (iterator it = begin(); it != end(); ++it) {
|
||||||
if (!strcmp(it->key(), key)) {
|
if (!strcmp(it->key(), key)) {
|
||||||
removeChild(it->_node);
|
removeChild(it->_node);
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ JsonObject JsonObject::createNestedObject(char const *key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
||||||
for (JsonObjectIterator it = begin(); it != end(); ++it) {
|
for (iterator it = begin(); it != end(); ++it) {
|
||||||
if (!strcmp(it->key(), key)) return it->value()._node;
|
if (!strcmp(it->key(), key)) return it->value()._node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@ TEST(JsonArray_Iterator_Test, SimpleTest) {
|
|||||||
array.add(12);
|
array.add(12);
|
||||||
array.add(34);
|
array.add(34);
|
||||||
|
|
||||||
JsonArrayIterator it = array.begin();
|
JsonArray::iterator it = array.begin();
|
||||||
JsonArrayIterator end = array.end();
|
JsonArray::iterator end = array.end();
|
||||||
|
|
||||||
EXPECT_NE(end, it);
|
EXPECT_NE(end, it);
|
||||||
EXPECT_EQ(12, it->as<int>());
|
EXPECT_EQ(12, it->as<int>());
|
||||||
|
@ -17,8 +17,8 @@ TEST(JsonObject_Iterator_Test, SimpleTest) {
|
|||||||
object["ab"] = 12;
|
object["ab"] = 12;
|
||||||
object["cd"] = 34;
|
object["cd"] = 34;
|
||||||
|
|
||||||
JsonObjectIterator it = object.begin();
|
JsonObject::iterator it = object.begin();
|
||||||
JsonObjectIterator end = object.end();
|
JsonObject::iterator end = object.end();
|
||||||
|
|
||||||
EXPECT_NE(end, it);
|
EXPECT_NE(end, it);
|
||||||
EXPECT_STREQ("ab", it->key());
|
EXPECT_STREQ("ab", it->key());
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
||||||
#include <ArduinoJson/JsonValue.hpp>
|
#include <ArduinoJson/JsonValue.hpp>
|
||||||
|
#include "Printers.hpp"
|
||||||
|
|
||||||
using namespace ArduinoJson;
|
using namespace ArduinoJson;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user