Merged JsonArrayNode and JsonObjectNode into a single template

This commit is contained in:
Benoit Blanchon
2014-11-05 11:09:48 +01:00
parent 5d0e326bfd
commit 768312e870
6 changed files with 52 additions and 74 deletions

View File

@ -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;
};
}
}

View File

@ -6,16 +6,15 @@
#pragma once #pragma once
#include "../JsonPair.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
struct JsonObjectNode { template <typename T>
JsonObjectNode() : next(NULL) {} struct Node {
Node() : next(NULL) {}
JsonPair content; T content;
JsonObjectNode* next; Node<T>* next;
}; };
} }
} }

View File

@ -6,13 +6,14 @@
#pragma once #pragma once
#include "Internals/JsonArrayNode.hpp"
#include "Internals/JsonIterator.hpp" #include "Internals/JsonIterator.hpp"
#include "Internals/JsonPrintable.hpp" #include "Internals/JsonPrintable.hpp"
#include "Internals/Node.hpp"
#include "Internals/ReferenceType.hpp" #include "Internals/ReferenceType.hpp"
#include "JsonVariant.hpp"
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \ #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 { namespace ArduinoJson {
@ -25,10 +26,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
public: public:
typedef JsonVariant value_type; typedef JsonVariant value_type;
typedef Internals::JsonIterator<Internals::JsonArrayNode, JsonVariant> typedef Internals::Node<JsonVariant> node_type;
iterator; typedef Internals::JsonIterator<node_type, JsonVariant> iterator;
typedef Internals::JsonIterator<Internals::JsonArrayNode, const JsonVariant> typedef Internals::JsonIterator<node_type, const JsonVariant> const_iterator;
const_iterator;
int size() const; int size() const;
@ -65,11 +65,11 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
// constructor is private: instance must be created via a JsonBuffer // constructor is private: instance must be created via a JsonBuffer
JsonArray(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} JsonArray(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
Internals::JsonArrayNode *createNode(); node_type *createNode();
inline void addNode(Internals::JsonArrayNode *node); inline void addNode(node_type *node);
JsonBuffer *_buffer; JsonBuffer *_buffer;
Internals::JsonArrayNode *_firstNode; node_type *_firstNode;
static JsonArray _invalid; static JsonArray _invalid;
}; };
} }

View File

@ -7,13 +7,13 @@
#pragma once #pragma once
#include "Internals/JsonIterator.hpp" #include "Internals/JsonIterator.hpp"
#include "Internals/JsonObjectNode.hpp"
#include "Internals/JsonPrintable.hpp" #include "Internals/JsonPrintable.hpp"
#include "Internals/Node.hpp"
#include "Internals/ReferenceType.hpp" #include "Internals/ReferenceType.hpp"
#include "JsonPair.hpp"
#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \ #define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \
(sizeof(JsonObject) + \ (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type))
(NUMBER_OF_ELEMENTS) * sizeof(Internals::JsonObjectNode))
namespace ArduinoJson { namespace ArduinoJson {
@ -27,9 +27,9 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
public: public:
typedef const char *key_type; typedef const char *key_type;
typedef JsonPair value_type; typedef JsonPair value_type;
typedef Internals::JsonIterator<Internals::JsonObjectNode, JsonPair> iterator; typedef Internals::Node<JsonPair> node_type;
typedef Internals::JsonIterator<Internals::JsonObjectNode, const JsonPair> typedef Internals::JsonIterator<node_type, JsonPair> iterator;
const_iterator; typedef Internals::JsonIterator<node_type, const JsonPair> const_iterator;
bool success() const { return _buffer != NULL; } bool success() const { return _buffer != NULL; }
int size() const; int size() const;
@ -67,15 +67,15 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
JsonObject(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} JsonObject(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
JsonVariant &add(key_type key) { return (*this)[key]; } JsonVariant &add(key_type key) { return (*this)[key]; }
Internals::JsonObjectNode *createNode(); node_type *createNode();
void addNode(Internals::JsonObjectNode *nodeToAdd); void addNode(node_type *nodeToAdd);
void removeNode(Internals::JsonObjectNode *nodeToRemove); void removeNode(node_type *nodeToRemove);
Internals::JsonObjectNode *getNodeAt(key_type key) const; node_type *getNodeAt(key_type key) const;
Internals::JsonObjectNode *getOrCreateNodeAt(key_type key); node_type *getOrCreateNodeAt(key_type key);
JsonBuffer *_buffer; JsonBuffer *_buffer;
Internals::JsonObjectNode *_firstNode; node_type *_firstNode;
static JsonObject _invalid; static JsonObject _invalid;
}; };
} }

View File

