2014-08-25 09:06:21 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
2014-08-25 11:42:07 +02:00
|
|
|
#include "PrettyPrintDecorator.h"
|
2014-08-25 09:06:21 +02:00
|
|
|
|
2014-08-25 13:19:07 +02:00
|
|
|
using namespace ArduinoJson::Generator;
|
|
|
|
|
2014-08-25 11:42:07 +02:00
|
|
|
size_t PrettyPrintDecorator::write(uint8_t c)
|
2014-08-25 09:06:21 +02:00
|
|
|
{
|
2014-08-25 12:41:49 +02:00
|
|
|
size_t n = inString ? handleStringChar(c) : handleMarkupChar(c);
|
|
|
|
previousChar = c;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t PrettyPrintDecorator::handleStringChar(uint8_t c)
|
|
|
|
{
|
|
|
|
bool isQuote = c == '"' && previousChar != '\\';
|
2014-08-25 11:46:42 +02:00
|
|
|
|
2014-08-25 12:48:38 +02:00
|
|
|
if (isQuote) inString = false;
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
return writeChar(c);
|
2014-08-25 12:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t PrettyPrintDecorator::handleMarkupChar(uint8_t c)
|
|
|
|
{
|
2014-08-25 09:52:42 +02:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '{':
|
2014-08-25 12:23:08 +02:00
|
|
|
case '[':
|
2014-08-25 13:14:09 +02:00
|
|
|
return handleBlockOpen(c);
|
2014-08-25 09:52:42 +02:00
|
|
|
|
|
|
|
case '}':
|
2014-08-25 12:41:49 +02:00
|
|
|
case ']':
|
2014-08-25 13:14:09 +02:00
|
|
|
return handleBlockClose(c);
|
2014-08-25 10:31:03 +02:00
|
|
|
|
2014-08-25 10:34:28 +02:00
|
|
|
case ':':
|
2014-08-25 13:14:09 +02:00
|
|
|
return handleColumn();
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
return handleComma();
|
2014-08-25 10:22:42 +02:00
|
|
|
|
2014-08-25 12:41:49 +02:00
|
|
|
case '"':
|
2014-08-25 13:14:09 +02:00
|
|
|
return handleQuoteOpen();
|
2014-08-25 11:46:42 +02:00
|
|
|
|
2014-08-25 12:23:08 +02:00
|
|
|
default:
|
2014-08-25 13:14:09 +02:00
|
|
|
return handleNormalChar(c);
|
2014-08-25 09:52:42 +02:00
|
|
|
}
|
2014-08-25 12:23:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t PrettyPrintDecorator::handleBlockOpen(uint8_t c)
|
2014-08-25 12:23:08 +02:00
|
|
|
{
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t n = inEmptyBlock() ? breakThenWrite(c) : writeChar(c);
|
|
|
|
|
|
|
|
indent++;
|
|
|
|
|
|
|
|
return n;
|
2014-08-25 12:23:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t PrettyPrintDecorator::handleBlockClose(uint8_t c)
|
2014-08-25 12:23:08 +02:00
|
|
|
{
|
2014-08-25 13:14:09 +02:00
|
|
|
indent--;
|
|
|
|
|
|
|
|
return inEmptyBlock() ? writeChar(c) : breakThenWrite(c);
|
2014-08-25 12:23:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t PrettyPrintDecorator::handleColumn()
|
2014-08-25 12:23:08 +02:00
|
|
|
{
|
2014-08-25 13:14:09 +02:00
|
|
|
return writeChar(':') + writeChar(' ');
|
2014-08-25 12:23:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t PrettyPrintDecorator::handleComma()
|
2014-08-25 12:23:08 +02:00
|
|
|
{
|
2014-08-25 13:14:09 +02:00
|
|
|
return writeThenBreak(',');
|
2014-08-25 12:23:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t PrettyPrintDecorator::handleQuoteOpen()
|
2014-08-25 12:41:49 +02:00
|
|
|
{
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t n = inEmptyBlock() ? breakThenWrite('"') : writeChar('"');
|
2014-08-25 12:48:38 +02:00
|
|
|
|
|
|
|
inString = true;
|
|
|
|
|
2014-08-25 12:41:49 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t PrettyPrintDecorator::handleNormalChar(uint8_t c)
|
2014-08-25 12:23:08 +02:00
|
|
|
{
|
2014-08-25 13:14:09 +02:00
|
|
|
return inEmptyBlock() ? breakThenWrite(c) : writeChar(c);
|
2014-08-25 12:41:49 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t PrettyPrintDecorator::breakAndIndent()
|
2014-08-25 12:41:49 +02:00
|
|
|
{
|
2014-08-25 13:14:09 +02:00
|
|
|
size_t n = writeChar('\n');
|
2014-08-25 12:41:49 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < indent; i++)
|
2014-08-25 13:14:09 +02:00
|
|
|
n += writeChar(' ');
|
2014-08-25 12:41:49 +02:00
|
|
|
|
|
|
|
return n;
|
2014-08-25 09:06:21 +02:00
|
|
|
}
|