diff --git a/include/ArduinoJson/Internals/CompactJsonWriter.hpp b/include/ArduinoJson/Internals/CompactJsonWriter.hpp index dcc1e7e7..bba6f567 100644 --- a/include/ArduinoJson/Internals/CompactJsonWriter.hpp +++ b/include/ArduinoJson/Internals/CompactJsonWriter.hpp @@ -15,14 +15,14 @@ class CompactJsonWriter : public JsonWriter { public: explicit CompactJsonWriter(Print *sink) : JsonWriter(sink) {} - virtual void beginArray() { _length += _sink->write('['); } - virtual void endArray() { _length += _sink->write(']'); } + void beginArray() { _length += _sink->write('['); } + void endArray() { _length += _sink->write(']'); } - virtual void beginObject() { _length += _sink->write('{'); } - virtual void endObject() { _length += _sink->write('}'); } + void beginObject() { _length += _sink->write('{'); } + void endObject() { _length += _sink->write('}'); } - virtual void writeColon() { _length += _sink->write(':'); } - virtual void writeComma() { _length += _sink->write(','); } + void writeColon() { _length += _sink->write(':'); } + void writeComma() { _length += _sink->write(','); } }; } } diff --git a/include/ArduinoJson/Internals/JsonWriter.hpp b/include/ArduinoJson/Internals/JsonWriter.hpp index 56002d2a..2c6b7d37 100644 --- a/include/ArduinoJson/Internals/JsonWriter.hpp +++ b/include/ArduinoJson/Internals/JsonWriter.hpp @@ -17,12 +17,8 @@ class JsonWriter { size_t bytesWritten() { return _length; } - virtual void beginArray() = 0; - virtual void endArray() = 0; void writeEmptyArray() { _length += _sink->print("[]"); } - virtual void beginObject() = 0; - virtual void endObject() = 0; void writeEmptyObject() { _length += _sink->print("{}"); } void writeString(const char *value); @@ -30,9 +26,6 @@ class JsonWriter { void writeBoolean(bool value); void writeDouble(double value, int decimals); - virtual void writeColon() = 0; - virtual void writeComma() = 0; - protected: Print *_sink; size_t _length; diff --git a/include/ArduinoJson/Internals/PrettyJsonWriter.hpp b/include/ArduinoJson/Internals/PrettyJsonWriter.hpp index ec2ea27a..43d62b2b 100644 --- a/include/ArduinoJson/Internals/PrettyJsonWriter.hpp +++ b/include/ArduinoJson/Internals/PrettyJsonWriter.hpp @@ -17,29 +17,29 @@ class PrettyJsonWriter : public JsonWriter { explicit PrettyJsonWriter(IndentedPrint *sink) : JsonWriter(sink), _indenter(sink) {} - virtual void beginArray() { + void beginArray() { _length += _sink->write('['); indent(); } - virtual void endArray() { + void endArray() { unindent(); _length += _sink->write(']'); } - virtual void writeColon() { _length += _sink->print(": "); } + void writeColon() { _length += _sink->print(": "); } - virtual void writeComma() { + void writeComma() { _length += _sink->write(','); _length += _indenter->println(); } - virtual void beginObject() { + void beginObject() { _length += _sink->write('{'); indent(); } - virtual void endObject() { + void endObject() { unindent(); _length += _sink->write('}'); } diff --git a/include/ArduinoJson/JsonPrintable.hpp b/include/ArduinoJson/JsonPrintable.hpp index 817670bb..f1709f67 100644 --- a/include/ArduinoJson/JsonPrintable.hpp +++ b/include/ArduinoJson/JsonPrintable.hpp @@ -9,18 +9,16 @@ #include "Arduino/Printable.hpp" #include "Internals/StringBuilder.hpp" #include "Internals/IndentedPrint.hpp" +#include "Internals/CompactJsonWriter.hpp" +#include "Internals/PrettyJsonWriter.hpp" namespace ArduinoJson { -namespace Internals { -class IndentedPrint; -} - template class JsonPrintable : public Printable { public: size_t printTo(Print &print) const { - CompactJsonWriter writer(&p); + Internals::CompactJsonWriter writer(&print); downcast().writeTo(writer); return writer.bytesWritten(); } @@ -30,7 +28,7 @@ class JsonPrintable : public Printable { } size_t prettyPrintTo(Internals::IndentedPrint &print) const { - PrettyJsonWriter writer(&p); + Internals::PrettyJsonWriter writer(&print); downcast().writeTo(writer); return writer.bytesWritten(); } @@ -45,6 +43,6 @@ class JsonPrintable : public Printable { } private: - const T &downcast() { return *static_cast(this); } + const T &downcast() const { return *static_cast(this); } }; } diff --git a/src/JsonValue.cpp b/src/JsonValue.cpp index c0e8a890..cbfd2d03 100644 --- a/src/JsonValue.cpp +++ b/src/JsonValue.cpp @@ -7,7 +7,8 @@ #include "ArduinoJson/JsonValue.hpp" #include "ArduinoJson/JsonArray.hpp" #include "ArduinoJson/JsonObject.hpp" -#include "ArduinoJson/Internals/JsonWriter.hpp" +#include "ArduinoJson/Internals/CompactJsonWriter.hpp" +#include "ArduinoJson/Internals/PrettyJsonWriter.hpp" using namespace ArduinoJson; using namespace ArduinoJson::Internals; @@ -74,7 +75,8 @@ void JsonValue::set(JsonObject &object) { _content.asObject = &object; } -void JsonValue::writeTo(JsonWriter &writer) const { +template +void JsonValue::writeTo(T &writer) const { switch (_type) { case JSON_ARRAY: _content.asArray->writeTo(writer); @@ -101,3 +103,6 @@ void JsonValue::writeTo(JsonWriter &writer) const { break; } } + +template void JsonValue::writeTo(CompactJsonWriter &) const; +template void JsonValue::writeTo(PrettyJsonWriter &) const; diff --git a/test/Issue10.cpp b/test/Issue10.cpp index 0dc7da78..b1a0c481 100644 --- a/test/Issue10.cpp +++ b/test/Issue10.cpp @@ -30,7 +30,8 @@ class Issue10 : public testing::Test { persons[1] = employee; } - void checkJsonString(JsonPrintable &p) { + template + void checkJsonString(const T &p) { char buffer[256]; p.printTo(buffer, sizeof(buffer)); @@ -38,7 +39,7 @@ class Issue10 : public testing::Test { buffer); } - StaticJsonBuffer json; + StaticJsonBuffer json; Person persons[2]; }; @@ -51,7 +52,7 @@ TEST_F(Issue10, PopulateArrayByAddingAnObject) { object["id"] = persons[i].id; object["name"] = persons[i].name; - array.add(object); + array.add(object); } checkJsonString(array);