Benoît Blanchon 11 роки тому
батько
коміт
bae5c36f41

+ 47 - 42
JsonGenerator/JsonArray.h

@@ -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));
-    }
+        template<int N>
+        class JsonArray : public JsonObjectBase
+        {
+        public:
+            JsonArray()
+            {
+                itemCount = 0;
+            }
 
-    void add(double value, int digits=2)
-    {
-        add(JsonValue(value, digits));
-    }
+            template<typename T>
+            void add(T value)
+            {
+                add(JsonValue(value));
+            }
 
-    void add(JsonValue value)
-    {
-        if (itemCount >= N) return;
+            void add(double value, int digits = 2)
+            {
+                add(JsonValue(value, digits));
+            }
 
-        items[itemCount] = value;
-        itemCount++;
-    }
+            void add(JsonValue value)
+            {
+                if (itemCount >= N) return;
 
-    using JsonObjectBase::printTo;
+                items[itemCount] = value;
+                itemCount++;
+            }
 
-private:
-    JsonValue items[N];
-    int itemCount;
+            using JsonObjectBase::printTo;
 
-    virtual size_t printTo(Print& p) const
-    {
-        size_t n = 0;
-        
-        n += p.write('[');
+        private:
+            JsonValue items[N];
+            int itemCount;
 
-        for (int i = 0; i < itemCount; i++)
-        {
-            if (i > 0)
+            virtual size_t printTo(Print& p) const
             {
-                n += p.write(',');
-            }
+                size_t n = 0;
 
-            n += items[i].printTo(p);
-        }
+                n += p.write('[');
 
-        n += p.write(']');
+                for (int i = 0; i < itemCount; i++)
+                {
+                    if (i > 0)
+                    {
+                        n += p.write(',');
+                    }
 
-        return n;
-    }
-};
+                    n += items[i].printTo(p);
+                }
 
+                n += p.write(']');
+
+                return n;
+            }
+        };
+    }
+}

+ 57 - 52
JsonGenerator/JsonHashTable.h

@@ -7,72 +7,77 @@
 
 #include "JsonObjectBase.h"
 
-template<int N>
-class JsonHashTable : public JsonObjectBase
+namespace ArduinoJson
 {
-public:
-
-    JsonHashTable()
-    {
-        itemCount = 0;
-    }
-
-    template<typename T>
-    void add(const char* key, T value)
+    namespace Generator
     {
-        add(key, JsonValue(value));
-    }
+        template<int N>
+        class JsonHashTable : public JsonObjectBase
+        {
+        public:
 
-    void add(const char* key, double value, int digits=2)
-    {
-        add(key, JsonValue(value, digits));
-    }
+            JsonHashTable()
+            {
+                itemCount = 0;
+            }
 
-    void add(const char* key, JsonValue value)
-    {
-        if (itemCount >= N) return;
+            template<typename T>
+            void add(const char* key, T value)
+            {
+                add(key, JsonValue(value));
+            }
 
-        items[itemCount].key = key;
-        items[itemCount].value = value;
-        itemCount++;
-    }
+            void add(const char* key, double value, int digits = 2)
+            {
+                add(key, JsonValue(value, digits));
+            }
 
-    using JsonObjectBase::printTo;
-    
-private:
+            void add(const char* key, JsonValue value)
+            {
+                if (itemCount >= N) return;
 
-    struct KeyValuePair
-    {
-        const char* key;
-        JsonValue value;
-    };
+                items[itemCount].key = key;
+                items[itemCount].value = value;
+                itemCount++;
+            }
 
-    KeyValuePair items[N];
-    int itemCount;
+            using JsonObjectBase::printTo;
 
-    virtual size_t printTo(Print& p) const
-    {
-        size_t n = 0;
+        private:
 
-        n += p.write('{');
+            struct KeyValuePair
+            {
+                const char* key;
+                JsonValue value;
+            };
 
-        for (int i = 0; i < itemCount; i++)
-        {
-            JsonValue key(items[i].key);
+            KeyValuePair items[N];
+            int itemCount;
 
-            if (i > 0)
+            virtual size_t printTo(Print& p) const
             {
-                n += p.write(',');
-            }
+                size_t n = 0;
 
-            n += key.printTo(p);
-            n += p.write(':');
-            n += items[i].value.printTo(p);
-        }
+                n += p.write('{');
 
-        n += p.write('}');
+                for (int i = 0; i < itemCount; i++)
+                {
+                    JsonValue key(items[i].key);
 
-        return n;
-    }
-};
+                    if (i > 0)
+                    {
+                        n += p.write(',');
+                    }
+
+                    n += key.printTo(p);
+                    n += p.write(':');
+                    n += items[i].value.printTo(p);
+                }
 
+                n += p.write('}');
+
+                return n;
+            }
+        };
+    }
+}

+ 14 - 9
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:
 
