forked from bblanchon/ArduinoJson
Reduced size by 300 bytes by reusing the JsonPrettyPrint from v3
This commit is contained in:
85
src/Internals/Prettyfier.cpp
Normal file
85
src/Internals/Prettyfier.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include "../../include/ArduinoJson/Internals/Prettyfier.hpp"
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
size_t Prettyfier::write(uint8_t c) {
|
||||
size_t n = _inString ? handleStringChar(c) : handleMarkupChar(c);
|
||||
_previousChar = c;
|
||||
return n;
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleStringChar(uint8_t c) {
|
||||
bool isQuote = c == '"' && _previousChar != '\\';
|
||||
|
||||
if (isQuote) _inString = false;
|
||||
|
||||
return _sink.write(c);
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleMarkupChar(uint8_t c) {
|
||||
switch (c) {
|
||||
case '{':
|
||||
case '[':
|
||||
return handleBlockOpen(c);
|
||||
|
||||
case '}':
|
||||
case ']':
|
||||
return handleBlockClose(c);
|
||||
|
||||
case ':':
|
||||
return handleColumn();
|
||||
|
||||
case ',':
|
||||
return handleComma();
|
||||
|
||||
case '"':
|
||||
return handleQuoteOpen();
|
||||
|
||||
default:
|
||||
return handleNormalChar(c);
|
||||
}
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleBlockOpen(uint8_t c) {
|
||||
return indentIfNeeded() + _sink.write(c);
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleBlockClose(uint8_t c) {
|
||||
return unindentIfNeeded() + _sink.write(c);
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleColumn() {
|
||||
return _sink.write(':') + _sink.write(' ');
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleComma() {
|
||||
return _sink.write(',') + _sink.println();
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleQuoteOpen() {
|
||||
_inString = true;
|
||||
return indentIfNeeded() + _sink.write('"');
|
||||
}
|
||||
|
||||
inline size_t Prettyfier::handleNormalChar(uint8_t c) {
|
||||
return indentIfNeeded() + _sink.write(c);
|
||||
}
|
||||
|
||||
size_t Prettyfier::indentIfNeeded() {
|
||||
if (!inEmptyBlock()) return 0;
|
||||
|
||||
_sink.indent();
|
||||
return _sink.println();
|
||||
}
|
||||
|
||||
size_t Prettyfier::unindentIfNeeded() {
|
||||
if (inEmptyBlock()) return 0;
|
||||
|
||||
_sink.unindent();
|
||||
return _sink.println();
|
||||
}
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/ArduinoJson/JsonArray.hpp"
|
||||
|
||||
#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
|
||||
#include "../include/ArduinoJson/JsonBuffer.hpp"
|
||||
#include "../include/ArduinoJson/JsonObject.hpp"
|
||||
|
||||
@ -44,8 +43,7 @@ JsonObject &JsonArray::createNestedObject() {
|
||||
return object;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void JsonArray::writeTo(T &writer) const {
|
||||
void JsonArray::writeTo(JsonWriter &writer) const {
|
||||
node_type *child = _firstNode;
|
||||
|
||||
if (child) {
|
||||
@ -65,6 +63,3 @@ void JsonArray::writeTo(T &writer) const {
|
||||
writer.writeEmptyArray();
|
||||
}
|
||||
}
|
||||
|
||||
template void JsonArray::writeTo(JsonWriter &) const;
|
||||
template void JsonArray::writeTo(PrettyJsonWriter &) const;
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include <string.h> // for strcmp
|
||||
|
||||
#include "../include/ArduinoJson/Internals/PlacementNew.hpp"
|
||||
#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
|
||||
#include "../include/ArduinoJson/Internals/StringBuilder.hpp"
|
||||
#include "../include/ArduinoJson/JsonArray.hpp"
|
||||
#include "../include/ArduinoJson/JsonBuffer.hpp"
|
||||
@ -69,8 +68,7 @@ JsonObject::node_type *JsonObject::getOrCreateNodeAt(const char *key) {
|
||||
return newNode;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void JsonObject::writeTo(T &writer) const {
|
||||
void JsonObject::writeTo(JsonWriter &writer) const {
|
||||
node_type *node = _firstNode;
|
||||
|
||||
if (node) {
|
||||
@ -92,6 +90,3 @@ void JsonObject::writeTo(T &writer) const {
|
||||
writer.writeEmptyObject();
|
||||
}
|
||||
}
|
||||
|
||||
template void JsonObject::writeTo(JsonWriter &writer) const;
|
||||
template void JsonObject::writeTo(PrettyJsonWriter &writer) const;
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/ArduinoJson/JsonVariant.hpp"
|
||||
|
||||
#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
|
||||
#include "../include/ArduinoJson/JsonArray.hpp"
|
||||
#include "../include/ArduinoJson/JsonObject.hpp"
|
||||
|
||||
@ -91,8 +90,7 @@ JsonVariant &JsonVariant::operator[](const char *key) {
|
||||
return _content.asObject->operator[](key);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void JsonVariant::writeTo(T &writer) const {
|
||||
void JsonVariant::writeTo(JsonWriter &writer) const {
|
||||
switch (_type) {
|
||||
case JSON_ARRAY:
|
||||
_content.asArray->writeTo(writer);
|
||||
@ -120,6 +118,3 @@ void JsonVariant::writeTo(T &writer) const {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template void JsonVariant::writeTo(JsonWriter &) const;
|
||||
template void JsonVariant::writeTo(PrettyJsonWriter &) const;
|
||||
|
Reference in New Issue
Block a user