From e8831c28d9904801bd2b485a4f20c6a5ba5261bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Tue, 1 Jul 2014 14:08:15 +0200 Subject: [PATCH] Added JsonGenerator example for Arduino --- JsonGenerator.cpp | 9 ++++++ JsonGenerator.h | 7 +++++ JsonGenerator/JsonArray.h | 6 ++-- JsonGenerator/JsonHashTable.h | 8 ++--- JsonGenerator/JsonObjectBase.h | 6 ++-- JsonGenerator/JsonValue.cpp | 15 +++++----- JsonGenerator/JsonValue.h | 18 +++++------ JsonGenerator/Print.h | 13 ++++++-- JsonGenerator/Printable.h | 10 ++++++- JsonGenerator/StringBuilder.cpp | 2 +- JsonGenerator/StringBuilder.h | 2 +- JsonGeneratorTests/JsonArrayTests.cpp | 4 +-- JsonGeneratorTests/JsonHashTableTests.cpp | 4 +-- JsonGeneratorTests/JsonValueTests.cpp | 2 +- .../JsonGeneratorExample.ino | 30 +++++++++++++++++++ 15 files changed, 99 insertions(+), 37 deletions(-) create mode 100644 JsonGenerator.cpp create mode 100644 JsonGenerator.h create mode 100644 examples/JsonGeneratorExample/JsonGeneratorExample.ino diff --git a/JsonGenerator.cpp b/JsonGenerator.cpp new file mode 100644 index 00000000..dff6ea59 --- /dev/null +++ b/JsonGenerator.cpp @@ -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" \ No newline at end of file diff --git a/JsonGenerator.h b/JsonGenerator.h new file mode 100644 index 00000000..bd0ed9d7 --- /dev/null +++ b/JsonGenerator.h @@ -0,0 +1,7 @@ +/* +* malloc-free JSON parser for Arduino +* Benoit Blanchon 2014 - MIT License +*/ + +#include "JsonGenerator/JsonArray.h" +#include "JsonGenerator/JsonHashTable.h" \ No newline at end of file diff --git a/JsonGenerator/JsonArray.h b/JsonGenerator/JsonArray.h index cf1bea97..213976b1 100644 --- a/JsonGenerator/JsonArray.h +++ b/JsonGenerator/JsonArray.h @@ -31,13 +31,13 @@ public: itemCount++; } - using JsonObjectBase::writeTo; + using JsonObjectBase::printTo; private: JsonValue items[N]; int itemCount; - virtual size_t writeTo(Print& p) const + virtual size_t printTo(Print& p) const { size_t n = 0; @@ -50,7 +50,7 @@ private: n += p.write(","); } - n += items[i].writeTo(p); + n += items[i].printTo(p); } n += p.write("]"); diff --git a/JsonGenerator/JsonHashTable.h b/JsonGenerator/JsonHashTable.h index f2894d73..26222067 100644 --- a/JsonGenerator/JsonHashTable.h +++ b/JsonGenerator/JsonHashTable.h @@ -32,7 +32,7 @@ public: itemCount++; } - using JsonObjectBase::writeTo; + using JsonObjectBase::printTo; private: @@ -45,7 +45,7 @@ private: KeyValuePair items[N]; int itemCount; - virtual size_t writeTo(Print& p) const + virtual size_t printTo(Print& p) const { size_t n = 0; @@ -60,9 +60,9 @@ private: n += p.write(','); } - n += key.writeTo(p); + n += key.printTo(p); n += p.write(':'); - n += items[i].value.writeTo(p); + n += items[i].value.printTo(p); } n += p.write('}'); diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index 7daad792..25c41fc5 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -13,12 +13,12 @@ class JsonObjectBase : public Printable { public: - size_t writeTo(char* buffer, size_t bufferSize) + size_t printTo(char* buffer, size_t 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; }; diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index 5a23e9b0..24589d18 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/JsonGenerator/JsonValue.cpp @@ -4,35 +4,34 @@ */ #include "JsonValue.h" -#include "JsonObjectBase.h" #include #include -size_t JsonValue::writeBooleanTo(Print& p) const +size_t JsonValue::printBooleanTo(Print& p) const { return p.write(content.boolean ? "true" : "false"); } -size_t JsonValue::writeNumberTo(Print& p) const +size_t JsonValue::printNumberTo(Print& p) const { char tmp[16]; - _snprintf(tmp, sizeof(tmp), "%lg", content.number); + sprintf(tmp, "%lg", content.number); return p.write(tmp); } -size_t JsonValue::writeObjectTo(Print& p) const +size_t JsonValue::printObjectTo(Print& p) const { if (content.object) - return ((JsonObjectBase*)content.object)->writeTo(p); + return ((Printable*) content.object)->printTo(p); else 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) { diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index 5fb506b6..45dff0b8 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -17,30 +17,30 @@ public: } JsonValue(const char* value) - : implementation(&JsonValue::writeStringTo) + : implementation(&JsonValue::printStringTo) { content.string = value; } JsonValue(double value) - : implementation(&JsonValue::writeNumberTo) + : implementation(&JsonValue::printNumberTo) { content.number = value; } JsonValue(bool value) - : implementation(&JsonValue::writeBooleanTo) + : implementation(&JsonValue::printBooleanTo) { content.boolean = value; } JsonValue(Printable& value) - : implementation(&JsonValue::writeObjectTo) + : implementation(&JsonValue::printObjectTo) { content.object = &value; } - virtual size_t writeTo(Print& p) const + virtual size_t printTo(Print& p) const { // handmade polymorphism return (this->*implementation)(p); @@ -60,8 +60,8 @@ private: size_t(JsonValue::*implementation)(Print& p)const; - size_t writeBooleanTo(Print& p) const; - size_t writeNumberTo(Print& p) const; - size_t writeObjectTo(Print& p) const; - size_t writeStringTo(Print& p) const; + size_t printBooleanTo(Print& p) const; + size_t printNumberTo(Print& p) const; + size_t printObjectTo(Print& p) const; + size_t printStringTo(Print& p) const; }; \ No newline at end of file diff --git a/JsonGenerator/Print.h b/JsonGenerator/Print.h index 91feaa73..77dd0664 100644 --- a/JsonGenerator/Print.h +++ b/JsonGenerator/Print.h @@ -5,12 +5,16 @@ #pragma once +#ifndef ARDUINO + +typedef unsigned char uint8_t; + class Print { public: - virtual size_t write(char c) = 0; - + virtual size_t write(uint8_t c) = 0; + size_t write(const char* s) { size_t n = 0; @@ -22,3 +26,8 @@ public: } }; +#else + +#include + +#endif diff --git a/JsonGenerator/Printable.h b/JsonGenerator/Printable.h index 136aac7d..c953b1e5 100644 --- a/JsonGenerator/Printable.h +++ b/JsonGenerator/Printable.h @@ -5,12 +5,20 @@ #pragma once +#ifndef ARDUINO + class Print; class Printable { public: - virtual size_t writeTo(Print& p) const = 0; + virtual size_t printTo(Print& p) const = 0; }; +#else + +#include + +#endif + diff --git a/JsonGenerator/StringBuilder.cpp b/JsonGenerator/StringBuilder.cpp index 77b836fe..93e7e934 100644 --- a/JsonGenerator/StringBuilder.cpp +++ b/JsonGenerator/StringBuilder.cpp @@ -5,7 +5,7 @@ #include "StringBuilder.h" -size_t StringBuilder::write(char c) +size_t StringBuilder::write(uint8_t c) { if (length >= capacity) return 0; diff --git a/JsonGenerator/StringBuilder.h b/JsonGenerator/StringBuilder.h index 2e64f57a..42cd3195 100644 --- a/JsonGenerator/StringBuilder.h +++ b/JsonGenerator/StringBuilder.h @@ -16,7 +16,7 @@ public: buffer[0] = 0; } - virtual size_t write(char c); + virtual size_t write(uint8_t c); private: char* buffer; diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index f850ba56..c8964db9 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -162,13 +162,13 @@ namespace JsonGeneratorTests void jsonIs(const char* expected) { - arr.writeTo(buffer, sizeof(buffer)); + arr.printTo(buffer, sizeof(buffer)); Assert::AreEqual(expected, buffer); } 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); } }; diff --git a/JsonGeneratorTests/JsonHashTableTests.cpp b/JsonGeneratorTests/JsonHashTableTests.cpp index 71b0250c..f6c8b9c8 100644 --- a/JsonGeneratorTests/JsonHashTableTests.cpp +++ b/JsonGeneratorTests/JsonHashTableTests.cpp @@ -111,13 +111,13 @@ namespace JsonGeneratorTests void jsonIs(const char* expected) { - hash.writeTo(buffer, sizeof(buffer)); + hash.printTo(buffer, sizeof(buffer)); Assert::AreEqual(expected, buffer); } 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); } }; diff --git a/JsonGeneratorTests/JsonValueTests.cpp b/JsonGeneratorTests/JsonValueTests.cpp index 38227b80..a5ed046f 100644 --- a/JsonGeneratorTests/JsonValueTests.cpp +++ b/JsonGeneratorTests/JsonValueTests.cpp @@ -86,7 +86,7 @@ namespace JsonGeneratorTests template void write(T value) { - returnValue = JsonValue(value).writeTo(*sb); + returnValue = JsonValue(value).printTo(*sb); } void assertResultIs(const char* expected) diff --git a/examples/JsonGeneratorExample/JsonGeneratorExample.ino b/examples/JsonGeneratorExample/JsonGeneratorExample.ino new file mode 100644 index 00000000..08f81547 --- /dev/null +++ b/examples/JsonGeneratorExample/JsonGeneratorExample.ino @@ -0,0 +1,30 @@ +/* + * Arduino JSON library + * Benoit Blanchon 2014 - MIT License + */ + +#include + +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() +{ + +}