From 768312e870a63b745c9f6c1dcb4f8ee6c2034c46 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 5 Nov 2014 11:09:48 +0100 Subject: [PATCH] Merged JsonArrayNode and JsonObjectNode into a single template --- .../ArduinoJson/Internals/JsonArrayNode.hpp | 21 ------------ .../{JsonObjectNode.hpp => Node.hpp} | 11 +++--- include/ArduinoJson/JsonArray.hpp | 18 +++++----- include/ArduinoJson/JsonObject.hpp | 24 ++++++------- src/JsonArray.cpp | 18 +++++----- src/JsonObject.cpp | 34 +++++++++---------- 6 files changed, 52 insertions(+), 74 deletions(-) delete mode 100644 include/ArduinoJson/Internals/JsonArrayNode.hpp rename include/ArduinoJson/Internals/{JsonObjectNode.hpp => Node.hpp} (59%) diff --git a/include/ArduinoJson/Internals/JsonArrayNode.hpp b/include/ArduinoJson/Internals/JsonArrayNode.hpp deleted file mode 100644 index 7b29805f..00000000 --- a/include/ArduinoJson/Internals/JsonArrayNode.hpp +++ /dev/null @@ -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; -}; -} -} diff --git a/include/ArduinoJson/Internals/JsonObjectNode.hpp b/include/ArduinoJson/Internals/Node.hpp similarity index 59% rename from include/ArduinoJson/Internals/JsonObjectNode.hpp rename to include/ArduinoJson/Internals/Node.hpp index 45dae7c5..ca80a48d 100644 --- a/include/ArduinoJson/Internals/JsonObjectNode.hpp +++ b/include/ArduinoJson/Internals/Node.hpp @@ -6,16 +6,15 @@ #pragma once -#include "../JsonPair.hpp" - namespace ArduinoJson { namespace Internals { -struct JsonObjectNode { - JsonObjectNode() : next(NULL) {} +template +struct Node { + Node() : next(NULL) {} - JsonPair content; - JsonObjectNode* next; + T content; + Node* next; }; } } diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index afe0390b..9e3114ad 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -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, public: typedef JsonVariant value_type; - typedef Internals::JsonIterator - iterator; - typedef Internals::JsonIterator - const_iterator; + typedef Internals::Node node_type; + typedef Internals::JsonIterator iterator; + typedef Internals::JsonIterator const_iterator; int size() const; @@ -65,11 +65,11 @@ class JsonArray : public Internals::JsonPrintable, // 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; }; } diff --git a/include/ArduinoJson/JsonObject.hpp b/include/ArduinoJson/JsonObject.hpp index 286e7006..a1155232 100644 --- a/include/ArduinoJson/JsonObject.hpp +++ b/include/ArduinoJson/JsonObject.hpp @@ -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, public: typedef const char *key_type; typedef JsonPair value_type; - typedef Internals::JsonIterator iterator; - typedef Internals::JsonIterator - const_iterator; + typedef Internals::Node node_type; + typedef Internals::JsonIterator iterator; + typedef Internals::JsonIterator const_iterator; bool success() const { return _buffer != NULL; } int size() const; @@ -67,15 +67,15 @@ class JsonObject : public Internals::JsonPrintable, 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; }; } diff --git a/src/JsonArray.cpp b/src/JsonArray.cpp index f0a2baf9..3abecc7a 100644 --- a/src/JsonArray.cpp +++ b/src/JsonArray.cpp @@ -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 void JsonArray::writeTo(T &writer) const { - JsonArrayNode *child = _firstNode; + node_type *child = _firstNode; if (child) { writer.beginArray(); diff --git a/src/JsonObject.cpp b/src/JsonObject.cpp index 4a94f791..d897453a 100644 --- a/src/JsonObject.cpp +++ b/src/JsonObject.cpp @@ -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 void JsonObject::writeTo(T &writer) const { - JsonObjectNode *node = _firstNode; + node_type *node = _firstNode; if (node) { writer.beginObject();