From 1e02fabdeccbb4490233200b33b6665e8c7cf1b1 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 6 Nov 2014 13:52:33 +0100 Subject: [PATCH] Simplified JsonWriter and PrettyJsonWriter --- include/ArduinoJson/Internals/JsonWriter.hpp | 19 ++++++++------- .../Internals/PrettyJsonWriter.hpp | 24 ++++++++++--------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/include/ArduinoJson/Internals/JsonWriter.hpp b/include/ArduinoJson/Internals/JsonWriter.hpp index 46da6b51..4bfa64bf 100644 --- a/include/ArduinoJson/Internals/JsonWriter.hpp +++ b/include/ArduinoJson/Internals/JsonWriter.hpp @@ -27,23 +27,26 @@ class JsonWriter { // number of bytes written. size_t bytesWritten() { return _length; } - void beginArray() { _length += _sink->write('['); } - void endArray() { _length += _sink->write(']'); } - void writeEmptyArray() { _length += _sink->print("[]"); } + void beginArray() { write('['); } + void endArray() { write(']'); } + void writeEmptyArray() { write("[]"); } - void beginObject() { _length += _sink->write('{'); } - void endObject() { _length += _sink->write('}'); } - void writeEmptyObject() { _length += _sink->print("{}"); } + void beginObject() { write('{'); } + void endObject() { write('}'); } + void writeEmptyObject() { write("{}"); } void writeString(const char *value); void writeInteger(long value); void writeBoolean(bool value); void writeDouble(double value, uint8_t decimals); - void writeColon() { _length += _sink->write(':'); } - void writeComma() { _length += _sink->write(','); } + void writeColon() { write(':'); } + void writeComma() { write(','); } protected: + void write(char c) { _length += _sink->write(c); } + void write(const char *s) { _length += _sink->print(s); } + Print *_sink; size_t _length; }; diff --git a/include/ArduinoJson/Internals/PrettyJsonWriter.hpp b/include/ArduinoJson/Internals/PrettyJsonWriter.hpp index 43d62b2b..bc574f69 100644 --- a/include/ArduinoJson/Internals/PrettyJsonWriter.hpp +++ b/include/ArduinoJson/Internals/PrettyJsonWriter.hpp @@ -18,44 +18,46 @@ class PrettyJsonWriter : public JsonWriter { : JsonWriter(sink), _indenter(sink) {} void beginArray() { - _length += _sink->write('['); + JsonWriter::beginArray(); indent(); } void endArray() { unindent(); - _length += _sink->write(']'); + JsonWriter::endArray(); } - void writeColon() { _length += _sink->print(": "); } + void writeColon() { write(": "); } void writeComma() { - _length += _sink->write(','); - _length += _indenter->println(); + JsonWriter::writeComma(); + newline(); } void beginObject() { - _length += _sink->write('{'); + JsonWriter::beginObject(); indent(); } void endObject() { unindent(); - _length += _sink->write('}'); + JsonWriter::endObject(); } private: - IndentedPrint *_indenter; - void indent() { _indenter->indent(); - _length += _indenter->println(); + newline(); } void unindent() { - _length += _indenter->println(); + newline(); _indenter->unindent(); } + + void newline() { _length += _indenter->println(); } + + IndentedPrint *_indenter; }; } }