mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added JsonGenerator example for Arduino
This commit is contained in:
		
							
								
								
									
										9
									
								
								JsonGenerator.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								JsonGenerator.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | /* | ||||||
|  | * malloc-free JSON parser for Arduino | ||||||
|  | * Benoit Blanchon 2014 - MIT License | ||||||
|  | */ | ||||||
|  |  | ||||||
|  | // This file is here to help the Arduino IDE find the .cpp files | ||||||
|  |  | ||||||
|  | #include "JsonGenerator/JsonValue.cpp" | ||||||
|  | #include "JsonGenerator/StringBuilder.cpp" | ||||||
							
								
								
									
										7
									
								
								JsonGenerator.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								JsonGenerator.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | /* | ||||||
|  | * malloc-free JSON parser for Arduino | ||||||
|  | * Benoit Blanchon 2014 - MIT License | ||||||
|  | */ | ||||||
|  |  | ||||||
|  | #include "JsonGenerator/JsonArray.h" | ||||||
|  | #include "JsonGenerator/JsonHashTable.h" | ||||||
| @@ -31,13 +31,13 @@ public: | |||||||
|         itemCount++; |         itemCount++; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     using JsonObjectBase::writeTo; |     using JsonObjectBase::printTo; | ||||||
|  |  | ||||||
| private: | private: | ||||||
|     JsonValue items[N]; |     JsonValue items[N]; | ||||||
|     int itemCount; |     int itemCount; | ||||||
|  |  | ||||||
|     virtual size_t writeTo(Print& p) const |     virtual size_t printTo(Print& p) const | ||||||
|     { |     { | ||||||
|         size_t n = 0; |         size_t n = 0; | ||||||
|          |          | ||||||
| @@ -50,7 +50,7 @@ private: | |||||||
|                 n += p.write(","); |                 n += p.write(","); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             n += items[i].writeTo(p); |             n += items[i].printTo(p); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         n += p.write("]"); |         n += p.write("]"); | ||||||
|   | |||||||
| @@ -32,7 +32,7 @@ public: | |||||||
|         itemCount++; |         itemCount++; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     using JsonObjectBase::writeTo; |     using JsonObjectBase::printTo; | ||||||
|      |      | ||||||
| private: | private: | ||||||
|  |  | ||||||
| @@ -45,7 +45,7 @@ private: | |||||||
|     KeyValuePair items[N]; |     KeyValuePair items[N]; | ||||||
|     int itemCount; |     int itemCount; | ||||||
|  |  | ||||||
|     virtual size_t writeTo(Print& p) const |     virtual size_t printTo(Print& p) const | ||||||
|     { |     { | ||||||
|         size_t n = 0; |         size_t n = 0; | ||||||
|  |  | ||||||
| @@ -60,9 +60,9 @@ private: | |||||||
|                 n += p.write(','); |                 n += p.write(','); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             n += key.writeTo(p); |             n += key.printTo(p); | ||||||
|             n += p.write(':'); |             n += p.write(':'); | ||||||
|             n += items[i].value.writeTo(p); |             n += items[i].value.printTo(p); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         n += p.write('}'); |         n += p.write('}'); | ||||||
|   | |||||||
| @@ -13,12 +13,12 @@ class JsonObjectBase : public Printable | |||||||
| { | { | ||||||
| public: | public: | ||||||
|  |  | ||||||
|     size_t writeTo(char* buffer, size_t bufferSize) |     size_t printTo(char* buffer, size_t bufferSize) | ||||||
|     { |     { | ||||||
|         StringBuilder sb(buffer, bufferSize); |         StringBuilder sb(buffer, bufferSize); | ||||||
|         return writeTo(sb); |         return printTo(sb); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     virtual size_t writeTo(Print& p) const = 0; |     virtual size_t printTo(Print& p) const = 0; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,35 +4,34 @@ | |||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include "JsonValue.h" | #include "JsonValue.h" | ||||||
| #include "JsonObjectBase.h" |  | ||||||
| #include <cstdio> | #include <cstdio> | ||||||
| #include <cstring> | #include <cstring> | ||||||
|  |  | ||||||
| size_t JsonValue::writeBooleanTo(Print& p) const | size_t JsonValue::printBooleanTo(Print& p) const | ||||||
| { | { | ||||||
|     return p.write(content.boolean ? "true" : "false"); |     return p.write(content.boolean ? "true" : "false"); | ||||||
| } | } | ||||||
|  |  | ||||||
| size_t JsonValue::writeNumberTo(Print& p) const | size_t JsonValue::printNumberTo(Print& p) const | ||||||
| { | { | ||||||
|     char tmp[16]; |     char tmp[16]; | ||||||
|  |  | ||||||
|     _snprintf(tmp, sizeof(tmp), "%lg", content.number); |     sprintf(tmp, "%lg", content.number); | ||||||
|  |  | ||||||
|     return p.write(tmp); |     return p.write(tmp); | ||||||
| } | } | ||||||
|  |  | ||||||
| size_t JsonValue::writeObjectTo(Print& p) const | size_t JsonValue::printObjectTo(Print& p) const | ||||||
| { | { | ||||||
|     if (content.object) |     if (content.object) | ||||||
|         return ((JsonObjectBase*)content.object)->writeTo(p); |         return ((Printable*) content.object)->printTo(p); | ||||||
|     else |     else | ||||||
|         return p.write("null"); |         return p.write("null"); | ||||||
| } | } | ||||||
|  |  | ||||||
| size_t JsonValue::writeStringTo(Print& p) const | size_t JsonValue::printStringTo(Print& p) const | ||||||
| { | { | ||||||
|     auto s = content.string; |     const char* s = content.string; | ||||||
|  |  | ||||||
|     if (!s) |     if (!s) | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -17,30 +17,30 @@ public: | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     JsonValue(const char* value) |     JsonValue(const char* value) | ||||||
|         : implementation(&JsonValue::writeStringTo) |         : implementation(&JsonValue::printStringTo) | ||||||
|     { |     { | ||||||
|         content.string = value; |         content.string = value; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     JsonValue(double value) |     JsonValue(double value) | ||||||
|         : implementation(&JsonValue::writeNumberTo) |         : implementation(&JsonValue::printNumberTo) | ||||||
|     { |     { | ||||||
|         content.number = value; |         content.number = value; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     JsonValue(bool value) |     JsonValue(bool value) | ||||||
|         : implementation(&JsonValue::writeBooleanTo) |         : implementation(&JsonValue::printBooleanTo) | ||||||
|     { |     { | ||||||
|         content.boolean = value; |         content.boolean = value; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     JsonValue(Printable& value) |     JsonValue(Printable& value) | ||||||
|         : implementation(&JsonValue::writeObjectTo) |         : implementation(&JsonValue::printObjectTo) | ||||||
|     { |     { | ||||||
|         content.object = &value; |         content.object = &value; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     virtual size_t writeTo(Print& p) const |     virtual size_t printTo(Print& p) const | ||||||
|     { |     { | ||||||
|         // handmade polymorphism |         // handmade polymorphism | ||||||
|         return (this->*implementation)(p); |         return (this->*implementation)(p); | ||||||
| @@ -60,8 +60,8 @@ private: | |||||||
|  |  | ||||||
|     size_t(JsonValue::*implementation)(Print& p)const; |     size_t(JsonValue::*implementation)(Print& p)const; | ||||||
|  |  | ||||||
|     size_t writeBooleanTo(Print& p) const; |     size_t printBooleanTo(Print& p) const; | ||||||
|     size_t writeNumberTo(Print& p) const; |     size_t printNumberTo(Print& p) const; | ||||||
|     size_t writeObjectTo(Print& p) const; |     size_t printObjectTo(Print& p) const; | ||||||
|     size_t writeStringTo(Print& p) const; |     size_t printStringTo(Print& p) const; | ||||||
| }; | }; | ||||||
| @@ -5,12 +5,16 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #ifndef ARDUINO | ||||||
|  |  | ||||||
|  | typedef unsigned char uint8_t; | ||||||
|  |  | ||||||
| class Print | class Print | ||||||
| { | { | ||||||
| public: | public: | ||||||
|  |  | ||||||
|     virtual size_t write(char c) = 0; |     virtual size_t write(uint8_t c) = 0; | ||||||
|      |  | ||||||
|     size_t write(const char* s) |     size_t write(const char* s) | ||||||
|     { |     { | ||||||
|         size_t n = 0; |         size_t n = 0; | ||||||
| @@ -22,3 +26,8 @@ public: | |||||||
|     } |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | #else | ||||||
|  |  | ||||||
|  | #include <Print.h> | ||||||
|  |  | ||||||
|  | #endif | ||||||
|   | |||||||
| @@ -5,12 +5,20 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #ifndef ARDUINO | ||||||
|  |  | ||||||
| class Print; | class Print; | ||||||
|  |  | ||||||
| class Printable | class Printable | ||||||
| { | { | ||||||
| public: | public: | ||||||
|  |  | ||||||
|     virtual size_t writeTo(Print& p) const = 0; |     virtual size_t printTo(Print& p) const = 0; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | #else | ||||||
|  |  | ||||||
|  | #include <Printable.h> | ||||||
|  |  | ||||||
|  | #endif | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ | |||||||
|  |  | ||||||
| #include "StringBuilder.h" | #include "StringBuilder.h" | ||||||
|  |  | ||||||
| size_t StringBuilder::write(char c) | size_t StringBuilder::write(uint8_t c) | ||||||
| { | { | ||||||
|     if (length >= capacity) return 0; |     if (length >= capacity) return 0; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ public: | |||||||
|         buffer[0] = 0; |         buffer[0] = 0; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     virtual size_t write(char c); |     virtual size_t write(uint8_t c); | ||||||
|  |  | ||||||
| private: | private: | ||||||
|     char* buffer; |     char* buffer; | ||||||
|   | |||||||
| @@ -162,13 +162,13 @@ namespace JsonGeneratorTests | |||||||
|  |  | ||||||
|         void jsonIs(const char* expected) |         void jsonIs(const char* expected) | ||||||
|         {       |         {       | ||||||
|             arr.writeTo(buffer, sizeof(buffer)); |             arr.printTo(buffer, sizeof(buffer)); | ||||||
|             Assert::AreEqual(expected, buffer); |             Assert::AreEqual(expected, buffer); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         void returns(size_t expected) |         void returns(size_t expected) | ||||||
|         { |         { | ||||||
|             size_t actual = arr.writeTo(buffer, sizeof(buffer)); |             size_t actual = arr.printTo(buffer, sizeof(buffer)); | ||||||
|             Assert::AreEqual(expected, actual); |             Assert::AreEqual(expected, actual); | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|   | |||||||
| @@ -111,13 +111,13 @@ namespace JsonGeneratorTests | |||||||
|  |  | ||||||
|         void jsonIs(const char* expected) |         void jsonIs(const char* expected) | ||||||
|         {             |         {             | ||||||
|             hash.writeTo(buffer, sizeof(buffer)); |             hash.printTo(buffer, sizeof(buffer)); | ||||||
|             Assert::AreEqual(expected, buffer); |             Assert::AreEqual(expected, buffer); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         void returns(size_t expected) |         void returns(size_t expected) | ||||||
|         { |         { | ||||||
|             size_t actual = hash.writeTo(buffer, sizeof(buffer)); |             size_t actual = hash.printTo(buffer, sizeof(buffer)); | ||||||
|             Assert::AreEqual(expected, actual); |             Assert::AreEqual(expected, actual); | ||||||
|         } |         } | ||||||
|     }; |     }; | ||||||
|   | |||||||
| @@ -86,7 +86,7 @@ namespace JsonGeneratorTests | |||||||
|         template<typename T> |         template<typename T> | ||||||
|         void write(T value) |         void write(T value) | ||||||
|         { |         { | ||||||
|             returnValue = JsonValue(value).writeTo(*sb); |             returnValue = JsonValue(value).printTo(*sb); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         void assertResultIs(const char* expected) |         void assertResultIs(const char* expected) | ||||||
|   | |||||||
							
								
								
									
										30
									
								
								examples/JsonGeneratorExample/JsonGeneratorExample.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								examples/JsonGeneratorExample/JsonGeneratorExample.ino
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | /* | ||||||
|  |  * Arduino JSON library | ||||||
|  |  * Benoit Blanchon 2014 - MIT License | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include <JsonGenerator.h> | ||||||
|  |  | ||||||
|  | void setup() | ||||||
|  | { | ||||||
|  |     Serial.begin(9600); | ||||||
|  |  | ||||||
|  |     JsonHashTable<3> h;     | ||||||
|  |  | ||||||
|  |     h.add("pi", 3.14); | ||||||
|  |  | ||||||
|  |     JsonArray<3> a; | ||||||
|  |     a.add(1.0); | ||||||
|  |     a.add(2.0); | ||||||
|  |     a.add(3.0); | ||||||
|  |     h.add("list", a); | ||||||
|  |  | ||||||
|  |     h.add("hellow", "world"); | ||||||
|  |  | ||||||
|  |     Serial.print(h); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void loop() | ||||||
|  | { | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user