@ -18,18 +18,18 @@ JsonArray JsonArray::_invalid(NULL);
int JsonArray::size() const { int JsonArray::size() const {
int nodeCount = 0; int nodeCount = 0;
for (JsonArrayNode *node = _firstNode; node; node = node->next) nodeCount++; for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount; return nodeCount;
} }
JsonVariant &JsonArray::at(int index) const { JsonVariant &JsonArray::at(int index) const {
JsonArrayNode *node = _firstNode; node_type *node = _firstNode;
while (node && index--) node = node->next; while (node && index--) node = node->next;
return node ? node->content : JsonVariant::invalid(); return node ? node->content : JsonVariant::invalid();
} }
JsonVariant &JsonArray::add() { JsonVariant &JsonArray::add() {
JsonArrayNode *node = createNode(); node_type *node = createNode();
if (!node) return JsonVariant::invalid(); if (!node) return JsonVariant::invalid();
addNode(node); addNode(node);
@ -37,15 +37,15 @@ JsonVariant &JsonArray::add() {
return node->content; return node->content;
} }
JsonArrayNode *JsonArray::createNode() { JsonArray::node_type *JsonArray::createNode() {
if (!_buffer) return NULL; if (!_buffer) return NULL;
void *ptr = _buffer->alloc(sizeof(JsonArrayNode)); void *ptr = _buffer->alloc(sizeof(node_type));
return ptr ? new (ptr) JsonArrayNode() : NULL; return ptr ? new (ptr) node_type() : NULL;
} }
void JsonArray::addNode(JsonArrayNode *newNode) { void JsonArray::addNode(node_type *newNode) {
if (_firstNode) { if (_firstNode) {
JsonArrayNode *lastNode = _firstNode; node_type *lastNode = _firstNode;
while (lastNode->next) lastNode = lastNode->next; while (lastNode->next) lastNode = lastNode->next;
lastNode->next = newNode; lastNode->next = newNode;
} else { } else {
@ -69,7 +69,7 @@ JsonObject &JsonArray::createNestedObject() {
template <typename T> template <typename T>
void JsonArray::writeTo(T &writer) const { void JsonArray::writeTo(T &writer) const {
JsonArrayNode *child = _firstNode; node_type *child = _firstNode;
if (child) { if (child) {
writer.beginArray(); writer.beginArray();

View File

@ -21,22 +21,22 @@ JsonObject JsonObject::_invalid(NULL);
int JsonObject::size() const { int JsonObject::size() const {
int nodeCount = 0; int nodeCount = 0;
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++; for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
return nodeCount; return nodeCount;
} }
JsonVariant &JsonObject::at(const char *key) { JsonVariant &JsonObject::at(const char *key) {
JsonObjectNode *node = getNodeAt(key); node_type *node = getNodeAt(key);
return node ? node->content.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); node_type *node = getNodeAt(key);
return node ? node->content.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); node_type *node = getOrCreateNodeAt(key);
return node ? node->content.value : JsonVariant::invalid(); return node ? node->content.value : JsonVariant::invalid();
} }
@ -56,18 +56,18 @@ JsonObject &JsonObject::createNestedObject(const char *key) {
return object; return object;
} }
JsonObjectNode *JsonObject::getNodeAt(const char *key) const { JsonObject::node_type *JsonObject::getNodeAt(const char *key) const {
for (JsonObjectNode *node = _firstNode; node; node = node->next) { for (node_type *node = _firstNode; node; node = node->next) {
if (!strcmp(node->content.key, key)) return node; if (!strcmp(node->content.key, key)) return node;
} }
return NULL; return NULL;
} }
JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) { JsonObject::node_type *JsonObject::getOrCreateNodeAt(const char *key) {
JsonObjectNode *existingNode = getNodeAt(key); node_type *existingNode = getNodeAt(key);
if (existingNode) return existingNode; if (existingNode) return existingNode;
JsonObjectNode *newNode = createNode(); node_type *newNode = createNode();
if (!newNode) return NULL; if (!newNode) return NULL;
newNode->content.key = key; newNode->content.key = key;
@ -75,35 +75,35 @@ JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
return newNode; return newNode;
} }
JsonObjectNode *JsonObject::createNode() { JsonObject::node_type *JsonObject::createNode() {
if (!_buffer) return NULL; if (!_buffer) return NULL;
void *ptr = _buffer->alloc(sizeof(JsonObjectNode)); void *ptr = _buffer->alloc(sizeof(node_type));
return ptr ? new (ptr) JsonObjectNode() : NULL; return ptr ? new (ptr) node_type() : NULL;
} }
void JsonObject::addNode(JsonObjectNode *nodeToAdd) { void JsonObject::addNode(node_type *nodeToAdd) {
if (!_firstNode) { if (!_firstNode) {
_firstNode = nodeToAdd; _firstNode = nodeToAdd;
} else { } else {
JsonObjectNode *lastNode = _firstNode; node_type *lastNode = _firstNode;
while (lastNode->next) lastNode = lastNode->next; while (lastNode->next) lastNode = lastNode->next;
lastNode->next = nodeToAdd; lastNode->next = nodeToAdd;
} }
} }
void JsonObject::removeNode(JsonObjectNode *nodeToRemove) { void JsonObject::removeNode(node_type *nodeToRemove) {
if (!nodeToRemove) return; if (!nodeToRemove) return;
if (nodeToRemove == _firstNode) { if (nodeToRemove == _firstNode) {
_firstNode = nodeToRemove->next; _firstNode = nodeToRemove->next;
} else { } 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; if (node->next == nodeToRemove) node->next = nodeToRemove->next;
} }
} }
template <typename T> template <typename T>
void JsonObject::writeTo(T &writer) const { void JsonObject::writeTo(T &writer) const {
JsonObjectNode *node = _firstNode; node_type *node = _firstNode;
if (node) { if (node) {
writer.beginObject(); writer.beginObject();