From 4c387e906253bc4bc9a3f0091848fef5d4ed3951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Wed, 25 Jun 2014 13:23:08 +0200 Subject: [PATCH] Extracted base class JsonObjectBase --- JsonGeneratorTests/JsonArray.h | 51 ++-------------- JsonGeneratorTests/JsonGeneratorTests.vcxproj | 2 + .../JsonGeneratorTests.vcxproj.filters | 6 ++ JsonGeneratorTests/JsonObjectBase.cpp | 1 + JsonGeneratorTests/JsonObjectBase.h | 60 +++++++++++++++++++ 5 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 JsonGeneratorTests/JsonObjectBase.cpp create mode 100644 JsonGeneratorTests/JsonObjectBase.h diff --git a/JsonGeneratorTests/JsonArray.h b/JsonGeneratorTests/JsonArray.h index 895e0af3..0a8bd8e8 100644 --- a/JsonGeneratorTests/JsonArray.h +++ b/JsonGeneratorTests/JsonArray.h @@ -5,33 +5,12 @@ #pragma once +#include "JsonObjectBase.h" #include "StringBuilder.h" -enum JsonObjectType -{ - JSON_STRING, - JSON_NUMBER, - JSON_BOOLEAN, -}; - -union JsonObjectValue -{ - const char* string; - double number; - bool boolean; -}; - -struct JsonObject -{ - JsonObjectType type; - JsonObjectValue value; -}; - template -class JsonArray +class JsonArray : public JsonObjectBase { - - public: JsonArray() { @@ -69,34 +48,14 @@ private: JsonObject items[N]; int itemCount; - void writeTo(StringBuilder& sb) + virtual void writeTo(StringBuilder& sb) { sb.append("["); for (int i = 0; i < itemCount; i++) { - if (i>0) - sb.append(","); - - JsonObjectValue value = items[i].value; - - switch (items[i].type) - { - case JSON_STRING: - if (value.string) - sb.append("\"%s\"", value.string); - else - sb.append("null"); - break; - - case JSON_NUMBER: - sb.append("%lg", value.number); - break; - - case JSON_BOOLEAN: - sb.append(value.boolean ? "true" : "false"); - break; - } + if (i>0) sb.append(","); + writeObjectTo(items[i], sb); } sb.append("]"); diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj b/JsonGeneratorTests/JsonGeneratorTests.vcxproj index 4904ab4f..0a61f62f 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj @@ -84,10 +84,12 @@ + + diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters index 7f5fd808..d018d192 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters @@ -24,6 +24,9 @@ Source Files + + Source Files + @@ -32,5 +35,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/JsonGeneratorTests/JsonObjectBase.cpp b/JsonGeneratorTests/JsonObjectBase.cpp new file mode 100644 index 00000000..0aa044a6 --- /dev/null +++ b/JsonGeneratorTests/JsonObjectBase.cpp @@ -0,0 +1 @@ +#include "JsonObjectBase.h" diff --git a/JsonGeneratorTests/JsonObjectBase.h b/JsonGeneratorTests/JsonObjectBase.h new file mode 100644 index 00000000..23b0ad43 --- /dev/null +++ b/JsonGeneratorTests/JsonObjectBase.h @@ -0,0 +1,60 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "StringBuilder.h" + +class JsonObjectBase +{ +public: + + +protected: + + virtual void writeTo(StringBuilder& sb) = 0; + + enum JsonObjectType + { + JSON_STRING, + JSON_NUMBER, + JSON_BOOLEAN, + }; + + union JsonObjectValue + { + const char* string; + double number; + bool boolean; + }; + + struct JsonObject + { + JsonObjectType type; + JsonObjectValue value; + }; + + void writeObjectTo(const JsonObject& obj, StringBuilder& sb) + { + switch (obj.type) + { + case JSON_STRING: + if (obj.value.string) + sb.append("\"%s\"", obj.value.string); + else + sb.append("null"); + break; + + case JSON_NUMBER: + sb.append("%lg", obj.value.number); + break; + + case JSON_BOOLEAN: + sb.append(obj.value.boolean ? "true" : "false"); + break; + } + } +}; +