From c99bdbf4b96ce3184e139ab4a86e01b6ddd1a011 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 1 Oct 2014 12:28:30 +0200 Subject: [PATCH] Extracted JsonNodeSerializer --- srcs/JsonNodeSerializer.cpp | 51 +++++++++++++++++++++++++++++++++++++ srcs/JsonNodeSerializer.h | 19 ++++++++++++++ srcs/JsonObject.cpp | 44 +++----------------------------- srcs/srcs.vcxproj | 2 ++ srcs/srcs.vcxproj.filters | 6 +++++ 5 files changed, 81 insertions(+), 41 deletions(-) create mode 100644 srcs/JsonNodeSerializer.cpp create mode 100644 srcs/JsonNodeSerializer.h diff --git a/srcs/JsonNodeSerializer.cpp b/srcs/JsonNodeSerializer.cpp new file mode 100644 index 00000000..6bcb4164 --- /dev/null +++ b/srcs/JsonNodeSerializer.cpp @@ -0,0 +1,51 @@ +#include "JsonNodeSerializer.h" + +#include "EscapedString.h" +#include "JsonNode.h" + +using namespace ArduinoJson::Internals; + +size_t JsonNodeSerializer::serialize(JsonNode const* node) +{ + size_t n = 0; + + n += _sink.write('{'); + + JsonNode* firstChild = node->content.asObject.child; + + for (JsonNode* child = firstChild; child; child = child->next) + { + const char* childKey = child->content.asKey.key; + JsonNode* childValue = child->content.asKey.value; + + n += EscapedString::printTo(childKey, _sink); + n += _sink.write(':'); + + switch (childValue->type) + { + case JSON_STRING: + n += EscapedString::printTo(childValue->content.asString, _sink); + break; + + case JSON_INTEGER: + n += _sink.print(childValue->content.asInteger); + break; + + case JSON_BOOLEAN: + n += _sink.print(childValue->content.asBoolean ? "true" : "false"); + break; + } + + if (childValue->type >= JSON_DOUBLE_0_DECIMALS) + n += _sink.print(childValue->content.asDouble, childValue->type - JSON_DOUBLE_0_DECIMALS); + + if (child->next) + { + n += _sink.write(','); + } + } + + n += _sink.write('}'); + + return n; +} \ No newline at end of file diff --git a/srcs/JsonNodeSerializer.h b/srcs/JsonNodeSerializer.h new file mode 100644 index 00000000..bf018e25 --- /dev/null +++ b/srcs/JsonNodeSerializer.h @@ -0,0 +1,19 @@ +#pragma once + +class Print; +struct JsonNode; + +class JsonNodeSerializer +{ +public: + explicit JsonNodeSerializer(Print& sink) + : _sink(sink) + { + } + + size_t serialize(const JsonNode* node); + +private: + Print& _sink; +}; + diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index 2f00a7ef..f9caa347 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -7,6 +7,7 @@ #include "JsonValue.h" #include "JsonNode.h" #include "StringBuilder.h" +#include "JsonNodeSerializer.h" using namespace ArduinoJson::Internals; @@ -99,45 +100,6 @@ size_t JsonObject::printTo(char* buffer, size_t bufferSize) const size_t JsonObject::printTo(Print& p) const { - size_t n = 0; - - n += p.write('{'); - - JsonNode* firstChild = _node->content.asObject.child; - - for (JsonNode* child = firstChild; child; child = child->next) - { - const char* childKey = child->content.asKey.key; - JsonNode* childValue = child->content.asKey.value; - - n += EscapedString::printTo(childKey, p); - n += p.write(':'); - - switch (childValue->type) - { - case JSON_STRING: - n += EscapedString::printTo(childValue->content.asString, p); - break; - - case JSON_INTEGER: - n += p.print(childValue->content.asInteger); - break; - - case JSON_BOOLEAN: - n += p.print(childValue->content.asBoolean ? "true" : "false"); - break; - } - - if (childValue->type >= JSON_DOUBLE_0_DECIMALS) - n += p.print(childValue->content.asDouble, childValue->type - JSON_DOUBLE_0_DECIMALS); - - if (child->next) - { - n += p.write(','); - } - } - - n += p.write('}'); - - return n; + JsonNodeSerializer serializer(p); + return serializer.serialize(_node); } \ No newline at end of file diff --git a/srcs/srcs.vcxproj b/srcs/srcs.vcxproj index b1ef050a..9e4cf523 100644 --- a/srcs/srcs.vcxproj +++ b/srcs/srcs.vcxproj @@ -70,6 +70,7 @@ + @@ -80,6 +81,7 @@ + diff --git a/srcs/srcs.vcxproj.filters b/srcs/srcs.vcxproj.filters index 0940e460..8b80182e 100644 --- a/srcs/srcs.vcxproj.filters +++ b/srcs/srcs.vcxproj.filters @@ -42,6 +42,9 @@ Header Files + + Header Files + @@ -62,5 +65,8 @@ Source Files + + Source Files + \ No newline at end of file