forked from bblanchon/ArduinoJson
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;
|
break;
|
||||||
|
|
||||||
case JSON_STRING:
|
case JSON_STRING:
|
||||||
writer.writeValue(content.asString);
|
writer.writeString(content.asString);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSON_INTEGER:
|
case JSON_INTEGER:
|
||||||
writer.writeValue(content.asInteger);
|
writer.writeInteger(content.asInteger);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSON_BOOLEAN:
|
case JSON_BOOLEAN:
|
||||||
writer.writeValue(content.asBoolean);
|
writer.writeBoolean(content.asBoolean);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JSON_PROXY:
|
case JSON_PROXY:
|
||||||
@ -33,40 +33,61 @@ void JsonNode::writeTo(JsonWriter& writer)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default: // >= JSON_DOUBLE_0_DECIMALS
|
default: // >= JSON_DOUBLE_0_DECIMALS
|
||||||
writer.writeValue(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
writer.writeDouble(content.asDouble, type - JSON_DOUBLE_0_DECIMALS);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonNode::writeArrayTo(JsonWriter& writer)
|
void JsonNode::writeArrayTo(JsonWriter& writer)
|
||||||
{
|
{
|
||||||
writer.beginArray();
|
|
||||||
|
|
||||||
JsonNode* child = content.asContainer.child;
|
JsonNode* child = content.asContainer.child;
|
||||||
|
|
||||||
while(child)
|
if (child)
|
||||||
|
{
|
||||||
|
writer.beginArray();
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
child->writeTo(writer);
|
child->writeTo(writer);
|
||||||
|
|
||||||
child = child->next;
|
child = child->next;
|
||||||
|
if (!child) break;
|
||||||
|
|
||||||
|
writer.writeComma();
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.endArray();
|
writer.endArray();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writer.writeEmptyArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void JsonNode::writeObjectTo(JsonWriter& writer)
|
void JsonNode::writeObjectTo(JsonWriter& writer)
|
||||||
{
|
{
|
||||||
writer.beginObject();
|
|
||||||
|
|
||||||
JsonNode* child = content.asContainer.child;
|
JsonNode* child = content.asContainer.child;
|
||||||
|
|
||||||
while (child)
|
if (child)
|
||||||
{
|
{
|
||||||
writer.writeKey(child->content.asKey.key);
|
writer.beginObject();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
writer.writeString(child->content.asKey.key);
|
||||||
|
writer.writeColon();
|
||||||
child->content.asKey.value->writeTo(writer);
|
child->content.asKey.value->writeTo(writer);
|
||||||
|
|
||||||
child = child->next;
|
child = child->next;
|
||||||
|
if (!child) break;
|
||||||
|
|
||||||
|
writer.writeComma();
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writer.writeEmptyObject();
|
||||||
|
}
|
||||||
|
}
|
@ -3,64 +3,23 @@
|
|||||||
|
|
||||||
using namespace ArduinoJson::Internals;
|
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);
|
_length += EscapedString::printTo(value, _sink);
|
||||||
_isCommaNeeded = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonWriter::writeValue(long value)
|
void JsonWriter::writeInteger(long value)
|
||||||
{
|
{
|
||||||
writeCommaIfNeeded();
|
|
||||||
_length += _sink.print(value);
|
_length += _sink.print(value);
|
||||||
_isCommaNeeded = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void JsonWriter::writeValue(bool value)
|
void JsonWriter::writeBoolean(bool value)
|
||||||
{
|
{
|
||||||
writeCommaIfNeeded();
|
|
||||||
_length += _sink.print(value ? "true" : "false");
|
_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);
|
_length += _sink.print(value, decimals);
|
||||||
_isCommaNeeded = true;
|
|
||||||
}
|
}
|
@ -6,7 +6,7 @@ class JsonWriter
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit JsonWriter(Print& sink)
|
explicit JsonWriter(Print& sink)
|
||||||
: _sink(sink), _length(0), _isCommaNeeded(false)
|
: _sink(sink), _length(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,28 +15,35 @@ public:
|
|||||||
return _length;
|
return _length;
|
||||||
}
|
}
|
||||||
|
|
||||||
void beginArray();
|
virtual void beginArray() = 0;
|
||||||
void endArray();
|
|
||||||
|
|
||||||
void beginObject();
|
virtual void endArray() = 0;
|
||||||
void endObject();
|
|
||||||
|
|
||||||
void writeKey(const char* key);
|
virtual void beginObject() = 0;
|
||||||
|
|
||||||
void writeValue(const char* value);
|
virtual void endObject() = 0;
|
||||||
void writeValue(long value);
|
|
||||||
void writeValue(bool value);
|
|
||||||
void writeValue(double value, int decimals);
|
|
||||||
|
|
||||||
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;
|
Print& _sink;
|
||||||
size_t _length;
|
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;
|
||||||
|
};
|
@ -57,3 +57,16 @@ void JsonArray::add(JsonContainer& innerContainer)
|
|||||||
node->content.asProxy.target = innerContainer._node;
|
node->content.asProxy.target = innerContainer._node;
|
||||||
addChild(node);
|
addChild(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonArray JsonArray::createNestedArray()
|
||||||
|
{
|
||||||
|
JsonNode* node = createNode(JSON_ARRAY);
|
||||||
|
|
||||||
|
if (node)
|
||||||
|
{
|
||||||
|
node->content.asContainer.buffer = _node->content.asContainer.buffer;
|
||||||
|
addChild(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonArray(node);
|
||||||
|
}
|
@ -22,5 +22,7 @@ public:
|
|||||||
void add(int value) { add((long) value); }
|
void add(int value) { add((long) value); }
|
||||||
void add(long value);
|
void add(long value);
|
||||||
void add(JsonContainer& innerContainer);
|
void add(JsonContainer& innerContainer);
|
||||||
|
|
||||||
|
JsonArray createNestedArray();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,24 +1,42 @@
|
|||||||
#include "JsonContainer.h"
|
#include "JsonContainer.h"
|
||||||
|
|
||||||
#include "JsonBuffer.h"
|
#include "JsonBuffer.h"
|
||||||
#include "Internals/JsonWriter.h"
|
|
||||||
#include "Internals/StringBuilder.h"
|
#include "Internals/StringBuilder.h"
|
||||||
|
#include "Internals/CompactJsonWriter.h"
|
||||||
|
#include "Internals/PrettyJsonWriter.h"
|
||||||
|
|
||||||
|
using namespace ArduinoJson::Internals;
|
||||||
|
|
||||||
size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const
|
size_t JsonContainer::printTo(char* buffer, size_t bufferSize) const
|
||||||
{
|
{
|
||||||
ArduinoJson::Internals::StringBuilder sb(buffer, bufferSize);
|
StringBuilder sb(buffer, bufferSize);
|
||||||
return printTo(sb);
|
return printTo(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t JsonContainer::printTo(Print& p) const
|
size_t JsonContainer::printTo(Print& p) const
|
||||||
{
|
{
|
||||||
JsonWriter writer(p);
|
CompactJsonWriter writer(p);
|
||||||
|
_node->writeTo(writer);
|
||||||
|
return writer.bytesWritten();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t JsonContainer::prettyPrintTo(char* buffer, size_t bufferSize) const
|
||||||
|
{
|
||||||
|
StringBuilder sb(buffer, bufferSize);
|
||||||
|
return prettyPrintTo(sb);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t JsonContainer::prettyPrintTo(IndentedPrint& p) const
|
||||||
|
{
|
||||||
|
PrettyJsonWriter writer(p);
|
||||||
_node->writeTo(writer);
|
_node->writeTo(writer);
|
||||||
return writer.bytesWritten();
|
return writer.bytesWritten();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode* JsonContainer::createNode(JsonNodeType type)
|
JsonNode* JsonContainer::createNode(JsonNodeType type)
|
||||||
{
|
{
|
||||||
|
if (!_node) return 0;
|
||||||
|
|
||||||
JsonBuffer* buffer = _node->content.asContainer.buffer;
|
JsonBuffer* buffer = _node->content.asContainer.buffer;
|
||||||
return buffer->createNode(type);
|
return buffer->createNode(type);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "Arduino/Printable.h"
|
#include "Arduino/Printable.h"
|
||||||
#include "Internals/JsonNodeIterator.h"
|
#include "Internals/JsonNodeIterator.h"
|
||||||
#include "Internals/JsonNode.h"
|
#include "Internals/JsonNode.h"
|
||||||
|
#include "Internals/IndentedPrint.h"
|
||||||
|
|
||||||
struct JsonNode;
|
struct JsonNode;
|
||||||
class JsonValue;
|
class JsonValue;
|
||||||
@ -31,6 +32,13 @@ public:
|
|||||||
size_t printTo(char* buffer, size_t bufferSize) const;
|
size_t printTo(char* buffer, size_t bufferSize) const;
|
||||||
virtual size_t printTo(Print& print) const;
|
virtual size_t printTo(Print& print) const;
|
||||||
|
|
||||||
|
size_t prettyPrintTo(char* buffer, size_t bufferSize) const;
|
||||||
|
size_t prettyPrintTo(ArduinoJson::Generator::IndentedPrint& print) const;
|
||||||
|
size_t prettyPrintTo(Print& print) const
|
||||||
|
{
|
||||||
|
return prettyPrintTo(ArduinoJson::Generator::IndentedPrint(print));
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
JsonNodeIterator beginChildren() const
|
JsonNodeIterator beginChildren() const
|
||||||
|
@ -74,7 +74,9 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Internals\CompactJsonWriter.h" />
|
||||||
<ClInclude Include="Internals\EscapedString.h" />
|
<ClInclude Include="Internals\EscapedString.h" />
|
||||||
|
<ClInclude Include="Internals\IndentedPrint.h" />
|
||||||
<ClInclude Include="JsonArray.h" />
|
<ClInclude Include="JsonArray.h" />
|
||||||
<ClInclude Include="JsonBuffer.h" />
|
<ClInclude Include="JsonBuffer.h" />
|
||||||
<ClInclude Include="Internals\JsonNode.h" />
|
<ClInclude Include="Internals\JsonNode.h" />
|
||||||
@ -85,11 +87,14 @@
|
|||||||
<ClInclude Include="JsonValue.h" />
|
<ClInclude Include="JsonValue.h" />
|
||||||
<ClInclude Include="Arduino\Print.h" />
|
<ClInclude Include="Arduino\Print.h" />
|
||||||
<ClInclude Include="Arduino\Printable.h" />
|
<ClInclude Include="Arduino\Printable.h" />
|
||||||
|
<ClInclude Include="Internals\PrettyJsonWriter.h" />
|
||||||
<ClInclude Include="StaticJsonBuffer.h" />
|
<ClInclude Include="StaticJsonBuffer.h" />
|
||||||
<ClInclude Include="Internals\StringBuilder.h" />
|
<ClInclude Include="Internals\StringBuilder.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Internals\CompactJsonWriter.cpp" />
|
||||||
<ClCompile Include="Internals\EscapedString.cpp" />
|
<ClCompile Include="Internals\EscapedString.cpp" />
|
||||||
|
<ClCompile Include="Internals\IndentedPrint.cpp" />
|
||||||
<ClCompile Include="Internals\JsonNode.cpp" />
|
<ClCompile Include="Internals\JsonNode.cpp" />
|
||||||
<ClCompile Include="JsonArray.cpp" />
|
<ClCompile Include="JsonArray.cpp" />
|
||||||
<ClCompile Include="JsonBuffer.cpp" />
|
<ClCompile Include="JsonBuffer.cpp" />
|
||||||
@ -99,6 +104,7 @@
|
|||||||
<ClCompile Include="JsonValue.cpp" />
|
<ClCompile Include="JsonValue.cpp" />
|
||||||
<ClCompile Include="Arduino\Print.cpp" />
|
<ClCompile Include="Arduino\Print.cpp" />
|
||||||
<ClCompile Include="Internals\StringBuilder.cpp" />
|
<ClCompile Include="Internals\StringBuilder.cpp" />
|
||||||
|
<ClCompile Include="Internals\PrettyJsonWriter.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -54,6 +54,15 @@
|
|||||||
<ClInclude Include="Internals\JsonWriter.h">
|
<ClInclude Include="Internals\JsonWriter.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Internals\CompactJsonWriter.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Internals\PrettyJsonWriter.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Internals\IndentedPrint.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="JsonObject.cpp">
|
<ClCompile Include="JsonObject.cpp">
|
||||||
@ -86,5 +95,14 @@
|
|||||||
<ClCompile Include="Internals\JsonWriter.cpp">
|
<ClCompile Include="Internals\JsonWriter.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Internals\CompactJsonWriter.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Internals\PrettyJsonWriter.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Internals\IndentedPrint.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
93
tests/JsonArray_PrettyPrintTo_Tests.cpp
Normal file
93
tests/JsonArray_PrettyPrintTo_Tests.cpp
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <JsonArray.h>
|
||||||
|
#include <JsonObject.h>
|
||||||
|
#include <StaticJsonBuffer.h>
|
||||||
|
|
||||||
|
class JsonArray_PrettyPrintTo_Tests : public testing::Test
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
JsonArray array;
|
||||||
|
StaticJsonBuffer<30> json;
|
||||||
|
|
||||||
|
virtual void SetUp()
|
||||||
|
{
|
||||||
|
array = json.createArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
void outputMustBe(const char* expected)
|
||||||
|
{
|
||||||
|
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
|
||||||
|
EXPECT_STREQ(expected, buffer);
|
||||||
|
EXPECT_EQ(strlen(expected), n);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
char buffer[256];
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty)
|
||||||
|
{
|
||||||
|
outputMustBe("[]");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement)
|
||||||
|
{
|
||||||
|
array.add(1);
|
||||||
|
|
||||||
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
|
" 1\r\n"
|
||||||
|
"]");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements)
|
||||||
|
{
|
||||||
|
array.add(1);
|
||||||
|
array.add(2);
|
||||||
|
|
||||||
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
|
" 1,\r\n"
|
||||||
|
" 2\r\n"
|
||||||
|
"]");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays)
|
||||||
|
{
|
||||||
|
array.createNestedArray();
|
||||||
|
array.createNestedArray();
|
||||||
|
|
||||||
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
|
" [],\r\n"
|
||||||
|
" []\r\n"
|
||||||
|
"]");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays)
|
||||||
|
{
|
||||||
|
JsonArray nested1 = array.createNestedArray();
|
||||||
|
nested1.add(1);
|
||||||
|
nested1.add(2);
|
||||||
|
|
||||||
|
JsonArray nested2 = array.createNestedArray();
|
||||||
|
nested2.add(3);
|
||||||
|
nested2.add(4);
|
||||||
|
|
||||||
|
outputMustBe(
|
||||||
|
"[\r\n"
|
||||||
|
" [\r\n"
|
||||||
|
" 1,\r\n"
|
||||||
|
" 2\r\n"
|
||||||
|
" ],\r\n"
|
||||||
|
" [\r\n"
|
||||||
|
" 3,\r\n"
|
||||||
|
" 4\r\n"
|
||||||
|
" ]\r\n"
|
||||||
|
"]");
|
||||||
|
}
|
@ -8,7 +8,7 @@
|
|||||||
#include <JsonObject.h>
|
#include <JsonObject.h>
|
||||||
#include <StaticJsonBuffer.h>
|
#include <StaticJsonBuffer.h>
|
||||||
|
|
||||||
class JsonArray_Serialization_Tests : public testing::Test
|
class JsonArray_PrintTo_Tests : public testing::Test
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
JsonArray array;
|
JsonArray array;
|
||||||
@ -30,26 +30,26 @@ private:
|
|||||||
char buffer[256];
|
char buffer[256];
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, Empty)
|
TEST_F(JsonArray_PrintTo_Tests, Empty)
|
||||||
{
|
{
|
||||||
outputMustBe("[]");
|
outputMustBe("[]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, Null)
|
TEST_F(JsonArray_PrintTo_Tests, Null)
|
||||||
{
|
{
|
||||||
array.add((char*) 0);
|
array.add((char*) 0);
|
||||||
|
|
||||||
outputMustBe("[null]");
|
outputMustBe("[null]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneString)
|
TEST_F(JsonArray_PrintTo_Tests, OneString)
|
||||||
{
|
{
|
||||||
array.add("hello");
|
array.add("hello");
|
||||||
|
|
||||||
outputMustBe("[\"hello\"]");
|
outputMustBe("[\"hello\"]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, TwoStrings)
|
TEST_F(JsonArray_PrintTo_Tests, TwoStrings)
|
||||||
{
|
{
|
||||||
array.add("hello");
|
array.add("hello");
|
||||||
array.add("world");
|
array.add("world");
|
||||||
@ -57,7 +57,7 @@ TEST_F(JsonArray_Serialization_Tests, TwoStrings)
|
|||||||
outputMustBe("[\"hello\",\"world\"]");
|
outputMustBe("[\"hello\",\"world\"]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneStringOverCapacity)
|
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity)
|
||||||
{
|
{
|
||||||
array.add("hello");
|
array.add("hello");
|
||||||
array.add("world");
|
array.add("world");
|
||||||
@ -66,26 +66,26 @@ TEST_F(JsonArray_Serialization_Tests, OneStringOverCapacity)
|
|||||||
outputMustBe("[\"hello\",\"world\"]");
|
outputMustBe("[\"hello\",\"world\"]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneDoubleDefaultDigits)
|
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits)
|
||||||
{
|
{
|
||||||
array.add(3.14159265358979323846);
|
array.add(3.14159265358979323846);
|
||||||
outputMustBe("[3.14]");
|
outputMustBe("[3.14]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneDoubleFourDigits)
|
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits)
|
||||||
{
|
{
|
||||||
array.add(3.14159265358979323846, 4);
|
array.add(3.14159265358979323846, 4);
|
||||||
outputMustBe("[3.1416]");
|
outputMustBe("[3.1416]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneInteger)
|
TEST_F(JsonArray_PrintTo_Tests, OneInteger)
|
||||||
{
|
{
|
||||||
array.add(1);
|
array.add(1);
|
||||||
|
|
||||||
outputMustBe("[1]");
|
outputMustBe("[1]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, TwoIntegers)
|
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers)
|
||||||
{
|
{
|
||||||
array.add(1);
|
array.add(1);
|
||||||
array.add(2);
|
array.add(2);
|
||||||
@ -93,7 +93,7 @@ TEST_F(JsonArray_Serialization_Tests, TwoIntegers)
|
|||||||
outputMustBe("[1,2]");
|
outputMustBe("[1,2]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneIntegerOverCapacity)
|
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity)
|
||||||
{
|
{
|
||||||
array.add(1);
|
array.add(1);
|
||||||
array.add(2);
|
array.add(2);
|
||||||
@ -102,21 +102,21 @@ TEST_F(JsonArray_Serialization_Tests, OneIntegerOverCapacity)
|
|||||||
outputMustBe("[1,2]");
|
outputMustBe("[1,2]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneTrue)
|
TEST_F(JsonArray_PrintTo_Tests, OneTrue)
|
||||||
{
|
{
|
||||||
array.add(true);
|
array.add(true);
|
||||||
|
|
||||||
outputMustBe("[true]");
|
outputMustBe("[true]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneFalse)
|
TEST_F(JsonArray_PrintTo_Tests, OneFalse)
|
||||||
{
|
{
|
||||||
array.add(false);
|
array.add(false);
|
||||||
|
|
||||||
outputMustBe("[false]");
|
outputMustBe("[false]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, TwoBooleans)
|
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans)
|
||||||
{
|
{
|
||||||
array.add(false);
|
array.add(false);
|
||||||
array.add(true);
|
array.add(true);
|
||||||
@ -124,7 +124,7 @@ TEST_F(JsonArray_Serialization_Tests, TwoBooleans)
|
|||||||
outputMustBe("[false,true]");
|
outputMustBe("[false,true]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneBooleanOverCapacity)
|
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity)
|
||||||
{
|
{
|
||||||
array.add(false);
|
array.add(false);
|
||||||
array.add(true);
|
array.add(true);
|
||||||
@ -133,7 +133,7 @@ TEST_F(JsonArray_Serialization_Tests, OneBooleanOverCapacity)
|
|||||||
outputMustBe("[false,true]");
|
outputMustBe("[false,true]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneEmptyNestedArray)
|
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray)
|
||||||
{
|
{
|
||||||
JsonArray nestedArray = json.createArray();
|
JsonArray nestedArray = json.createArray();
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ TEST_F(JsonArray_Serialization_Tests, OneEmptyNestedArray)
|
|||||||
outputMustBe("[[]]");
|
outputMustBe("[[]]");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Serialization_Tests, OneEmptyNestedHash)
|
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash)
|
||||||
{
|
{
|
||||||
JsonObject nestedObject = json.createObject();
|
JsonObject nestedObject = json.createObject();
|
||||||
|
|
@ -87,7 +87,8 @@
|
|||||||
<ClCompile Include="..\third-party\gtest-1.7.0\src\gtest_main.cc" />
|
<ClCompile Include="..\third-party\gtest-1.7.0\src\gtest_main.cc" />
|
||||||
<ClCompile Include="EscapedStringTests.cpp" />
|
<ClCompile Include="EscapedStringTests.cpp" />
|
||||||
<ClCompile Include="JsonArray_Container_Tests.cpp" />
|
<ClCompile Include="JsonArray_Container_Tests.cpp" />
|
||||||
<ClCompile Include="JsonArray_Serialization_Tests.cpp" />
|
<ClCompile Include="JsonArray_PrettyPrintTo_Tests.cpp" />
|
||||||
|
<ClCompile Include="JsonArray_PrintTo_Tests.cpp" />
|
||||||
<ClCompile Include="JsonObject_Serialization_Tests.cpp" />
|
<ClCompile Include="JsonObject_Serialization_Tests.cpp" />
|
||||||
<ClCompile Include="JsonObject_Container_Tests.cpp" />
|
<ClCompile Include="JsonObject_Container_Tests.cpp" />
|
||||||
<ClCompile Include="JsonValueTests.cpp" />
|
<ClCompile Include="JsonValueTests.cpp" />
|
||||||
|
@ -36,9 +36,6 @@
|
|||||||
<ClCompile Include="StringBuilderTests.cpp">
|
<ClCompile Include="StringBuilderTests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="JsonArray_Serialization_Tests.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="JsonObject_Serialization_Tests.cpp">
|
<ClCompile Include="JsonObject_Serialization_Tests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -48,5 +45,11 @@
|
|||||||
<ClCompile Include="JsonArray_Container_Tests.cpp">
|
<ClCompile Include="JsonArray_Container_Tests.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="JsonArray_PrintTo_Tests.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="JsonArray_PrettyPrintTo_Tests.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Reference in New Issue
Block a user