Removed virtuals from JsonWriter hierarchy

This commit is contained in:
Benoit Blanchon
2014-11-03 12:51:24 +01:00
parent f26f4263ea
commit 2a60c96baf
6 changed files with 28 additions and 31 deletions

View File

@ -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(','); }
};
}
}

View File

@ -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;

View File

@ -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('}');
}

View File

@ -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 <typename T>
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<const T *>(this); }
const T &downcast() const { return *static_cast<const T *>(this); }
};
}

View File

@ -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 <typename T>
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>(CompactJsonWriter &) const;
template void JsonValue::writeTo<PrettyJsonWriter>(PrettyJsonWriter &) const;

View File

@ -30,7 +30,8 @@ class Issue10 : public testing::Test {
persons[1] = employee;
}
void checkJsonString(JsonPrintable &p) {
template <typename T>
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_ARRAY_SIZE(2)+2*JSON_OBJECT_SIZE(2)> json;
StaticJsonBuffer<JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(2)> json;
Person persons[2];
};