-    virtual size_t printTo(Print& p) const = 0;
-};
+            size_t printTo(char* buffer, size_t bufferSize)
+            {
+                StringBuilder sb(buffer, bufferSize);
+                return printTo(sb);
+            }
 
+            virtual size_t printTo(Print& p) const = 0;
+        };
+    }
+}

+ 2 - 0
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");

+ 82 - 76
JsonGenerator/JsonValue.h

@@ -8,87 +8,93 @@
 #include "Printable.h"
 #include "StringBuilder.h"
 
-class JsonValue : public Printable
-{    
-public:
-
-    JsonValue()
-    {
-    }
-    
-    JsonValue(bool value)
-        : implementation(&JsonValue::printBoolTo)
+namespace ArduinoJson
+{
+    namespace Generator
     {
-        content.asBool = value;
-    }
+        class JsonValue : public Printable
+        {
+        public:
 
-    JsonValue(double value, int digits=2)
-        : implementation(&JsonValue::printDoubleTo)
-    {
-        content.asDouble.value = value;
-        content.asDouble.digits = digits;
-    }
+            JsonValue()
+            {
+            }
 
-    JsonValue(float value)
-        : implementation(&JsonValue::printFloatTo)
-    {
-        content.asFloat = value;
-    }
-    
-    JsonValue(long value)
-        : implementation(&JsonValue::printLongTo)
-    {
-        content.asLong = value;
-    }
+            JsonValue(bool value)
+                : implementation(&JsonValue::printBoolTo)
+            {
+                content.asBool = value;
+            }
 
-    JsonValue(int value) 
-        : implementation(&JsonValue::printLongTo)
-    {
-        content.asLong = value;
-    }
+            JsonValue(double value, int digits = 2)
+                : implementation(&JsonValue::printDoubleTo)
+            {
+                content.asDouble.value = value;
+                content.asDouble.digits = digits;
+            }
 
-    JsonValue(Printable& value)
-        : implementation(&JsonValue::printPrintableTo)
-    {
-        content.asPrintable = &value;
-    }
+            JsonValue(float value)
+                : implementation(&JsonValue::printFloatTo)
+            {
+                content.asFloat = value;
+            }
 
-    JsonValue(const char* value)
-        : implementation(&JsonValue::printStringTo)
-    {
-        content.asString = value;
-    }
+            JsonValue(long value)
+                : implementation(&JsonValue::printLongTo)
+            {
+                content.asLong = value;
+            }
 
-    virtual size_t printTo(Print& p) const
-    {
-        // handmade polymorphism
-        return (this->*implementation)(p);
+            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;
+        };
     }
-    
-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;
-};
+}

+ 2 - 0
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;

+ 18 - 13
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;
-    }
-
-    virtual size_t write(uint8_t c);
+        class StringBuilder : public Print
+        {
+        public:
+            StringBuilder(char* buf, int size)
+                : buffer(buf), capacity(size - 1), length(0)
+            {
+                buffer[0] = 0;
+            }
 
-private:
-    char* buffer;
-    int capacity;
-    int length;
-};
+            virtual size_t write(uint8_t c);
 
+        private:
+            char* buffer;
+            int capacity;
+            int length;
+        };
+    }
+}

+ 1 - 0
JsonGeneratorTests/JsonArrayTests.cpp

@@ -3,6 +3,7 @@
 #include "JsonHashTable.h"
 
 using namespace Microsoft::VisualStudio::CppUnitTestFramework;
+using namespace ArduinoJson::Generator;
 
 namespace JsonGeneratorTests
 {		

+ 1 - 0
JsonGeneratorTests/JsonHashTableTests.cpp

@@ -3,6 +3,7 @@
 #include "JsonHashTable.h"
 
 using namespace Microsoft::VisualStudio::CppUnitTestFramework;
+using namespace ArduinoJson::Generator;
 
 namespace JsonGeneratorTests
 {

+ 1 - 0
JsonGeneratorTests/JsonValueTests.cpp

@@ -3,6 +3,7 @@
 #include "JsonValue.h"
 
 using namespace Microsoft::VisualStudio::CppUnitTestFramework;
+using namespace ArduinoJson::Generator;
 
 namespace JsonGeneratorTests
 {

+ 1 - 0
JsonGeneratorTests/StringBuilderTests.cpp

@@ -2,6 +2,7 @@
 #include "StringBuilder.h"
 
 using namespace Microsoft::VisualStudio::CppUnitTestFramework;
+using namespace ArduinoJson::Generator;
 
 namespace JsonGeneratorTests
 {

+ 2 - 0
examples/JsonGeneratorExample/JsonGeneratorExample.ino

@@ -5,6 +5,8 @@
 
 #include <JsonGenerator.h>
 
+using namespace ArduinoJson::Generator;
+
 void setup()
 {
     Serial.begin(9600);