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 "JsonObjectBase.h" | ||||||
| #include "StringBuilder.h" | #include "StringBuilder.h" | ||||||
|  |  | ||||||
| template<int N> | namespace ArduinoJson | ||||||
| class JsonArray : public JsonObjectBase |  | ||||||
| { | { | ||||||
| public: |     namespace Generator | ||||||
|     JsonArray() |  | ||||||
|     { |     { | ||||||
|         itemCount = 0; |         template<int N> | ||||||
|     } |         class JsonArray : public JsonObjectBase | ||||||
|  |  | ||||||
|     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++) |  | ||||||
|         { |         { | ||||||
|             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" | #include "JsonObjectBase.h" | ||||||
|  |  | ||||||
| template<int N> | namespace ArduinoJson | ||||||
| class JsonHashTable : public JsonObjectBase |  | ||||||
| { | { | ||||||
| public: |     namespace Generator | ||||||
|  |  | ||||||
|     JsonHashTable() |  | ||||||
|     { |     { | ||||||
|         itemCount = 0; |         template<int N> | ||||||
|     } |         class JsonHashTable : public JsonObjectBase | ||||||
|  |  | ||||||
|     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++) |  | ||||||
|         { |         { | ||||||
|             JsonValue key(items[i].key); |         public: | ||||||
|  |  | ||||||
|             if (i > 0) |             JsonHashTable() | ||||||
|             { |             { | ||||||
|                 n += p.write(','); |                 itemCount = 0; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             n += key.printTo(p); |             template<typename T> | ||||||
|             n += p.write(':'); |             void add(const char* key, T value) | ||||||
|             n += items[i].value.printTo(p); |             { | ||||||
|         } |                 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 "Print.h" | ||||||
| #include "Printable.h" | #include "Printable.h" | ||||||
|  |  | ||||||
| class JsonObjectBase : public Printable | namespace ArduinoJson | ||||||
| { | { | ||||||
| public: |     namespace Generator | ||||||
|  |  | ||||||
|     size_t printTo(char* buffer, size_t bufferSize) |  | ||||||
|     { |     { | ||||||
|         StringBuilder sb(buffer, bufferSize); |         class JsonObjectBase : public Printable | ||||||
|         return printTo(sb); |         { | ||||||
|  |         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" | #include "JsonValue.h" | ||||||
|  |  | ||||||
|  | using namespace ArduinoJson::Generator; | ||||||
|  |  | ||||||
| size_t JsonValue::printBoolTo(Print& p) const | size_t JsonValue::printBoolTo(Print& p) const | ||||||
| { | { | ||||||
|     return p.print(content.asBool ? "true" : "false"); |     return p.print(content.asBool ? "true" : "false"); | ||||||
|   | |||||||
| @@ -8,87 +8,93 @@ | |||||||
| #include "Printable.h" | #include "Printable.h" | ||||||
| #include "StringBuilder.h" | #include "StringBuilder.h" | ||||||
|  |  | ||||||
| class JsonValue : public Printable | namespace ArduinoJson | ||||||
| {     | { | ||||||
| public: |     namespace Generator | ||||||
|  |  | ||||||
|     JsonValue() |  | ||||||
|     { |     { | ||||||
|  |         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" | #include "StringBuilder.h" | ||||||
|  |  | ||||||
|  | using namespace ArduinoJson::Generator; | ||||||
|  |  | ||||||
| size_t StringBuilder::write(uint8_t c) | size_t StringBuilder::write(uint8_t c) | ||||||
| { | { | ||||||
|     if (length >= capacity) return 0; |     if (length >= capacity) return 0; | ||||||
|   | |||||||
| @@ -7,20 +7,25 @@ | |||||||
|  |  | ||||||
| #include "Print.h" | #include "Print.h" | ||||||
|  |  | ||||||
| class StringBuilder : public Print | namespace ArduinoJson | ||||||
| { | { | ||||||
| public: |     namespace Generator | ||||||
|     StringBuilder(char* buf, int size) |  | ||||||
|         : buffer(buf), capacity(size-1), length(0) |  | ||||||
|     { |     { | ||||||
|         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" | #include "JsonHashTable.h" | ||||||
|  |  | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Generator; | ||||||
|  |  | ||||||
| namespace JsonGeneratorTests | namespace JsonGeneratorTests | ||||||
| {		 | {		 | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ | |||||||
| #include "JsonHashTable.h" | #include "JsonHashTable.h" | ||||||
|  |  | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Generator; | ||||||
|  |  | ||||||
| namespace JsonGeneratorTests | namespace JsonGeneratorTests | ||||||
| { | { | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ | |||||||
| #include "JsonValue.h" | #include "JsonValue.h" | ||||||
|  |  | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Generator; | ||||||
|  |  | ||||||
| namespace JsonGeneratorTests | namespace JsonGeneratorTests | ||||||
| { | { | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ | |||||||
| #include "StringBuilder.h" | #include "StringBuilder.h" | ||||||
|  |  | ||||||
| using namespace Microsoft::VisualStudio::CppUnitTestFramework; | using namespace Microsoft::VisualStudio::CppUnitTestFramework; | ||||||
|  | using namespace ArduinoJson::Generator; | ||||||
|  |  | ||||||
| namespace JsonGeneratorTests | namespace JsonGeneratorTests | ||||||
| { | { | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ | |||||||
|  |  | ||||||
| #include <JsonGenerator.h> | #include <JsonGenerator.h> | ||||||
|  |  | ||||||
|  | using namespace ArduinoJson::Generator; | ||||||
|  |  | ||||||
| void setup() | void setup() | ||||||
| { | { | ||||||
|     Serial.begin(9600); |     Serial.begin(9600); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user