forked from bblanchon/ArduinoJson
Extracted class IndentedPrintDecorator from PrettyPrintDecorator
This commit is contained in:
40
JsonGenerator/IndentedPrintDecorator.cpp
Normal file
40
JsonGenerator/IndentedPrintDecorator.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "IndentedPrintDecorator.h"
|
||||||
|
|
||||||
|
void IndentedPrintDecorator::indent()
|
||||||
|
{
|
||||||
|
currentTabCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IndentedPrintDecorator::unindent()
|
||||||
|
{
|
||||||
|
currentTabCount--;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t IndentedPrintDecorator::write(uint8_t c)
|
||||||
|
{
|
||||||
|
size_t n = 0;
|
||||||
|
|
||||||
|
if (isNewLine)
|
||||||
|
n += writeCurrentTabs();
|
||||||
|
|
||||||
|
n += sink.write(c);
|
||||||
|
|
||||||
|
isNewLine = c == '\n';
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t IndentedPrintDecorator::writeCurrentTabs()
|
||||||
|
{
|
||||||
|
return writeTabs(currentTabCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t IndentedPrintDecorator::writeTabs(int count)
|
||||||
|
{
|
||||||
|
size_t n = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i<count; i++)
|
||||||
|
n += sink.write(' ');
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
36
JsonGenerator/IndentedPrintDecorator.h
Normal file
36
JsonGenerator/IndentedPrintDecorator.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Arduino JSON library
|
||||||
|
* Benoit Blanchon 2014 - MIT License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Print.h"
|
||||||
|
|
||||||
|
class IndentedPrintDecorator : public Print
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
IndentedPrintDecorator(Print& p)
|
||||||
|
: sink(p)
|
||||||
|
{
|
||||||
|
pendingTabCount = 0;
|
||||||
|
currentTabCount = 0;
|
||||||
|
isNewLine = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void indent();
|
||||||
|
void unindent();
|
||||||
|
|
||||||
|
bool isNewLine;
|
||||||
|
virtual size_t write(uint8_t);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Print& sink;
|
||||||
|
int currentTabCount;
|
||||||
|
int pendingTabCount;
|
||||||
|
|
||||||
|
size_t writeCurrentTabs();
|
||||||
|
size_t writeTabs(int count);
|
||||||
|
};
|
||||||
|
|
@ -12,6 +12,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="EscapedString.h" />
|
<ClInclude Include="EscapedString.h" />
|
||||||
|
<ClInclude Include="IndentedPrintDecorator.h" />
|
||||||
<ClInclude Include="PrettyPrintDecorator.h" />
|
<ClInclude Include="PrettyPrintDecorator.h" />
|
||||||
<ClInclude Include="JsonArray.h" />
|
<ClInclude Include="JsonArray.h" />
|
||||||
<ClInclude Include="JsonArrayBase.h" />
|
<ClInclude Include="JsonArrayBase.h" />
|
||||||
@ -25,6 +26,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="EscapedString.cpp" />
|
<ClCompile Include="EscapedString.cpp" />
|
||||||
|
<ClCompile Include="IndentedPrintDecorator.cpp" />
|
||||||
<ClCompile Include="PrettyPrintDecorator.cpp" />
|
<ClCompile Include="PrettyPrintDecorator.cpp" />
|
||||||
<ClCompile Include="JsonArrayBase.cpp" />
|
<ClCompile Include="JsonArrayBase.cpp" />
|
||||||
<ClCompile Include="JsonObjectBase.cpp" />
|
<ClCompile Include="JsonObjectBase.cpp" />
|
||||||
|
@ -48,6 +48,9 @@
|
|||||||
<ClInclude Include="PrettyPrintDecorator.h">
|
<ClInclude Include="PrettyPrintDecorator.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="IndentedPrintDecorator.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="EscapedString.cpp">
|
<ClCompile Include="EscapedString.cpp">
|
||||||
@ -71,5 +74,8 @@
|
|||||||
<ClCompile Include="PrettyPrintDecorator.cpp">
|
<ClCompile Include="PrettyPrintDecorator.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="IndentedPrintDecorator.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -26,12 +26,18 @@ namespace ArduinoJson
|
|||||||
return printTo(sb);
|
return printTo(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t prettyPrintTo(Print& p) const
|
size_t prettyPrintTo(IndentedPrintDecorator& p) const
|
||||||
{
|
{
|
||||||
PrettyPrintDecorator decorator(p);
|
PrettyPrintDecorator decorator(p);
|
||||||
return printTo(decorator);
|
return printTo(decorator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t prettyPrintTo(Print& p) const
|
||||||
|
{
|
||||||
|
IndentedPrintDecorator decorator(p);
|
||||||
|
return printTo(decorator);
|
||||||
|
}
|
||||||
|
|
||||||
virtual size_t printTo(Print& p) const = 0;
|
virtual size_t printTo(Print& p) const = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -51,18 +51,20 @@ size_t PrettyPrintDecorator::handleMarkupChar(uint8_t c)
|
|||||||
|
|
||||||
size_t PrettyPrintDecorator::handleBlockOpen(uint8_t c)
|
size_t PrettyPrintDecorator::handleBlockOpen(uint8_t c)
|
||||||
{
|
{
|
||||||
size_t n = inEmptyBlock() ? breakThenWrite(c) : writeChar(c);
|
return indentIfNeeded() + writeChar(c);
|
||||||
|
|
||||||
indent++;
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t PrettyPrintDecorator::handleBlockClose(uint8_t c)
|
size_t PrettyPrintDecorator::handleBlockClose(uint8_t c)
|
||||||
{
|
{
|
||||||
indent--;
|
if (inEmptyBlock())
|
||||||
|
{
|
||||||
return inEmptyBlock() ? writeChar(c) : breakThenWrite(c);
|
return writeChar(c);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sink.unindent();
|
||||||
|
return breakThenWrite(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t PrettyPrintDecorator::handleColumn()
|
size_t PrettyPrintDecorator::handleColumn()
|
||||||
@ -77,7 +79,7 @@ size_t PrettyPrintDecorator::handleComma()
|
|||||||
|
|
||||||
size_t PrettyPrintDecorator::handleQuoteOpen()
|
size_t PrettyPrintDecorator::handleQuoteOpen()
|
||||||
{
|
{
|
||||||
size_t n = inEmptyBlock() ? breakThenWrite('"') : writeChar('"');
|
size_t n = indentIfNeeded() + writeChar('"');
|
||||||
|
|
||||||
inString = true;
|
inString = true;
|
||||||
|
|
||||||
@ -86,15 +88,13 @@ size_t PrettyPrintDecorator::handleQuoteOpen()
|
|||||||
|
|
||||||
size_t PrettyPrintDecorator::handleNormalChar(uint8_t c)
|
size_t PrettyPrintDecorator::handleNormalChar(uint8_t c)
|
||||||
{
|
{
|
||||||
return inEmptyBlock() ? breakThenWrite(c) : writeChar(c);
|
return indentIfNeeded() + writeChar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t PrettyPrintDecorator::breakAndIndent()
|
size_t PrettyPrintDecorator::indentIfNeeded()
|
||||||
{
|
{
|
||||||
size_t n = writeChar('\n');
|
if (!inEmptyBlock()) return 0;
|
||||||
|
|
||||||
for (int i = 0; i < indent; i++)
|
sink.indent();
|
||||||
n += writeChar(' ');
|
return sink.println();
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
}
|
@ -1,4 +1,3 @@
|
|||||||
#pragma once
|
|
||||||
/*
|
/*
|
||||||
* Arduino JSON library
|
* Arduino JSON library
|
||||||
* Benoit Blanchon 2014 - MIT License
|
* Benoit Blanchon 2014 - MIT License
|
||||||
@ -7,6 +6,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Print.h"
|
#include "Print.h"
|
||||||
|
#include "IndentedPrintDecorator.h"
|
||||||
|
|
||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
@ -16,8 +16,8 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
PrettyPrintDecorator(Print& p)
|
PrettyPrintDecorator(IndentedPrintDecorator& p)
|
||||||
: indent(0), sink(p)
|
: sink(p)
|
||||||
{
|
{
|
||||||
previousChar = 0;
|
previousChar = 0;
|
||||||
inString = false;
|
inString = false;
|
||||||
@ -26,9 +26,8 @@ namespace ArduinoJson
|
|||||||
virtual size_t write(uint8_t);
|
virtual size_t write(uint8_t);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int indent;
|
|
||||||
uint8_t previousChar;
|
uint8_t previousChar;
|
||||||
Print& sink;
|
IndentedPrintDecorator& sink;
|
||||||
bool inString;
|
bool inString;
|
||||||
|
|
||||||
bool inEmptyBlock()
|
bool inEmptyBlock()
|
||||||
@ -45,17 +44,16 @@ namespace ArduinoJson
|
|||||||
size_t handleComma();
|
size_t handleComma();
|
||||||
size_t handleQuoteOpen();
|
size_t handleQuoteOpen();
|
||||||
size_t handleNormalChar(uint8_t);
|
size_t handleNormalChar(uint8_t);
|
||||||
|
size_t indentIfNeeded();
|
||||||
size_t breakAndIndent();
|
|
||||||
|
|
||||||
size_t breakThenWrite(uint8_t c)
|
size_t breakThenWrite(uint8_t c)
|
||||||
{
|
{
|
||||||
return breakAndIndent() + writeChar(c);
|
return sink.println() + writeChar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t writeThenBreak(uint8_t c)
|
size_t writeThenBreak(uint8_t c)
|
||||||
{
|
{
|
||||||
return writeChar(c) + breakAndIndent();
|
return writeChar(c) + sink.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t writeChar(uint8_t c)
|
size_t writeChar(uint8_t c)
|
||||||
|
@ -32,4 +32,9 @@ size_t Print::print(long value)
|
|||||||
return print(tmp);
|
return print(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t Print::println()
|
||||||
|
{
|
||||||
|
return /*write('\r') +*/ write('\n');
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -19,6 +19,7 @@ public:
|
|||||||
size_t print(const char[]);
|
size_t print(const char[]);
|
||||||
size_t print(double, int = 2);
|
size_t print(double, int = 2);
|
||||||
size_t print(long);
|
size_t print(long);
|
||||||
|
size_t println();
|
||||||
};
|
};
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
@ -76,7 +76,8 @@ namespace JsonGeneratorTests
|
|||||||
void whenInputIs(const char input[])
|
void whenInputIs(const char input[])
|
||||||
{
|
{
|
||||||
StringBuilder sb(buffer, sizeof(buffer));
|
StringBuilder sb(buffer, sizeof(buffer));
|
||||||
PrettyPrintDecorator decorator(sb);
|
IndentedPrintDecorator indentedPrint(sb);
|
||||||
|
PrettyPrintDecorator decorator(indentedPrint);
|
||||||
|
|
||||||
returnValue = decorator.print(input);
|
returnValue = decorator.print(input);
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,8 @@ namespace JsonGeneratorTests
|
|||||||
void whenInputIs(const char input[])
|
void whenInputIs(const char input[])
|
||||||
{
|
{
|
||||||
StringBuilder sb(buffer, sizeof(buffer));
|
StringBuilder sb(buffer, sizeof(buffer));
|
||||||
PrettyPrintDecorator decorator(sb);
|
IndentedPrintDecorator indentedPrint(sb);
|
||||||
|
PrettyPrintDecorator decorator(indentedPrint);
|
||||||
|
|
||||||
returnValue = decorator.print(input);
|
returnValue = decorator.print(input);
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,8 @@ namespace JsonGeneratorTests
|
|||||||
void whenInputIs(const char input[])
|
void whenInputIs(const char input[])
|
||||||
{
|
{
|
||||||
StringBuilder sb(buffer, sizeof(buffer));
|
StringBuilder sb(buffer, sizeof(buffer));
|
||||||
PrettyPrintDecorator decorator(sb);
|
IndentedPrintDecorator indentedPrint(sb);
|
||||||
|
PrettyPrintDecorator decorator(indentedPrint);
|
||||||
|
|
||||||
returnValue = decorator.print(input);
|
returnValue = decorator.print(input);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user