mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-22 23:07:29 +02:00
Added JsonContainer::prettyPrintTo()
This commit is contained in:
1
srcs/Internals/CompactJsonWriter.cpp
Normal file
1
srcs/Internals/CompactJsonWriter.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "CompactJsonWriter.h"
|
43
srcs/Internals/CompactJsonWriter.h
Normal file
43
srcs/Internals/CompactJsonWriter.h
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Internals/JsonWriter.h"
|
||||
|
||||
class CompactJsonWriter : public JsonWriter
|
||||
{
|
||||
public:
|
||||
explicit CompactJsonWriter(Print& sink)
|
||||
: JsonWriter(sink)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void beginArray()
|
||||
{
|
||||
_length += _sink.write('[');
|
||||
}
|
||||
|
||||
virtual void endArray()
|
||||
{
|
||||
_length += _sink.write(']');
|
||||
}
|
||||
|
||||
virtual void writeColon()
|
||||
{
|
||||
_length += _sink.write(':');
|
||||
}
|
||||
|
||||
virtual void writeComma()
|
||||
{
|
||||
_length += _sink.write(',');
|
||||
}
|
||||
|
||||
virtual void beginObject()
|
||||
{
|
||||
_length += _sink.write('{');
|
||||
}
|
||||
|
||||
virtual void endObject()
|
||||
{
|
||||
_length += _sink.write('}');
|
||||
}
|
||||
};
|
||||
|
45
srcs/Internals/IndentedPrint.cpp
Normal file
45
srcs/Internals/IndentedPrint.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "IndentedPrint.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
void IndentedPrint::indent()
|
||||
{
|
||||
if (level < MAX_LEVEL)
|
||||
level++;
|
||||
}
|
||||
|
||||
void IndentedPrint::unindent()
|
||||
{
|
||||
if (level > 0)
|
||||
level--;
|
||||
}
|
||||
|
||||
void IndentedPrint::setTabSize(uint8_t n)
|
||||
{
|
||||
if (n < MAX_TAB_SIZE)
|
||||
tabSize = n;
|
||||
}
|
||||
|
||||
size_t IndentedPrint::write(uint8_t c)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
if (isNewLine)
|
||||
n += writeTabs();
|
||||
|
||||
n += sink.write(c);
|
||||
|
||||
isNewLine = c == '\n';
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
inline size_t IndentedPrint::writeTabs()
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
for (int i = 0; i < level*tabSize; i++)
|
||||
n += sink.write(' ');
|
||||
|
||||
return n;
|
||||
}
|
53
srcs/Internals/IndentedPrint.h
Normal file
53
srcs/Internals/IndentedPrint.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Arduino/Print.h"
|
||||
|
||||
namespace ArduinoJson
|
||||
{
|
||||
namespace Generator
|
||||
{
|
||||
// Decorator on top of Print to allow indented output.
|
||||
// This class is used by JsonPrintable::prettyPrintTo() but can also be used
|
||||
// for your own purpose, like logging.
|
||||
class IndentedPrint : public Print
|
||||
{
|
||||
public:
|
||||
|
||||
IndentedPrint(Print& p)
|
||||
: sink(p)
|
||||
{
|
||||
level = 0;
|
||||
tabSize = 2;
|
||||
isNewLine = true;
|
||||
}
|
||||
|
||||
virtual size_t write(uint8_t);
|
||||
|
||||
// Adds one level of indentation
|
||||
void indent();
|
||||
|
||||
// Removes one level of indentation
|
||||
void unindent();
|
||||
|
||||
// Set the number of space printed for each level of indentation
|
||||
void setTabSize(uint8_t n);
|
||||
|
||||
private:
|
||||
Print& sink;
|
||||
uint8_t level : 4;
|
||||
uint8_t tabSize : 3;
|
||||
bool isNewLine : 1;
|
||||
|
||||
size_t writeTabs();
|
||||
|
||||
static const int MAX_LEVEL = 15; // because it's only 4 bits
|
||||
static const int MAX_TAB_SIZE = 7; // because it's only 3 bits
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -17,15 +17,15 @@ void JsonNode::writeTo(JsonWriter& writer)
|
||||
break;
|
||||
|
||||
case JSON_STRING:
|
||||
writer.writeValue(content.asString);
|
||||
writer.writeString(content.asString);
|
||||
break;
|
||||
|
||||
case JSON_INTEGER:
|
||||
writer.writeValue(content.asInteger);
|
||||
writer.writeInteger(content.asInteger);
|
||||
break;
|
||||
|
||||
case JSON_BOOLEAN:
|
||||
writer.writeValue(content.asBoolean);
|
||||
writer.writeBoolean(content.asBoolean);
|
||||
break;
|
||||
|
||||
case JSON_PROXY:
|
||||
@ -33,40 +33,61 @@ void JsonNode::writeTo(JsonWriter& writer)
|
||||
break;
|
||||
|
||||
default: // >= JSON_DOUBLE_0_DECIMALS
|
||||
writer.writeValue(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
||||
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void JsonNode::writeArrayTo(JsonWriter& writer)
|
||||
{
|
||||
writer.beginArray();
|
||||
|
||||
JsonNode* child = content.asContainer.child;
|
||||
|
||||
while(child)
|
||||
|
||||
if (child)
|
||||
{
|
||||
child->writeTo(writer);
|
||||
writer.beginArray();
|
||||
|
||||
child = child->next;
|
||||
while (true)
|
||||
{
|
||||
child->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.writeEmptyArray();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
}
|
||||
|
||||
void JsonNode::writeObjectTo(JsonWriter& writer)
|
||||
{
|
||||
writer.beginObject();
|
||||
|
||||
JsonNode* child = content.asContainer.child;
|
||||
|
||||
while (child)
|
||||
if (child)
|
||||
{
|
||||
writer.writeKey(child->content.asKey.key);
|
||||
child->content.asKey.value->writeTo(writer);
|
||||
writer.beginObject();
|
||||
|
||||
child = child->next;
|
||||
while (true)
|
||||
{
|
||||
writer.writeString(child->content.asKey.key);
|
||||
writer.writeColon();
|
||||
child->content.asKey.value->writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.writeEmptyObject();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
}
|
@ -3,64 +3,23 @@
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
void JsonWriter::beginArray()
|
||||
void JsonWriter::writeString(char const* value)
|
||||
{
|
||||
writeCommaIfNeeded();
|
||||
_length += _sink.write('[');
|
||||
_isCommaNeeded = false;
|
||||
}
|
||||
|
||||
void JsonWriter::endArray()
|
||||
{
|
||||
_length += _sink.write(']');
|
||||
_isCommaNeeded = true;
|
||||
}
|
||||
|
||||
void JsonWriter::beginObject()
|
||||
{
|
||||
writeCommaIfNeeded();
|
||||
_length += _sink.write('{');
|
||||
_isCommaNeeded = false;
|
||||
}
|
||||
|
||||
void JsonWriter::endObject()
|
||||
{
|
||||
_length += _sink.write('}');
|
||||
_isCommaNeeded = true;
|
||||
}
|
||||
|
||||
void JsonWriter::writeKey(char const* key)
|
||||
{
|
||||
writeCommaIfNeeded();
|
||||
_length += EscapedString::printTo(key, _sink);
|
||||
_length += _sink.write(':');
|
||||
_isCommaNeeded = false;
|
||||
}
|
||||
|
||||
void JsonWriter::writeValue(char const* value)
|
||||
{
|
||||
writeCommaIfNeeded();
|
||||
_length += EscapedString::printTo(value, _sink);
|
||||
_isCommaNeeded = true;
|
||||
}
|
||||
|
||||
void JsonWriter::writeValue(long value)
|
||||
void JsonWriter::writeInteger(long value)
|
||||
{
|
||||
writeCommaIfNeeded();
|
||||
|
||||
_length += _sink.print(value);
|
||||
_isCommaNeeded = true;
|
||||
}
|
||||
|
||||
void JsonWriter::writeValue(bool value)
|
||||
void JsonWriter::writeBoolean(bool value)
|
||||
{
|
||||
writeCommaIfNeeded();
|
||||
_length += _sink.print(value ? "true" : "false");
|
||||
_isCommaNeeded = true;
|
||||
}
|
||||
|
||||
void JsonWriter::writeValue(double value, int decimals)
|
||||
void JsonWriter::writeDouble(double value, int decimals)
|
||||
{
|
||||
writeCommaIfNeeded();
|
||||
_length += _sink.print(value, decimals);
|
||||
_isCommaNeeded = true;
|
||||
}
|
@ -6,7 +6,7 @@ class JsonWriter
|
||||
{
|
||||
public:
|
||||
explicit JsonWriter(Print& sink)
|
||||
: _sink(sink), _length(0), _isCommaNeeded(false)
|
||||
: _sink(sink), _length(0)
|
||||
{
|
||||
}
|
||||
|
||||
@ -15,28 +15,35 @@ public:
|
||||
return _length;
|
||||
}
|
||||
|
||||
void beginArray();
|
||||
void endArray();
|
||||
virtual void beginArray() = 0;
|
||||
|
||||
void beginObject();
|
||||
void endObject();
|
||||
virtual void endArray() = 0;
|
||||
|
||||
void writeKey(const char* key);
|
||||
virtual void beginObject() = 0;
|
||||
|
||||
void writeValue(const char* value);
|
||||
void writeValue(long value);
|
||||
void writeValue(bool value);
|
||||
void writeValue(double value, int decimals);
|
||||
virtual void endObject() = 0;
|
||||
|
||||
private:
|
||||
void writeString(const char* value);
|
||||
void writeInteger(long value);
|
||||
void writeBoolean(bool value);
|
||||
void writeDouble(double value, int decimals);
|
||||
|
||||
virtual void writeColon() = 0;
|
||||
|
||||
virtual void writeComma() = 0;
|
||||
|
||||
void writeEmptyArray()
|
||||
{
|
||||
_length += _sink.print("[]");
|
||||
}
|
||||
|
||||
void writeEmptyObject()
|
||||
{
|
||||
_length += _sink.print("{}");
|
||||
}
|
||||
|
||||
protected:
|
||||
Print& _sink;
|
||||
size_t _length;
|
||||
bool _isCommaNeeded;
|
||||
|
||||
void writeCommaIfNeeded()
|
||||
{
|
||||
if (_isCommaNeeded)
|
||||
_length += _sink.write(',');
|
||||
}
|
||||
};
|
||||
|
||||
|
1
srcs/Internals/PrettyJsonWriter.cpp
Normal file
1
srcs/Internals/PrettyJsonWriter.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "PrettyJsonWriter.h"
|
54
srcs/Internals/PrettyJsonWriter.h
Normal file
54
srcs/Internals/PrettyJsonWriter.h
Normal file
@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonWriter.h"
|
||||
#include "IndentedPrint.h"
|
||||
|
||||
using namespace ArduinoJson::Generator;
|
||||
|
||||
class PrettyJsonWriter : public JsonWriter
|
||||
{
|
||||
public:
|
||||
explicit PrettyJsonWriter(IndentedPrint& sink)
|
||||
: JsonWriter(sink), _indenter(sink)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void beginArray()
|
||||
{
|
||||
_length += _sink.write('[');
|
||||
_indenter.indent();
|
||||
_length += _indenter.println();
|
||||
}
|
||||
|
||||
virtual void endArray()
|
||||
{
|
||||
_length += _indenter.println();
|
||||
_indenter.unindent();
|
||||
_length += _sink.write(']');
|
||||
}
|
||||
|
||||
virtual void writeColon()
|
||||
{
|
||||
_length += _sink.print(": ");
|
||||
}
|
||||
|
||||
virtual void writeComma()
|
||||
{
|
||||
_length += _sink.write(',');
|
||||
_length += _indenter.println();
|
||||
}
|
||||
|
||||
virtual void beginObject()
|
||||
{
|
||||
_length += _sink.write('{');
|
||||
}
|
||||
|
||||
virtual void endObject()
|
||||
{
|
||||
_length += _sink.write('}');
|
||||
_indenter.unindent();
|
||||
}
|
||||
|
||||
private:
|
||||
IndentedPrint& _indenter;
|
||||
};
|
Reference in New Issue
Block a user