diff --git a/JsonGenerator/JsonArray.h b/JsonGenerator/JsonArray.h index 1eb2ecf7..c155eb29 100644 --- a/JsonGenerator/JsonArray.h +++ b/JsonGenerator/JsonArray.h @@ -8,59 +8,64 @@ #include "JsonObjectBase.h" #include "StringBuilder.h" -template -class JsonArray : public JsonObjectBase +namespace ArduinoJson { -public: - JsonArray() + namespace Generator { - itemCount = 0; - } - - template - void add(T value) - { - add(JsonValue(value)); - } - - void add(double value, int digits=2) - { - add(JsonValue(value, digits)); - } - - void add(JsonValue value) - { - if (itemCount >= N) return; - - items[itemCount] = value; - itemCount++; - } - - using JsonObjectBase::printTo; - -private: - JsonValue 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++) + template + class JsonArray : public JsonObjectBase { - if (i > 0) + public: + JsonArray() { - n += p.write(','); + itemCount = 0; } - n += items[i].printTo(p); - } + template + void add(T value) + { + add(JsonValue(value)); + } - n += p.write(']'); + void add(double value, int digits = 2) + { + add(JsonValue(value, digits)); + } - return n; + void add(JsonValue value) + { + if (itemCount >= N) return; + + items[itemCount] = value; + itemCount++; + } + + using JsonObjectBase::printTo; + + private: + JsonValue 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].printTo(p); + } + + n += p.write(']'); + + return n; + } + }; } -}; - +} \ No newline at end of file diff --git a/JsonGenerator/JsonHashTable.h b/JsonGenerator/JsonHashTable.h index dc164a19..d5098918 100644 --- a/JsonGenerator/JsonHashTable.h +++ b/JsonGenerator/JsonHashTable.h @@ -7,72 +7,77 @@ #include "JsonObjectBase.h" -template -class JsonHashTable : public JsonObjectBase +namespace ArduinoJson { -public: - - JsonHashTable() + namespace Generator { - itemCount = 0; - } - - template - void add(const char* key, T value) - { - add(key, JsonValue(value)); - } - - void add(const char* key, double value, int digits=2) - { - add(key, JsonValue(value, digits)); - } - - void add(const char* key, JsonValue value) - { - if (itemCount >= N) return; - - items[itemCount].key = key; - items[itemCount].value = value; - itemCount++; - } - - using JsonObjectBase::printTo; - -private: - - struct KeyValuePair - { - const char* key; - 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++) + template + class JsonHashTable : public JsonObjectBase { - JsonValue key(items[i].key); + public: - if (i > 0) + JsonHashTable() { - n += p.write(','); + itemCount = 0; } - n += key.printTo(p); - n += p.write(':'); - n += items[i].value.printTo(p); - } + template + void add(const char* key, T value) + { + add(key, JsonValue(value)); + } - n += p.write('}'); + void add(const char* key, double value, int digits = 2) + { + add(key, JsonValue(value, digits)); + } - return n; + void add(const char* key, JsonValue value) + { + if (itemCount >= N) return; + + items[itemCount].key = key; + items[itemCount].value = value; + itemCount++; + } + + using JsonObjectBase::printTo; + + private: + + struct KeyValuePair + { + const char* key; + 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++) + { + JsonValue key(items[i].key); + + if (i > 0) + { + n += p.write(','); + } + + n += 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/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index 25c41fc5..57363c44 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -9,16 +9,21 @@ #include "Print.h" #include "Printable.h" -class JsonObjectBase : public Printable +namespace ArduinoJson { -public: - - size_t printTo(char* buffer, size_t bufferSize) + namespace Generator { - StringBuilder sb(buffer, bufferSize); - return printTo(sb); + class JsonObjectBase : public Printable + { + public: + + size_t printTo(char* buffer, size_t bufferSize) + { + StringBuilder sb(buffer, bufferSize); + return printTo(sb); + } + + virtual size_t printTo(Print& p) const = 0; + }; } - - virtual size_t printTo(Print& p) const = 0; -}; - +} \ No newline at end of file diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index 6021fda1..8edf87e8 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/JsonGenerator/JsonValue.cpp @@ -5,6 +5,8 @@ #include "JsonValue.h" +using namespace ArduinoJson::Generator; + size_t JsonValue::printBoolTo(Print& p) const { return p.print(content.asBool ? "true" : "false"); diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index f1b0d80e..ed9c3af4 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -8,87 +8,93 @@ #include "Printable.h" #include "StringBuilder.h" -class JsonValue : public Printable -{ -public: - - JsonValue() +namespace ArduinoJson +{ + namespace Generator { + class JsonValue : public Printable + { + public: + + JsonValue() + { + } + + JsonValue(bool value) + : implementation(&JsonValue::printBoolTo) + { + content.asBool = value; + } + + JsonValue(double value, int digits = 2) + : implementation(&JsonValue::printDoubleTo) + { + content.asDouble.value = value; + content.asDouble.digits = digits; + } + + JsonValue(float value) + : implementation(&JsonValue::printFloatTo) + { + content.asFloat = value; + } + + JsonValue(long value) + : implementation(&JsonValue::printLongTo) + { + content.asLong = value; + } + + JsonValue(int value) + : implementation(&JsonValue::printLongTo) + { + content.asLong = value; + } + + JsonValue(Printable& value) + : implementation(&JsonValue::printPrintableTo) + { + content.asPrintable = &value; + } + + JsonValue(const char* value) + : implementation(&JsonValue::printStringTo) + { + content.asString = value; + } + + virtual size_t printTo(Print& p) const + { + // handmade polymorphism + return (this->*implementation)(p); + } + + private: + + union Content + { + bool asBool; + float asFloat; + long asLong; + Printable* asPrintable; + const char* asString; + + struct { + double value; + int digits; + } asDouble; + }; + + Content content; + + size_t(JsonValue::*implementation)(Print& p)const; + + size_t printBoolTo(Print& p) const; + size_t printDoubleTo(Print& p) const; + size_t printFloatTo(Print& p) const; + size_t printLongTo(Print& p) const; + size_t printPrintableTo(Print& p) const; + size_t printStringTo(Print& p) const; + }; } - - JsonValue(bool value) - : implementation(&JsonValue::printBoolTo) - { - content.asBool = value; - } - - JsonValue(double value, int digits=2) - : implementation(&JsonValue::printDoubleTo) - { - content.asDouble.value = value; - content.asDouble.digits = digits; - } - - JsonValue(float value) - : implementation(&JsonValue::printFloatTo) - { - content.asFloat = value; - } - - JsonValue(long value) - : implementation(&JsonValue::printLongTo) - { - content.asLong = value; - } - - JsonValue(int value) - : implementation(&JsonValue::printLongTo) - { - content.asLong = value; - } - - JsonValue(Printable& value) - : implementation(&JsonValue::printPrintableTo) - { - content.asPrintable = &value; - } - - JsonValue(const char* value) - : implementation(&JsonValue::printStringTo) - { - content.asString = value; - } - - virtual size_t printTo(Print& p) const - { - // handmade polymorphism - return (this->*implementation)(p); - } - -private: - - union Content - { - bool asBool; - float asFloat; - long asLong; - Printable* asPrintable; - const char* asString; - - struct { - double value; - int digits; - } asDouble; - }; - - Content content; - - size_t(JsonValue::*implementation)(Print& p)const; - - size_t printBoolTo(Print& p) const; - size_t printDoubleTo(Print& p) const; - size_t printFloatTo(Print& p) const; - size_t printLongTo(Print& p) const; - size_t printPrintableTo(Print& p) const; - size_t printStringTo(Print& p) const; -}; \ No newline at end of file +} \ No newline at end of file diff --git a/JsonGenerator/StringBuilder.cpp b/JsonGenerator/StringBuilder.cpp index 93e7e934..aaa96a2b 100644 --- a/JsonGenerator/StringBuilder.cpp +++ b/JsonGenerator/StringBuilder.cpp @@ -5,6 +5,8 @@ #include "StringBuilder.h" +using namespace ArduinoJson::Generator; + size_t StringBuilder::write(uint8_t c) { if (length >= capacity) return 0; diff --git a/JsonGenerator/StringBuilder.h b/JsonGenerator/StringBuilder.h index 42cd3195..dc16b4cb 100644 --- a/JsonGenerator/StringBuilder.h +++ b/JsonGenerator/StringBuilder.h @@ -7,20 +7,25 @@ #include "Print.h" -class StringBuilder : public Print +namespace ArduinoJson { -public: - StringBuilder(char* buf, int size) - : buffer(buf), capacity(size-1), length(0) + namespace Generator { - buffer[0] = 0; + class StringBuilder : public Print + { + public: + StringBuilder(char* buf, int size) + : buffer(buf), capacity(size - 1), length(0) + { + buffer[0] = 0; + } + + virtual size_t write(uint8_t c); + + private: + char* buffer; + int capacity; + int length; + }; } - - virtual size_t write(uint8_t c); - -private: - char* buffer; - int capacity; - int length; -}; - +} \ No newline at end of file diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index 3277fc3d..07e61f37 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -3,6 +3,7 @@ #include "JsonHashTable.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; namespace JsonGeneratorTests { diff --git a/JsonGeneratorTests/JsonHashTableTests.cpp b/JsonGeneratorTests/JsonHashTableTests.cpp index 8ad55998..d2f2a899 100644 --- a/JsonGeneratorTests/JsonHashTableTests.cpp +++ b/JsonGeneratorTests/JsonHashTableTests.cpp @@ -3,6 +3,7 @@ #include "JsonHashTable.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; namespace JsonGeneratorTests { diff --git a/JsonGeneratorTests/JsonValueTests.cpp b/JsonGeneratorTests/JsonValueTests.cpp index fa0b185d..0ba22684 100644 --- a/JsonGeneratorTests/JsonValueTests.cpp +++ b/JsonGeneratorTests/JsonValueTests.cpp @@ -3,6 +3,7 @@ #include "JsonValue.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; namespace JsonGeneratorTests { diff --git a/JsonGeneratorTests/StringBuilderTests.cpp b/JsonGeneratorTests/StringBuilderTests.cpp index aeab00f5..fe76490a 100644 --- a/JsonGeneratorTests/StringBuilderTests.cpp +++ b/JsonGeneratorTests/StringBuilderTests.cpp @@ -2,6 +2,7 @@ #include "StringBuilder.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace ArduinoJson::Generator; namespace JsonGeneratorTests { diff --git a/examples/JsonGeneratorExample/JsonGeneratorExample.ino b/examples/JsonGeneratorExample/JsonGeneratorExample.ino index 3d7b38d2..d83d36c0 100644 --- a/examples/JsonGeneratorExample/JsonGeneratorExample.ino +++ b/examples/JsonGeneratorExample/JsonGeneratorExample.ino @@ -5,6 +5,8 @@ #include +using namespace ArduinoJson::Generator; + void setup() { Serial.begin(9600);