Poco::JSON::PrintHandler not working for nested arrays #782

This commit is contained in:
albert.j.cross@gmail.com 2015-04-20 23:13:49 +02:00
parent 1b70142f63
commit fffb6bc7a0
3 changed files with 41 additions and 28 deletions

View File

@ -115,7 +115,6 @@ private:
unsigned _indent; unsigned _indent;
std::string _tab; std::string _tab;
int _array; int _array;
bool _value;
bool _objStart; bool _objStart;
}; };

View File

@ -27,8 +27,7 @@ PrintHandler::PrintHandler(unsigned indent):
_out(std::cout), _out(std::cout),
_indent(indent), _indent(indent),
_array(0), _array(0),
_value(false), _objStart(true)
_objStart(false)
{ {
} }
@ -37,8 +36,7 @@ PrintHandler::PrintHandler(std::ostream& out, unsigned indent):
_out(out), _out(out),
_indent(indent), _indent(indent),
_array(0), _array(0),
_value(false), _objStart(true)
_objStart(false)
{ {
} }
@ -53,7 +51,7 @@ void PrintHandler::reset()
_out.flush(); _out.flush();
_tab = ""; _tab = "";
_array = 0; _array = 0;
_value = false; _objStart = true;
} }
@ -94,16 +92,17 @@ void PrintHandler::endObject()
_tab.erase(_tab.length() - indent()); _tab.erase(_tab.length() - indent());
_out << endLine() << _tab << '}'; _out << endLine() << _tab << '}';
if (array()) _value = true; _objStart = false;
} }
void PrintHandler::startArray() void PrintHandler::startArray()
{ {
arrayValue();
_out << '[' << endLine(); _out << '[' << endLine();
_tab.append(indent(), ' '); _tab.append(indent(), ' ');
++_array; ++_array;
_value = false; _objStart = true;
} }
@ -113,18 +112,16 @@ void PrintHandler::endArray()
_out << endLine() << _tab << ']'; _out << endLine() << _tab << ']';
--_array; --_array;
poco_assert (_array >= 0); poco_assert (_array >= 0);
_value = false; _objStart = false;
} }
void PrintHandler::key(const std::string& k) void PrintHandler::key(const std::string& k)
{ {
if (_value)
{
if (!_objStart) comma(); if (!_objStart) comma();
_value = false;
} _objStart = true;
_objStart = false;
_out << _tab; _out << _tab;
Stringifier::formatString(k, _out); Stringifier::formatString(k, _out);
if (!printFlat()) _out << ' '; if (!printFlat()) _out << ' ';
@ -137,7 +134,8 @@ void PrintHandler::null()
{ {
arrayValue(); arrayValue();
_out << "null"; _out << "null";
_value = true;
_objStart = false;
} }
@ -145,7 +143,7 @@ void PrintHandler::value(int v)
{ {
arrayValue(); arrayValue();
_out << v; _out << v;
_value = true; _objStart = false;
} }
@ -153,7 +151,7 @@ void PrintHandler::value(unsigned v)
{ {
arrayValue(); arrayValue();
_out << v; _out << v;
_value = true; _objStart = false;
} }
@ -162,7 +160,7 @@ void PrintHandler::value(Int64 v)
{ {
arrayValue(); arrayValue();
_out << v; _out << v;
_value = true; _objStart = false;
} }
@ -170,7 +168,7 @@ void PrintHandler::value(UInt64 v)
{ {
arrayValue(); arrayValue();
_out << v; _out << v;
_value = true; _objStart = false;
} }
#endif #endif
@ -179,7 +177,7 @@ void PrintHandler::value(const std::string& value)
{ {
arrayValue(); arrayValue();
Stringifier::formatString(value, _out); Stringifier::formatString(value, _out);
_value = true; _objStart = false;
} }
@ -188,7 +186,7 @@ void PrintHandler::value(double d)
{ {
arrayValue(); arrayValue();
_out << d; _out << d;
_value = true; _objStart = false;
} }
@ -196,7 +194,7 @@ void PrintHandler::value(bool b)
{ {
arrayValue(); arrayValue();
_out << b; _out << b;
_value = true; _objStart = false;
} }
@ -208,9 +206,10 @@ void PrintHandler::comma()
void PrintHandler::arrayValue() void PrintHandler::arrayValue()
{ {
if (array())
{
if (_value) comma(); if (!_objStart) comma();
if (array()) {
_out << _tab; _out << _tab;
} }
} }

View File

@ -1220,6 +1220,21 @@ void JSONTest::testPrintHandler()
parser.reset(); parser.reset();
parser.parse(json); parser.parse(json);
assert (json == ostr.str()); assert (json == ostr.str());
json="[[\"a\"],[\"b\"],[[\"c\"],[\"d\"]]]";
ostr.str("");
pHandler->setIndent(0);
parser.reset();
parser.parse(json);
assert (json == ostr.str());
json="[{\"1\":\"one\",\"0\":[\"zero\",\"nil\"]}]";
ostr.str("");
pHandler->setIndent(0);
parser.reset();
parser.parse(json);
assert (json == ostr.str());
} }