From 024027049271dc3242ea71bf3e08602b39ac34d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Fri, 27 Jun 2014 13:42:26 +0200 Subject: [PATCH] Extracted class JsonValue --- JsonGeneratorTests/JsonArray.h | 35 ++++------ JsonGeneratorTests/JsonGeneratorTests.vcxproj | 3 +- .../JsonGeneratorTests.vcxproj.filters | 9 ++- JsonGeneratorTests/JsonHashTable.h | 27 ++++---- JsonGeneratorTests/JsonObjectBase.cpp | 26 ------- JsonGeneratorTests/JsonObjectBase.h | 28 +------- JsonGeneratorTests/JsonValue.cpp | 32 +++++++++ JsonGeneratorTests/JsonValue.h | 67 +++++++++++++++++++ 8 files changed, 133 insertions(+), 94 deletions(-) delete mode 100644 JsonGeneratorTests/JsonObjectBase.cpp create mode 100644 JsonGeneratorTests/JsonValue.cpp create mode 100644 JsonGeneratorTests/JsonValue.h diff --git a/JsonGeneratorTests/JsonArray.h b/JsonGeneratorTests/JsonArray.h index e279bace..58e81cde 100644 --- a/JsonGeneratorTests/JsonArray.h +++ b/JsonGeneratorTests/JsonArray.h @@ -19,30 +19,30 @@ public: void add(const char* value) { - JsonValueContent v; - v.string = value; - addItem(JSON_STRING, v); + addItem(JsonValue(value)); } void add(double value) { - JsonValueContent v; - v.number = value; - addItem(JSON_NUMBER, v); + addItem(JsonValue(value)); } void add(bool value) { - JsonValueContent v; - v.boolean = value; - addItem(JSON_BOOLEAN, v); + addItem(JsonValue(value)); } void add(JsonObjectBase& value) { - JsonValueContent v; - v.object = &value; - addItem(JSON_OBJECT, v); + addItem(JsonValue(value)); + } + + void addItem(JsonValue value) + { + if (itemCount >= N) return; + + items[itemCount] = value; + itemCount++; } using JsonObjectBase::writeTo; @@ -58,19 +58,10 @@ private: for (int i = 0; i < itemCount; i++) { if (i>0) sb.append(","); - writeValueTo(items[i], sb); + items[i].writeTo(sb); } sb.append("]"); } - - void addItem(JsonValueType type, JsonValueContent content) - { - if (itemCount >= N) return; - - items[itemCount].type = type; - items[itemCount].content = content; - itemCount++; - } }; diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj b/JsonGeneratorTests/JsonGeneratorTests.vcxproj index 16a0860d..18e185bf 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj @@ -84,7 +84,7 @@ - + @@ -93,6 +93,7 @@ + diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters index 0962d62c..b6aff73e 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters @@ -21,9 +21,6 @@ Source Files - - Source Files - Source Files @@ -33,6 +30,9 @@ Source Files + + Source Files + @@ -47,5 +47,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/JsonGeneratorTests/JsonHashTable.h b/JsonGeneratorTests/JsonHashTable.h index 6932618d..0919058a 100644 --- a/JsonGeneratorTests/JsonHashTable.h +++ b/JsonGeneratorTests/JsonHashTable.h @@ -19,13 +19,20 @@ public: void add(const char* key, const char* value) { - JsonValueContent v; - v.string = value; - addItem(key, JSON_STRING, v); + addItem(key, JsonValue(value)); + } + + void addItem(const char* key, JsonValue value) + { + if (itemCount >= N) return; + + items[itemCount].key = key; + items[itemCount].value = value; + itemCount++; } using JsonObjectBase::writeTo; - + private: struct KeyValuePair @@ -46,20 +53,10 @@ private: if (i>0) sb.append(","); sb.appendEscaped(items[i].key); sb.append(":"); - writeValueTo(items[i].value, sb); + items[i].value.writeTo(sb); } sb.append("}"); } - - void addItem(const char* key, JsonValueType type, JsonValueContent content) - { - if (itemCount >= N) return; - - items[itemCount].key = key; - items[itemCount].value.type = type; - items[itemCount].value.content = content; - itemCount++; - } }; diff --git a/JsonGeneratorTests/JsonObjectBase.cpp b/JsonGeneratorTests/JsonObjectBase.cpp deleted file mode 100644 index 008278c2..00000000 --- a/JsonGeneratorTests/JsonObjectBase.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "JsonObjectBase.h" - -void JsonObjectBase::writeValueTo(JsonValue& obj, StringBuilder& sb) -{ - switch (obj.type) - { - case JSON_STRING: - sb.appendEscaped(obj.content.string); - break; - - case JSON_NUMBER: - sb.append(obj.content.number); - break; - - case JSON_BOOLEAN: - sb.append(obj.content.boolean ? "true" : "false"); - break; - - case JSON_OBJECT: - if (obj.content.object) - obj.content.object->writeTo(sb); - else - sb.append("null"); - break; - } -} \ No newline at end of file diff --git a/JsonGeneratorTests/JsonObjectBase.h b/JsonGeneratorTests/JsonObjectBase.h index 1e15300c..c9939250 100644 --- a/JsonGeneratorTests/JsonObjectBase.h +++ b/JsonGeneratorTests/JsonObjectBase.h @@ -5,7 +5,7 @@ #pragma once -#include "StringBuilder.h" +#include "JsonValue.h" class JsonObjectBase { @@ -17,32 +17,6 @@ public: writeTo(sb); } -protected: - - enum JsonValueType - { - JSON_STRING, - JSON_NUMBER, - JSON_BOOLEAN, - JSON_OBJECT, - }; - - union JsonValueContent - { - const char* string; - double number; - bool boolean; - JsonObjectBase* object; - }; - - struct JsonValue - { - JsonValueType type; - JsonValueContent content; - }; - - void writeValueTo(JsonValue& obj, StringBuilder& sb); - virtual void writeTo(StringBuilder& sb) = 0; }; diff --git a/JsonGeneratorTests/JsonValue.cpp b/JsonGeneratorTests/JsonValue.cpp new file mode 100644 index 00000000..fd99d1ed --- /dev/null +++ b/JsonGeneratorTests/JsonValue.cpp @@ -0,0 +1,32 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#include "JsonValue.h" +#include "JsonObjectBase.h" + +void JsonValue::writeTo(StringBuilder& sb) +{ + switch (type) + { + case JSON_STRING: + sb.appendEscaped(content.string); + break; + + case JSON_NUMBER: + sb.append(content.number); + break; + + case JSON_BOOLEAN: + sb.append(content.boolean ? "true" : "false"); + break; + + case JSON_OBJECT: + if (content.object) + ((JsonObjectBase*)content.object)->writeTo(sb); + else + sb.append("null"); + break; + } +} \ No newline at end of file diff --git a/JsonGeneratorTests/JsonValue.h b/JsonGeneratorTests/JsonValue.h new file mode 100644 index 00000000..a1567c84 --- /dev/null +++ b/JsonGeneratorTests/JsonValue.h @@ -0,0 +1,67 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#pragma once + +#include "StringBuilder.h" + +class JsonObjectBase; + +class JsonValue +{ +public: + + JsonValue() + { + } + + JsonValue(const char* value) + : type(JSON_STRING) + { + content.string = value; + } + + JsonValue(double value) + : type(JSON_NUMBER) + { + content.number = value; + } + + JsonValue(bool value) + : type(JSON_BOOLEAN) + { + content.boolean = value; + } + + JsonValue(JsonObjectBase& value) + : type(JSON_OBJECT) + { + content.object = &value; + } + + void writeTo(StringBuilder& sb); + +private: + + enum Type + { + JSON_STRING, + JSON_NUMBER, + JSON_BOOLEAN, + JSON_OBJECT, + }; + + union Content + { + const char* string; + double number; + bool boolean; + JsonObjectBase* object; + }; + + Type type; + Content content; +}; +