From 618a54579f8f08ff745181377607ef02de38b8a6 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 25 Oct 2014 21:02:13 +0200 Subject: [PATCH] Simplified JsonArray --- include/ArduinoJson/JsonArray.hpp | 17 +++++++---- include/ArduinoJson/JsonContainer.hpp | 5 +--- src/JsonArray.cpp | 41 ++------------------------- 3 files changed, 15 insertions(+), 48 deletions(-) diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index 8164781a..48435dbd 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -24,12 +24,14 @@ class JsonArray : public JsonContainer { JsonValue operator[](int index) const; - void add(bool value); - void add(const char *value); - void add(double value, int decimals = 2); - void add(int value) { add(static_cast(value)); } - void add(long value); - void add(JsonContainer nestedArray); // TODO: should allow JsonValue too + template + void add(T value) { + addNewValue() = value; + } + + void add(double value, int decimals = 2) { + addNewValue().set(value, decimals); + } JsonArray createNestedArray(); JsonObject createNestedObject(); @@ -41,5 +43,8 @@ class JsonArray : public JsonContainer { const_iterator begin() const { return const_iterator(firstChild()); } const_iterator end() const { return const_iterator(0); } + +private: + JsonValue addNewValue(); }; } diff --git a/include/ArduinoJson/JsonContainer.hpp b/include/ArduinoJson/JsonContainer.hpp index c0ae7de4..a067210a 100644 --- a/include/ArduinoJson/JsonContainer.hpp +++ b/include/ArduinoJson/JsonContainer.hpp @@ -7,15 +7,12 @@ #pragma once #include "Arduino/Printable.hpp" -#include "ForwardDeclarations.hpp" #include "Internals/JsonNodeIterator.hpp" #include "Internals/JsonNodeWrapper.hpp" namespace ArduinoJson { class JsonContainer : public Printable, public Internals::JsonNodeWrapper { - friend class JsonArray; - public: JsonContainer() {} @@ -44,6 +41,6 @@ class JsonContainer : public Printable, public Internals::JsonNodeWrapper { void addChild(Internals::JsonNode *); void removeChild(Internals::JsonNode *); Internals::JsonNode *createNode(); - Internals::JsonNode* firstChild() const; + Internals::JsonNode *firstChild() const; }; } diff --git a/src/JsonArray.cpp b/src/JsonArray.cpp index 7f007627..3cb1a541 100644 --- a/src/JsonArray.cpp +++ b/src/JsonArray.cpp @@ -20,45 +20,10 @@ JsonValue JsonArray::operator[](int index) const { return JsonValue(); } -void JsonArray::add(bool value) { +JsonValue JsonArray::addNewValue() { JsonNode *node = createNode(); - if (!node) return; - - node->setAsBoolean(value); - addChild(node); -} - -void JsonArray::add(char const *value) { - JsonNode *node = createNode(); - if (!node) return; - - node->setAsString(value); - addChild(node); -} - -void JsonArray::add(double value, int decimals) { - JsonNode *node = createNode(); - if (!node) return; - - node->setAsDouble(value, decimals); - addChild(node); -} - -void JsonArray::add(long value) { - JsonNode *node = createNode(); - if (!node) return; - - node->setAsLong(value); - addChild(node); -} - -// TODO: we should have the same issue as in JsonValue -void JsonArray::add(JsonContainer nestedContainer) { - JsonNode *node = createNode(); - if (!node) return; - - node->duplicate(nestedContainer._node); - addChild(node); + if (node) addChild(node); + return JsonValueInternal(node); } JsonArray JsonArray::createNestedArray() {