Browse Source

Reduced size by 300 bytes by reusing the JsonPrettyPrint from v3

Benoit Blanchon 11 years ago
parent
commit
f6133f2d36

+ 4 - 4
include/ArduinoJson/Internals/JsonPrintable.hpp

@@ -7,7 +7,8 @@
 #pragma once
 #pragma once
 
 
 #include "IndentedPrint.hpp"
 #include "IndentedPrint.hpp"
-#include "PrettyJsonWriter.hpp"
+#include "JsonWriter.hpp"
+#include "Prettyfier.hpp"
 #include "StringBuilder.hpp"
 #include "StringBuilder.hpp"
 
 
 namespace ArduinoJson {
 namespace ArduinoJson {
@@ -32,9 +33,8 @@ class JsonPrintable {
   }
   }
 
 
   size_t prettyPrintTo(IndentedPrint &print) const {
   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 {
   size_t prettyPrintTo(char *buffer, size_t bufferSize) const {

+ 0 - 64
include/ArduinoJson/Internals/PrettyJsonWriter.hpp

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

+ 43 - 0
include/ArduinoJson/Internals/Prettyfier.hpp

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

+ 1 - 2
include/ArduinoJson/JsonArray.hpp

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

+ 1 - 2
include/ArduinoJson/JsonObject.hpp

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

+ 1 - 2
include/ArduinoJson/JsonVariant.hpp

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

+ 85 - 0
src/Internals/Prettyfier.cpp

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

+ 1 - 6
src/JsonArray.cpp

@@ -6,7 +6,6 @@
 
 
 #include "../include/ArduinoJson/JsonArray.hpp"
 #include "../include/ArduinoJson/JsonArray.hpp"
 
 
-#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
 #include "../include/ArduinoJson/JsonBuffer.hpp"
 #include "../include/ArduinoJson/JsonBuffer.hpp"
 #include "../include/ArduinoJson/JsonObject.hpp"
 #include "../include/ArduinoJson/JsonObject.hpp"
 
 
@@ -44,8 +43,7 @@ JsonObject &JsonArray::createNestedObject() {
   return object;
   return object;
 }
 }
 
 
-template <typename T>
-void JsonArray::writeTo(T &writer) const {
+void JsonArray::writeTo(JsonWriter &writer) const {
   node_type *child = _firstNode;
   node_type *child = _firstNode;
 
 
   if (child) {
   if (child) {
@@ -65,6 +63,3 @@ void JsonArray::writeTo(T &writer) const {
     writer.writeEmptyArray();
     writer.writeEmptyArray();
   }
   }
 }
 }
-
-template void JsonArray::writeTo(JsonWriter &) const;
-template void JsonArray::writeTo(PrettyJsonWriter &) const;

+ 1 - 6
src/JsonObject.cpp

@@ -9,7 +9,6 @@
 #include <string.h>  // for strcmp
 #include <string.h>  // for strcmp
 
 
 #include "../include/ArduinoJson/Internals/PlacementNew.hpp"
 #include "../include/ArduinoJson/Internals/PlacementNew.hpp"
-#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
 #include "../include/ArduinoJson/Internals/StringBuilder.hpp"
 #include "../include/ArduinoJson/Internals/StringBuilder.hpp"
 #include "../include/ArduinoJson/JsonArray.hpp"
 #include "../include/ArduinoJson/JsonArray.hpp"
 #include "../include/ArduinoJson/JsonBuffer.hpp"
 #include "../include/ArduinoJson/JsonBuffer.hpp"
@@ -69,8 +68,7 @@ JsonObject::node_type *JsonObject::getOrCreateNodeAt(const char *key) {
   return newNode;
   return newNode;
 }
 }
 
 
-template <typename T>
-void JsonObject::writeTo(T &writer) const {
+void JsonObject::writeTo(JsonWriter &writer) const {
   node_type *node = _firstNode;
   node_type *node = _firstNode;
 
 
   if (node) {
   if (node) {
@@ -92,6 +90,3 @@ void JsonObject::writeTo(T &writer) const {
     writer.writeEmptyObject();
     writer.writeEmptyObject();
   }
   }
 }
 }
-
-template void JsonObject::writeTo(JsonWriter &writer) const;
-template void JsonObject::writeTo(PrettyJsonWriter &writer) const;

+ 1 - 6
src/JsonVariant.cpp

@@ -6,7 +6,6 @@
 
 
 #include "../include/ArduinoJson/JsonVariant.hpp"
 #include "../include/ArduinoJson/JsonVariant.hpp"
 
 
-#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
 #include "../include/ArduinoJson/JsonArray.hpp"
 #include "../include/ArduinoJson/JsonArray.hpp"
 #include "../include/ArduinoJson/JsonObject.hpp"
 #include "../include/ArduinoJson/JsonObject.hpp"
 
 
@@ -91,8 +90,7 @@ JsonVariant &JsonVariant::operator[](const char *key) {
   return _content.asObject->operator[](key);
   return _content.asObject->operator[](key);
 }
 }
 
 
-template <typename T>
-void JsonVariant::writeTo(T &writer) const {
+void JsonVariant::writeTo(JsonWriter &writer) const {
   switch (_type) {
   switch (_type) {
     case JSON_ARRAY:
     case JSON_ARRAY:
       _content.asArray->writeTo(writer);
       _content.asArray->writeTo(writer);
@@ -120,6 +118,3 @@ void JsonVariant::writeTo(T &writer) const {
       break;
       break;
   }
   }
 }
 }
-
-template void JsonVariant::writeTo(JsonWriter &) const;
-template void JsonVariant::writeTo(PrettyJsonWriter &) const;