forked from bblanchon/ArduinoJson
Merged JsonArrayNode and JsonObjectNode into a single template
This commit is contained in:
@ -1,21 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonVariant.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
struct JsonArrayNode {
|
||||
JsonArrayNode() : next(NULL) {}
|
||||
|
||||
JsonVariant content;
|
||||
JsonArrayNode* next;
|
||||
};
|
||||
}
|
||||
}
|
@ -6,16 +6,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonPair.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
struct JsonObjectNode {
|
||||
JsonObjectNode() : next(NULL) {}
|
||||
template <typename T>
|
||||
struct Node {
|
||||
Node() : next(NULL) {}
|
||||
|
||||
JsonPair content;
|
||||
JsonObjectNode* next;
|
||||
T content;
|
||||
Node<T>* next;
|
||||
};
|
||||
}
|
||||
}
|
@ -6,13 +6,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Internals/JsonArrayNode.hpp"
|
||||
#include "Internals/JsonIterator.hpp"
|
||||
#include "Internals/JsonPrintable.hpp"
|
||||
#include "Internals/Node.hpp"
|
||||
#include "Internals/ReferenceType.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
|
||||
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
|
||||
(sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonArrayNode))
|
||||
(sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type))
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
@ -25,10 +26,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
public:
|
||||
typedef JsonVariant value_type;
|
||||
typedef Internals::JsonIterator<Internals::JsonArrayNode, JsonVariant>
|
||||
iterator;
|
||||
typedef Internals::JsonIterator<Internals::JsonArrayNode, const JsonVariant>
|
||||
const_iterator;
|
||||
typedef Internals::Node<JsonVariant> node_type;
|
||||
typedef Internals::JsonIterator<node_type, JsonVariant> iterator;
|
||||
typedef Internals::JsonIterator<node_type, const JsonVariant> const_iterator;
|
||||
|
||||
int size() const;
|
||||
|
||||
@ -65,11 +65,11 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
// constructor is private: instance must be created via a JsonBuffer
|
||||
JsonArray(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
|
||||
Internals::JsonArrayNode *createNode();
|
||||
inline void addNode(Internals::JsonArrayNode *node);
|
||||
node_type *createNode();
|
||||
inline void addNode(node_type *node);
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
Internals::JsonArrayNode *_firstNode;
|
||||
node_type *_firstNode;
|
||||
static JsonArray _invalid;
|
||||
};
|
||||
}
|
||||
|
@ -7,13 +7,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Internals/JsonIterator.hpp"
|
||||
#include "Internals/JsonObjectNode.hpp"
|
||||
#include "Internals/JsonPrintable.hpp"
|
||||
#include "Internals/Node.hpp"
|
||||
#include "Internals/ReferenceType.hpp"
|
||||
#include "JsonPair.hpp"
|
||||
|
||||
#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
|
||||
(sizeof(JsonObject) + \
|
||||
(NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonObjectNode))
|
||||
(sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type))
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
@ -27,9 +27,9 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
public:
|
||||
typedef const char *key_type;
|
||||
typedef JsonPair value_type;
|
||||
typedef Internals::JsonIterator<Internals::JsonObjectNode, JsonPair> iterator;
|
||||
typedef Internals::JsonIterator<Internals::JsonObjectNode, const JsonPair>
|
||||
const_iterator;
|
||||
typedef Internals::Node<JsonPair> node_type;
|
||||
typedef Internals::JsonIterator<node_type, JsonPair> iterator;
|
||||
typedef Internals::JsonIterator<node_type, const JsonPair> const_iterator;
|
||||
|
||||
bool success() const { return _buffer != NULL; }
|
||||
int size() const;
|
||||
@ -67,15 +67,15 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
JsonObject(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
|
||||
JsonVariant &add(key_type key) { return (*this)[key]; }
|
||||
Internals::JsonObjectNode *createNode();
|
||||
void addNode(Internals::JsonObjectNode *nodeToAdd);
|
||||
void removeNode(Internals::JsonObjectNode *nodeToRemove);
|
||||
node_type *createNode();
|
||||
void addNode(node_type *nodeToAdd);
|
||||
void removeNode(node_type *nodeToRemove);
|
||||
|
||||
Internals::JsonObjectNode *getNodeAt(key_type key) const;
|
||||
Internals::JsonObjectNode *getOrCreateNodeAt(key_type key);
|
||||
node_type *getNodeAt(key_type key) const;
|
||||
node_type *getOrCreateNodeAt(key_type key);
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
Internals::JsonObjectNode *_firstNode;
|
||||
node_type *_firstNode;
|
||||
static JsonObject _invalid;
|
||||
};
|
||||
}
|
||||
|
@ -18,18 +18,18 @@ JsonArray JsonArray::_invalid(NULL);
|
||||
|
||||
int JsonArray::size() const {
|
||||
int nodeCount = 0;
|
||||
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
JsonVariant &JsonArray::at(int index) const {
|
||||
JsonArrayNode *node = _firstNode;
|
||||
node_type *node = _firstNode;
|
||||
while (node && index--) node = node->next;
|
||||
return node ? node->content : JsonVariant::invalid();
|
||||
}
|
||||
|
||||
JsonVariant &JsonArray::add() {
|
||||
JsonArrayNode *node = createNode();
|
||||
node_type *node = createNode();
|
||||
if (!node) return JsonVariant::invalid();
|
||||
|
||||
addNode(node);
|
||||
@ -37,15 +37,15 @@ JsonVariant &JsonArray::add() {
|
||||
return node->content;
|
||||
}
|
||||
|
||||
JsonArrayNode *JsonArray::createNode() {
|
||||
JsonArray::node_type *JsonArray::createNode() {
|
||||
if (!_buffer) return NULL;
|
||||
void *ptr = _buffer->alloc(sizeof(JsonArrayNode));
|
||||
return ptr ? new (ptr) JsonArrayNode() : NULL;
|
||||
void *ptr = _buffer->alloc(sizeof(node_type));
|
||||
return ptr ? new (ptr) node_type() : NULL;
|
||||
}
|
||||
|
||||
void JsonArray::addNode(JsonArrayNode *newNode) {
|
||||
void JsonArray::addNode(node_type *newNode) {
|
||||
if (_firstNode) {
|
||||
JsonArrayNode *lastNode = _firstNode;
|
||||
node_type *lastNode = _firstNode;
|
||||
while (lastNode->next) lastNode = lastNode->next;
|
||||
lastNode->next = newNode;
|
||||
} else {
|
||||
@ -69,7 +69,7 @@ JsonObject &JsonArray::createNestedObject() {
|
||||
|
||||
template <typename T>
|
||||
void JsonArray::writeTo(T &writer) const {
|
||||
JsonArrayNode *child = _firstNode;
|
||||
node_type *child = _firstNode;
|
||||
|
||||
if (child) {
|
||||
writer.beginArray();
|
||||
|
@ -21,22 +21,22 @@ JsonObject JsonObject::_invalid(NULL);
|
||||
|
||||
int JsonObject::size() const {
|
||||
int nodeCount = 0;
|
||||
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
JsonVariant &JsonObject::at(const char *key) {
|
||||
JsonObjectNode *node = getNodeAt(key);
|
||||
node_type *node = getNodeAt(key);
|
||||
return node ? node->content.value : JsonVariant::invalid();
|
||||
}
|
||||
|
||||
const JsonVariant &JsonObject::at(const char *key) const {
|
||||
JsonObjectNode *node = getNodeAt(key);
|
||||
node_type *node = getNodeAt(key);
|
||||
return node ? node->content.value : JsonVariant::invalid();
|
||||
}
|
||||
|
||||
JsonVariant &JsonObject::operator[](const char *key) {
|
||||
JsonObjectNode *node = getOrCreateNodeAt(key);
|
||||
node_type *node = getOrCreateNodeAt(key);
|
||||
return node ? node->content.value : JsonVariant::invalid();
|
||||
}
|
||||
|
||||
@ -56,18 +56,18 @@ JsonObject &JsonObject::createNestedObject(const char *key) {
|
||||
return object;
|
||||
}
|
||||
|
||||
JsonObjectNode *JsonObject::getNodeAt(const char *key) const {
|
||||
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
|
||||
JsonObject::node_type *JsonObject::getNodeAt(const char *key) const {
|
||||
for (node_type *node = _firstNode; node; node = node->next) {
|
||||
if (!strcmp(node->content.key, key)) return node;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
||||
JsonObjectNode *existingNode = getNodeAt(key);
|
||||
JsonObject::node_type *JsonObject::getOrCreateNodeAt(const char *key) {
|
||||
node_type *existingNode = getNodeAt(key);
|
||||
if (existingNode) return existingNode;
|
||||
|
||||
JsonObjectNode *newNode = createNode();
|
||||
node_type *newNode = createNode();
|
||||
if (!newNode) return NULL;
|
||||
|
||||
newNode->content.key = key;
|
||||
@ -75,35 +75,35 @@ JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
||||
return newNode;
|
||||
}
|
||||
|
||||
JsonObjectNode *JsonObject::createNode() {
|
||||
JsonObject::node_type *JsonObject::createNode() {
|
||||
if (!_buffer) return NULL;
|
||||
void *ptr = _buffer->alloc(sizeof(JsonObjectNode));
|
||||
return ptr ? new (ptr) JsonObjectNode() : NULL;
|
||||
void *ptr = _buffer->alloc(sizeof(node_type));
|
||||
return ptr ? new (ptr) node_type() : NULL;
|
||||
}
|
||||
|
||||
void JsonObject::addNode(JsonObjectNode *nodeToAdd) {
|
||||
void JsonObject::addNode(node_type *nodeToAdd) {
|
||||
if (!_firstNode) {
|
||||
_firstNode = nodeToAdd;
|
||||
} else {
|
||||
JsonObjectNode *lastNode = _firstNode;
|
||||
node_type *lastNode = _firstNode;
|
||||
while (lastNode->next) lastNode = lastNode->next;
|
||||
lastNode->next = nodeToAdd;
|
||||
}
|
||||
}
|
||||
|
||||
void JsonObject::removeNode(JsonObjectNode *nodeToRemove) {
|
||||
void JsonObject::removeNode(node_type *nodeToRemove) {
|
||||
if (!nodeToRemove) return;
|
||||
if (nodeToRemove == _firstNode) {
|
||||
_firstNode = nodeToRemove->next;
|
||||
} else {
|
||||
for (JsonObjectNode *node = _firstNode; node; node = node->next)
|
||||
for (node_type *node = _firstNode; node; node = node->next)
|
||||
if (node->next == nodeToRemove) node->next = nodeToRemove->next;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void JsonObject::writeTo(T &writer) const {
|
||||
JsonObjectNode *node = _firstNode;
|
||||
node_type *node = _firstNode;
|
||||
|
||||
if (node) {
|
||||
writer.beginObject();
|
||||
|
Reference in New Issue
Block a user