mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added namespace
This commit is contained in:
		| @@ -8,59 +8,64 @@ | ||||
| #include "JsonObjectBase.h" | ||||
| #include "StringBuilder.h" | ||||
|  | ||||
| template<int N> | ||||
| class JsonArray : public JsonObjectBase | ||||
| namespace ArduinoJson | ||||
| { | ||||
| public: | ||||
|     JsonArray() | ||||
|     namespace Generator | ||||
|     { | ||||
|         itemCount = 0; | ||||
|     } | ||||
|  | ||||
|     template<typename T> | ||||
|     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<int N> | ||||
|         class JsonArray : public JsonObjectBase | ||||
|         { | ||||
|             if (i > 0) | ||||
|         public: | ||||
|             JsonArray() | ||||
|             { | ||||
|                 n += p.write(','); | ||||
|                 itemCount = 0; | ||||
|             } | ||||
|  | ||||
|             n += items[i].printTo(p); | ||||
|         } | ||||
|             template<typename T> | ||||
|             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; | ||||
|             } | ||||
|         }; | ||||
|     } | ||||
| }; | ||||
|  | ||||
| } | ||||
| @@ -7,72 +7,77 @@ | ||||
|  | ||||
| #include "JsonObjectBase.h" | ||||
|  | ||||
| template<int N> | ||||
| class JsonHashTable : public JsonObjectBase | ||||
| namespace ArduinoJson | ||||
| { | ||||
| public: | ||||
|  | ||||
|     JsonHashTable() | ||||
|     namespace Generator | ||||
|     { | ||||
|         itemCount = 0; | ||||
|     } | ||||
|  | ||||
|     template<typename T> | ||||
|     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<int N> | ||||
|         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<typename T> | ||||
|             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; | ||||
|             } | ||||
|         }; | ||||
|     } | ||||
| }; | ||||
|  | ||||
| } | ||||
| @@ -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; | ||||
| }; | ||||
|  | ||||
| } | ||||
| @@ -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"); | ||||
|   | ||||
| @@ -8,87 +8,93 @@ | ||||
| #include "Printable.h" | ||||
| #include "StringBuilder.h" | ||||
|  | ||||
| class JsonValue : public Printable | ||||
| namespace ArduinoJson | ||||
| { | ||||
| public: | ||||
|  | ||||
|     JsonValue() | ||||
|     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; | ||||
| }; | ||||
| } | ||||
| @@ -5,6 +5,8 @@ | ||||
|  | ||||
| #include "StringBuilder.h" | ||||
|  | ||||
| using namespace ArduinoJson::Generator; | ||||
|  | ||||
| size_t StringBuilder::write(uint8_t c) | ||||
| { | ||||
|     if (length >= capacity) return 0; | ||||
|   | ||||
| @@ -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; | ||||
| }; | ||||
|  | ||||
| } | ||||
| @@ -3,6 +3,7 @@ | ||||
| #include "JsonHashTable.h" | ||||
|  | ||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||
| using namespace ArduinoJson::Generator; | ||||
|  | ||||
| namespace JsonGeneratorTests | ||||
| {		 | ||||
|   | ||||
| @@ -3,6 +3,7 @@ | ||||
| #include "JsonHashTable.h" | ||||
|  | ||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||
| using namespace ArduinoJson::Generator; | ||||
|  | ||||
| namespace JsonGeneratorTests | ||||
| { | ||||
|   | ||||
| @@ -3,6 +3,7 @@ | ||||
| #include "JsonValue.h" | ||||
|  | ||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||
| using namespace ArduinoJson::Generator; | ||||
|  | ||||
| namespace JsonGeneratorTests | ||||
| { | ||||
|   | ||||
| @@ -2,6 +2,7 @@ | ||||
| #include "StringBuilder.h" | ||||
|  | ||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||
| using namespace ArduinoJson::Generator; | ||||
|  | ||||
| namespace JsonGeneratorTests | ||||
| { | ||||
|   | ||||
| @@ -5,6 +5,8 @@ | ||||
|  | ||||
| #include <JsonGenerator.h> | ||||
|  | ||||
| using namespace ArduinoJson::Generator; | ||||
|  | ||||
| void setup() | ||||
| { | ||||
|     Serial.begin(9600); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user