Reduced size by 300 bytes by reusing the JsonPrettyPrint from v3

This commit is contained in:
Benoit Blanchon
2014-11-08 19:40:07 +01:00
parent d8dbfe6e6d
commit f6133f2d36
10 changed files with 138 additions and 92 deletions

View File

@ -7,7 +7,8 @@
#pragma once
#include "IndentedPrint.hpp"
#include "PrettyJsonWriter.hpp"
#include "JsonWriter.hpp"
#include "Prettyfier.hpp"
#include "StringBuilder.hpp"
namespace ArduinoJson {
@ -32,9 +33,8 @@ class JsonPrintable {
}
size_t prettyPrintTo(IndentedPrint &print) const {
PrettyJsonWriter writer(print);
downcast().writeTo(writer);
return writer.bytesWritten();
Prettyfier p(print);
return printTo(p);
}
size_t prettyPrintTo(char *buffer, size_t bufferSize) const {

View File

@ -1,64 +0,0 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include "JsonWriter.hpp"
#include "IndentedPrint.hpp"
namespace ArduinoJson {
namespace Internals {
// An indented version of JsonWriter.
class PrettyJsonWriter : public JsonWriter {
public:
explicit PrettyJsonWriter(IndentedPrint &sink)
: JsonWriter(sink), _indenter(sink) {}
void beginArray() {
JsonWriter::beginArray();
indent();
}
void endArray() {
unindent();
JsonWriter::endArray();
}
void writeColon() { write(": "); }
void writeComma() {
JsonWriter::writeComma();
newline();
}
void beginObject() {
JsonWriter::beginObject();
indent();
}
void endObject() {
unindent();
JsonWriter::endObject();
}
private:
void indent() {
_indenter.indent();
newline();
}
void unindent() {
newline();
_indenter.unindent();
}
void newline() { _length += _indenter.println(); }
IndentedPrint &_indenter;
};
}
}

View File

@ -0,0 +1,43 @@
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#pragma once
#include "IndentedPrint.hpp"
namespace ArduinoJson {
namespace Internals {
// Converts a compact JSON string into an indented one.
class Prettyfier : public Print {
public:
Prettyfier(IndentedPrint& p) : _sink(p) {
_previousChar = 0;
_inString = false;
}
virtual size_t write(uint8_t);
private:
uint8_t _previousChar;
IndentedPrint& _sink;
bool _inString;
bool inEmptyBlock() { return _previousChar == '{' || _previousChar == '['; }
size_t handleStringChar(uint8_t);
size_t handleMarkupChar(uint8_t);
size_t handleBlockClose(uint8_t);
size_t handleBlockOpen(uint8_t);
size_t handleColumn();
size_t handleComma();
size_t handleQuoteOpen();
size_t handleNormalChar(uint8_t);
size_t indentIfNeeded();
size_t unindentIfNeeded();
};
}
}

View File

@ -75,8 +75,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
static JsonArray &invalid() { return _invalid; }
// Serialize the array to the specified JsonWriter.
template <typename T>
void writeTo(T &writer) const;
void writeTo(Internals::JsonWriter &writer) const;
private:
// Create an empty JsonArray attached to the specified JsonBuffer.

View File

@ -90,8 +90,7 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
static JsonObject &invalid() { return _invalid; }
// Serialize the object to the specified JsonWriter
template <typename T>
void writeTo(T &writer) const;
void writeTo(Internals::JsonWriter &writer) const;
private:
// Create an empty JsonArray attached to the specified JsonBuffer.

View File

@ -146,8 +146,7 @@ class JsonVariant : public Internals::JsonPrintable<JsonVariant> {
static JsonVariant &invalid() { return _invalid; }
// Serialize the variant to a JsonWriter
template <typename T>
void writeTo(T &writer) const;
void writeTo(Internals::JsonWriter &writer) const;
// Mimics an array or an object.
// Returns the size of the array or object if the variant has that type.