forked from bblanchon/ArduinoJson
Unified JsonArrayNode and JsonObjectNode
This commit is contained in:
@ -14,8 +14,8 @@ namespace Internals {
|
|||||||
struct JsonArrayNode {
|
struct JsonArrayNode {
|
||||||
JsonArrayNode() : next(NULL) {}
|
JsonArrayNode() : next(NULL) {}
|
||||||
|
|
||||||
|
JsonVariant content;
|
||||||
JsonArrayNode* next;
|
JsonArrayNode* next;
|
||||||
JsonVariant value;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,8 @@ class JsonIterator {
|
|||||||
public:
|
public:
|
||||||
explicit JsonIterator(TNode *node) : _node(node) {}
|
explicit JsonIterator(TNode *node) : _node(node) {}
|
||||||
|
|
||||||
TValue &operator*() const { return _node->value; }
|
TValue &operator*() const { return _node->content; }
|
||||||
TValue *operator->() { return &_node->value; }
|
TValue *operator->() { return &_node->content; }
|
||||||
|
|
||||||
bool operator==(const JsonIterator<TNode, TValue> &other) const {
|
bool operator==(const JsonIterator<TNode, TValue> &other) const {
|
||||||
return _node == other._node;
|
return _node == other._node;
|
||||||
|
@ -12,9 +12,9 @@ namespace ArduinoJson {
|
|||||||
namespace Internals {
|
namespace Internals {
|
||||||
|
|
||||||
struct JsonObjectNode {
|
struct JsonObjectNode {
|
||||||
JsonObjectNode(const char* k) : pair(k), next(NULL) {}
|
JsonObjectNode(const char* key) : content(key), next(NULL) {}
|
||||||
|
|
||||||
JsonPair pair;
|
JsonPair content;
|
||||||
JsonObjectNode* next;
|
JsonObjectNode* next;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,8 @@ class JsonObjectConstIterator {
|
|||||||
explicit JsonObjectConstIterator(Internals::JsonObjectNode *node)
|
explicit JsonObjectConstIterator(Internals::JsonObjectNode *node)
|
||||||
: _node(node) {}
|
: _node(node) {}
|
||||||
|
|
||||||
const JsonPair &operator*() { return _node->pair; }
|
const JsonPair &operator*() { return _node->content; }
|
||||||
const JsonPair *operator->() { return &_node->pair; }
|
const JsonPair *operator->() { return &_node->content; }
|
||||||
|
|
||||||
bool operator==(const JsonObjectConstIterator &other) const {
|
bool operator==(const JsonObjectConstIterator &other) const {
|
||||||
return _node == other._node;
|
return _node == other._node;
|
||||||
|
@ -14,8 +14,8 @@ class JsonObjectIterator {
|
|||||||
public:
|
public:
|
||||||
explicit JsonObjectIterator(Internals::JsonObjectNode *node) : _node(node) {}
|
explicit JsonObjectIterator(Internals::JsonObjectNode *node) : _node(node) {}
|
||||||
|
|
||||||
JsonPair &operator*() { return _node->pair; }
|
JsonPair &operator*() { return _node->content; }
|
||||||
JsonPair *operator->() { return &_node->pair; }
|
JsonPair *operator->() { return &_node->content; }
|
||||||
|
|
||||||
bool operator==(const JsonObjectIterator &other) const {
|
bool operator==(const JsonObjectIterator &other) const {
|
||||||
return _node == other._node;
|
return _node == other._node;
|
||||||
|
@ -25,7 +25,7 @@ int JsonArray::size() const {
|
|||||||
JsonVariant &JsonArray::at(int index) const {
|
JsonVariant &JsonArray::at(int index) const {
|
||||||
JsonArrayNode *node = _firstNode;
|
JsonArrayNode *node = _firstNode;
|
||||||
while (node && index--) node = node->next;
|
while (node && index--) node = node->next;
|
||||||
return node ? node->value : JsonVariant::invalid();
|
return node ? node->content : JsonVariant::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonVariant &JsonArray::add() {
|
JsonVariant &JsonArray::add() {
|
||||||
@ -34,7 +34,7 @@ JsonVariant &JsonArray::add() {
|
|||||||
|
|
||||||
addNode(node);
|
addNode(node);
|
||||||
|
|
||||||
return node->value;
|
return node->content;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArrayNode *JsonArray::createNode() {
|
JsonArrayNode *JsonArray::createNode() {
|
||||||
@ -75,7 +75,7 @@ void JsonArray::writeTo(T &writer) const {
|
|||||||
writer.beginArray();
|
writer.beginArray();
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
child->value.writeTo(writer);
|
child->content.writeTo(writer);
|
||||||
|
|
||||||
child = child->next;
|
child = child->next;
|
||||||
if (!child) break;
|
if (!child) break;
|
||||||
|
@ -27,17 +27,17 @@ int JsonObject::size() const {
|
|||||||
|
|
||||||
JsonVariant &JsonObject::at(const char *key) {
|
JsonVariant &JsonObject::at(const char *key) {
|
||||||
JsonObjectNode *node = getNodeAt(key);
|
JsonObjectNode *node = getNodeAt(key);
|
||||||
return node ? node->pair.value : JsonVariant::invalid();
|
return node ? node->content.value : JsonVariant::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
const JsonVariant &JsonObject::at(const char *key) const {
|
const JsonVariant &JsonObject::at(const char *key) const {
|
||||||
JsonObjectNode *node = getNodeAt(key);
|
JsonObjectNode *node = getNodeAt(key);
|
||||||
return node ? node->pair.value : JsonVariant::invalid();
|
return node ? node->content.value : JsonVariant::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonVariant &JsonObject::operator[](const char *key) {
|
JsonVariant &JsonObject::operator[](const char *key) {
|
||||||
JsonObjectNode *node = getOrCreateNodeAt(key);
|
JsonObjectNode *node = getOrCreateNodeAt(key);
|
||||||
return node ? node->pair.value : JsonVariant::invalid();
|
return node ? node->content.value : JsonVariant::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
|
void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
|
||||||
@ -58,7 +58,7 @@ JsonObject &JsonObject::createNestedObject(const char *key) {
|
|||||||
|
|
||||||
JsonObjectNode *JsonObject::getNodeAt(const char *key) const {
|
JsonObjectNode *JsonObject::getNodeAt(const char *key) const {
|
||||||
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
|
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
|
||||||
if (!strcmp(node->pair.key, key)) return node;
|
if (!strcmp(node->content.key, key)) return node;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -108,9 +108,9 @@ void JsonObject::writeTo(T &writer) const {
|
|||||||
writer.beginObject();
|
writer.beginObject();
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
writer.writeString(node->pair.key);
|
writer.writeString(node->content.key);
|
||||||
writer.writeColon();
|
writer.writeColon();
|
||||||
node->pair.value.writeTo(writer);
|
node->content.value.writeTo(writer);
|
||||||
|
|
||||||
node = node->next;
|
node = node->next;
|
||||||
if (!node) break;
|
if (!node) break;
|
||||||
|
Reference in New Issue
Block a user