From b0e43f753815f3dc0dcfa1bade2e43deaa62a2ed Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 1 Oct 2014 16:13:36 +0200 Subject: [PATCH] Extracted class JsonContainer --- srcs/Internals/JsonNode.h | 2 +- srcs/Internals/JsonNodeSerializer.cpp | 2 +- srcs/JsonBuffer.cpp | 2 +- srcs/JsonContainer.cpp | 16 ++++++++++++++++ srcs/JsonContainer.h | 26 ++++++++++++++++++++++++++ srcs/JsonObject.cpp | 24 ++++++------------------ srcs/JsonObject.h | 14 ++++---------- srcs/srcs.vcxproj | 2 ++ srcs/srcs.vcxproj.filters | 6 ++++++ 9 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 srcs/JsonContainer.cpp create mode 100644 srcs/JsonContainer.h diff --git a/srcs/Internals/JsonNode.h b/srcs/Internals/JsonNode.h index 263a2e4b..efa897b2 100644 --- a/srcs/Internals/JsonNode.h +++ b/srcs/Internals/JsonNode.h @@ -41,7 +41,7 @@ struct JsonNode { JsonNode* child; JsonBuffer* buffer; - } asObject; + } asContainer; struct { diff --git a/srcs/Internals/JsonNodeSerializer.cpp b/srcs/Internals/JsonNodeSerializer.cpp index f7394933..5b44385b 100644 --- a/srcs/Internals/JsonNodeSerializer.cpp +++ b/srcs/Internals/JsonNodeSerializer.cpp @@ -41,7 +41,7 @@ size_t JsonNodeSerializer::serializeObject(const JsonNode* node) n += _sink.write('{'); - JsonNode* firstChild = node->content.asObject.child; + JsonNode* firstChild = node->content.asContainer.child; for (JsonNode* child = firstChild; child; child = child->next) { diff --git a/srcs/JsonBuffer.cpp b/srcs/JsonBuffer.cpp index 66832825..1036fb0d 100644 --- a/srcs/JsonBuffer.cpp +++ b/srcs/JsonBuffer.cpp @@ -11,7 +11,7 @@ JsonObject JsonBuffer::createObject() JsonNode* node = createNode(JSON_OBJECT); if (node) - node->content.asObject.buffer = this; + node->content.asContainer.buffer = this; return JsonObject(node); } diff --git a/srcs/JsonContainer.cpp b/srcs/JsonContainer.cpp new file mode 100644 index 00000000..cb0f6a55 --- /dev/null +++ b/srcs/JsonContainer.cpp @@ -0,0 +1,16 @@ +#include "JsonContainer.h" + +#include "Internals/JsonNodeSerializer.h" +#include "Internals/StringBuilder.h" + +size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const +{ + ArduinoJson::Internals::StringBuilder sb(buffer, bufferSize); + return printTo(sb); +} + +size_t JsonContainer::printTo(Print& p) const +{ + JsonNodeSerializer serializer(p); + return serializer.serialize(_node); +} \ No newline at end of file diff --git a/srcs/JsonContainer.h b/srcs/JsonContainer.h new file mode 100644 index 00000000..a31fc639 --- /dev/null +++ b/srcs/JsonContainer.h @@ -0,0 +1,26 @@ +#pragma once + +#include "Arduino\Printable.h" + +struct JsonNode; + +class JsonContainer : public Printable +{ +public: + JsonContainer() + : _node(0) + { + } + + JsonContainer(JsonNode* node) + : _node(node) + { + } + + size_t printTo(char* buffer, size_t bufferSize) const; + virtual size_t printTo(Print& print) const; + +protected: + JsonNode* _node; +}; + diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index 716ac22b..9d0ccb80 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -13,7 +13,7 @@ using namespace ArduinoJson::Internals; size_t JsonObject::size() { - JsonNode* firstChild = _node->content.asObject.child; + JsonNode* firstChild = _node->content.asContainer.child; int size = 0; @@ -33,7 +33,7 @@ JsonValue JsonObject::operator[](char const* key) void JsonObject::remove(char const* key) { - JsonNode* firstChild = _node->content.asObject.child; + JsonNode* firstChild = _node->content.asContainer.child; JsonNode* lastChild = 0; for (JsonNode* child = firstChild; child; child = child->next) @@ -45,7 +45,7 @@ void JsonObject::remove(char const* key) if (lastChild) lastChild->next = child->next; else - _node->content.asObject.child = child->next; + _node->content.asContainer.child = child->next; } lastChild = child; } @@ -60,7 +60,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key) { if (!_node || _node->type != JSON_OBJECT) return 0; - JsonNode* firstChild = _node->content.asObject.child; + JsonNode* firstChild = _node->content.asContainer.child; JsonNode* lastChild = 0; for (JsonNode* child = firstChild; child; child = child->next) @@ -73,7 +73,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key) lastChild = child; } - JsonBuffer* buffer = _node->content.asObject.buffer; + JsonBuffer* buffer = _node->content.asContainer.buffer; JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED); if (!newValueNode) return 0; @@ -87,19 +87,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key) if (lastChild) lastChild->next = newKeyNode; else - _node->content.asObject.child = newKeyNode; + _node->content.asContainer.child = newKeyNode; return newValueNode; } - -size_t JsonObject::printTo(char* buffer, size_t bufferSize) const -{ - StringBuilder sb(buffer, bufferSize); - return printTo(sb); -} - -size_t JsonObject::printTo(Print& p) const -{ - JsonNodeSerializer serializer(p); - return serializer.serialize(_node); -} \ No newline at end of file diff --git a/srcs/JsonObject.h b/srcs/JsonObject.h index 313b911c..db10aa75 100644 --- a/srcs/JsonObject.h +++ b/srcs/JsonObject.h @@ -1,22 +1,21 @@ #pragma once -#include "Arduino/Printable.h" +#include "JsonContainer.h" class JsonValue; struct JsonNode; -class JsonObject : public Printable +class JsonObject : public JsonContainer { friend JsonValue; public: JsonObject() - : _node(0) { } - JsonObject(JsonNode* node) - : _node(node) + explicit JsonObject(JsonNode* node) + : JsonContainer(node) { } @@ -27,11 +26,6 @@ public: bool operator==(const JsonObject& other) const; - size_t printTo(char* buffer, size_t bufferSize) const; - virtual size_t printTo(Print& print) const; - private: - JsonNode* _node; - JsonNode* getOrCreateNodeAt(char const* key); }; \ No newline at end of file diff --git a/srcs/srcs.vcxproj b/srcs/srcs.vcxproj index af7a4b0a..7542e775 100644 --- a/srcs/srcs.vcxproj +++ b/srcs/srcs.vcxproj @@ -78,6 +78,7 @@ + @@ -89,6 +90,7 @@ + diff --git a/srcs/srcs.vcxproj.filters b/srcs/srcs.vcxproj.filters index e83bb14d..a37c1acf 100644 --- a/srcs/srcs.vcxproj.filters +++ b/srcs/srcs.vcxproj.filters @@ -45,6 +45,9 @@ Header Files + + Header Files + @@ -68,5 +71,8 @@ Source Files + + Source Files + \ No newline at end of file