From bbc18b5ca433094712b1c0da001d089d858d6755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Tue, 8 Jul 2014 13:27:29 +0200 Subject: [PATCH] Extracted class JsonHashTableBase to reduce the size of the code. --- JsonGenerator.cpp | 5 +- JsonGenerator/JsonHashTable.h | 65 ++----------------- JsonGenerator/JsonHashTableBase.cpp | 26 ++++++++ JsonGenerator/JsonHashTableBase.h | 62 ++++++++++++++++++ JsonGenerator/README.md | 2 +- JsonGeneratorTests/JsonGeneratorTests.vcxproj | 2 + .../JsonGeneratorTests.vcxproj.filters | 6 ++ 7 files changed, 105 insertions(+), 63 deletions(-) create mode 100644 JsonGenerator/JsonHashTableBase.cpp create mode 100644 JsonGenerator/JsonHashTableBase.h diff --git a/JsonGenerator.cpp b/JsonGenerator.cpp index 4e8e123b..9250e2f8 100644 --- a/JsonGenerator.cpp +++ b/JsonGenerator.cpp @@ -5,6 +5,7 @@ // This file is here to help the Arduino IDE find the .cpp files +#include "JsonGenerator/EscapedString.cpp" #include "JsonGenerator/JsonValue.cpp" -#include "JsonGenerator/StringBuilder.cpp" -#include "JsonGenerator/EscapedString.cpp" \ No newline at end of file +#include "JsonGenerator/JsonHashTableBase.cpp" +#include "JsonGenerator/StringBuilder.cpp" \ No newline at end of file diff --git a/JsonGenerator/JsonHashTable.h b/JsonGenerator/JsonHashTable.h index a42083cc..b7bf9f45 100644 --- a/JsonGenerator/JsonHashTable.h +++ b/JsonGenerator/JsonHashTable.h @@ -5,78 +5,23 @@ #pragma once -#include "EscapedString.h" -#include "JsonObjectBase.h" +#include "JsonHashTableBase.h" namespace ArduinoJson { namespace Generator { template - class JsonHashTable : public JsonObjectBase + class JsonHashTable : public JsonHashTableBase { public: - JsonHashTable() + : JsonHashTableBase(items, N) { - itemCount = 0; - } - - template - void add(const char* key, T value) - { - if (itemCount >= N) return; - - items[itemCount].key.set(key); - items[itemCount].value.set(value); - itemCount++; - } - - template - void add(const char* key, double value) - { - if (itemCount >= N) return; - - items[itemCount].key.set(key); - items[itemCount].value.set(value); - itemCount++; - } - - using JsonObjectBase::printTo; + } private: - - struct KeyValuePair - { - Internals::EscapedString key; - Internals::JsonValue value; - }; - - KeyValuePair items[N]; - int itemCount; - - virtual size_t printTo(Print& p) const - { - size_t n = 0; - - n += p.write('{'); - - for (int i = 0; i < itemCount; i++) - { - if (i > 0) - { - n += p.write(','); - } - - n += items[i].key.printTo(p); - n += p.write(':'); - n += items[i].value.printTo(p); - } - - n += p.write('}'); - - return n; - } + KeyValuePair items[N]; }; } } \ No newline at end of file diff --git a/JsonGenerator/JsonHashTableBase.cpp b/JsonGenerator/JsonHashTableBase.cpp new file mode 100644 index 00000000..738ecd67 --- /dev/null +++ b/JsonGenerator/JsonHashTableBase.cpp @@ -0,0 +1,26 @@ +#include "JsonHashTable.h" + +using namespace ArduinoJson::Generator; + +size_t JsonHashTableBase::printTo(Print& p) const +{ + size_t n = 0; + + n += p.write('{'); + + for (int i = 0; i < itemCount; i++) + { + if (i > 0) + { + n += p.write(','); + } + + n += items[i].key.printTo(p); + n += p.write(':'); + n += items[i].value.printTo(p); + } + + n += p.write('}'); + + return n; +} \ No newline at end of file diff --git a/JsonGenerator/JsonHashTableBase.h b/JsonGenerator/JsonHashTableBase.h new file mode 100644 index 00000000..90c8aeef --- /dev/null +++ b/JsonGenerator/JsonHashTableBase.h @@ -0,0 +1,62 @@ +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#pragma once + +#include "JsonObjectBase.h" +#include "EscapedString.h" + +namespace ArduinoJson +{ + namespace Generator + { + class JsonHashTableBase : public JsonObjectBase + { + public: + + template + void add(const char* key, T value) + { + if (itemCount >= capacity) return; + + items[itemCount].key.set(key); + items[itemCount].value.set(value); + itemCount++; + } + + template + void add(const char* key, double value) + { + if (itemCount >= capacity) return; + + items[itemCount].key.set(key); + items[itemCount].value.set(value); + itemCount++; + } + + using JsonObjectBase::printTo; + + virtual size_t printTo(Print& p) const; + + protected: + + struct KeyValuePair + { + Internals::EscapedString key; + Internals::JsonValue value; + }; + + JsonHashTableBase(KeyValuePair* items, int capacity) + : items(items), capacity(capacity), itemCount(0) + { + } + + private: + KeyValuePair* items; + int itemCount; + int capacity; + }; + } +} \ No newline at end of file diff --git a/JsonGenerator/README.md b/JsonGenerator/README.md index 8113e8ed..26c325df 100644 --- a/JsonGenerator/README.md +++ b/JsonGenerator/README.md @@ -164,4 +164,4 @@ This table is for an 8-bit Arduino, types would be bigger on a 32-bit processor. | Type | Size in bytes | | ---------------------- | ------------- | | JsonArray<N> | 4 + 6 x N | -| JsonHashTable<N> | 4 + 8 x N | +| JsonHashTable<N> | 8 + 8 x N | diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj b/JsonGeneratorTests/JsonGeneratorTests.vcxproj index b7c0ac24..ae2dfc70 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj @@ -85,6 +85,7 @@ + @@ -97,6 +98,7 @@ + diff --git a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters index 99116900..9e50c6c3 100644 --- a/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters +++ b/JsonGeneratorTests/JsonGeneratorTests.vcxproj.filters @@ -39,6 +39,9 @@ Source Files + + Source Files + @@ -65,5 +68,8 @@ Header Files + + Header Files + \ No newline at end of file