Skip to content

Commit fffb6bc

Browse files
author
albert.j.cross@gmail.com
committed
Poco::JSON::PrintHandler not working for nested arrays pocoproject#782
1 parent 1b70142 commit fffb6bc

3 files changed

Lines changed: 41 additions & 28 deletions

File tree

‎JSON/include/Poco/JSON/PrintHandler.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ class JSON_API PrintHandler : public Handler
115115
unsigned _indent;
116116
std::string _tab;
117117
int _array;
118-
bool _value;
119118
bool _objStart;
120119
};
121120

‎JSON/src/PrintHandler.cpp‎

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ PrintHandler::PrintHandler(unsigned indent):
2727
_out(std::cout),
2828
_indent(indent),
2929
_array(0),
30-
_value(false),
31-
_objStart(false)
30+
_objStart(true)
3231
{
3332
}
3433

@@ -37,8 +36,7 @@ PrintHandler::PrintHandler(std::ostream& out, unsigned indent):
3736
_out(out),
3837
_indent(indent),
3938
_array(0),
40-
_value(false),
41-
_objStart(false)
39+
_objStart(true)
4240
{
4341
}
4442

@@ -53,7 +51,7 @@ void PrintHandler::reset()
5351
_out.flush();
5452
_tab = "";
5553
_array = 0;
56-
_value = false;
54+
_objStart = true;
5755
}
5856

5957

@@ -94,16 +92,17 @@ void PrintHandler::endObject()
9492
_tab.erase(_tab.length() - indent());
9593

9694
_out << endLine() << _tab << '}';
97-
if (array()) _value = true;
95+
_objStart = false;
9896
}
9997

10098

10199
void PrintHandler::startArray()
102100
{
101+
arrayValue();
103102
_out << '[' << endLine();
104103
_tab.append(indent(), ' ');
105104
++_array;
106-
_value = false;
105+
_objStart = true;
107106
}
108107

109108

@@ -113,18 +112,16 @@ void PrintHandler::endArray()
113112
_out << endLine() << _tab << ']';
114113
--_array;
115114
poco_assert (_array >= 0);
116-
_value = false;
115+
_objStart = false;
117116
}
118117

119118

120119
void PrintHandler::key(const std::string& k)
121120
{
122-
if (_value)
123-
{
124-
if (!_objStart) comma();
125-
_value = false;
126-
}
127-
_objStart = false;
121+
if (!_objStart) comma();
122+
123+
_objStart = true;
124+
128125
_out << _tab;
129126
Stringifier::formatString(k, _out);
130127
if (!printFlat()) _out << ' ';
@@ -137,23 +134,24 @@ void PrintHandler::null()
137134
{
138135
arrayValue();
139136
_out << "null";
140-
_value = true;
137+
138+
_objStart = false;
141139
}
142140

143141

144142
void PrintHandler::value(int v)
145143
{
146144
arrayValue();
147145
_out << v;
148-
_value = true;
146+
_objStart = false;
149147
}
150148

151149

152150
void PrintHandler::value(unsigned v)
153151
{
154152
arrayValue();
155153
_out << v;
156-
_value = true;
154+
_objStart = false;
157155
}
158156

159157

@@ -162,15 +160,15 @@ void PrintHandler::value(Int64 v)
162160
{
163161
arrayValue();
164162
_out << v;
165-
_value = true;
163+
_objStart = false;
166164
}
167165

168166

169167
void PrintHandler::value(UInt64 v)
170168
{
171169
arrayValue();
172170
_out << v;
173-
_value = true;
171+
_objStart = false;
174172
}
175173
#endif
176174

@@ -179,7 +177,7 @@ void PrintHandler::value(const std::string& value)
179177
{
180178
arrayValue();
181179
Stringifier::formatString(value, _out);
182-
_value = true;
180+
_objStart = false;
183181
}
184182

185183

@@ -188,15 +186,15 @@ void PrintHandler::value(double d)
188186
{
189187
arrayValue();
190188
_out << d;
191-
_value = true;
189+
_objStart = false;
192190
}
193191

194192

195193
void PrintHandler::value(bool b)
196194
{
197195
arrayValue();
198196
_out << b;
199-
_value = true;
197+
_objStart = false;
200198
}
201199

202200

@@ -207,12 +205,13 @@ void PrintHandler::comma()
207205

208206

209207
void PrintHandler::arrayValue()
210-
{
211-
if (array())
212-
{
213-
if (_value) comma();
208+
{
209+
210+
211+
if (!_objStart) comma();
212+
if (array()) {
214213
_out << _tab;
215-
}
214+
}
216215
}
217216

218217

‎JSON/testsuite/src/JSONTest.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,21 @@ void JSONTest::testPrintHandler()
12201220
parser.reset();
12211221
parser.parse(json);
12221222
assert (json == ostr.str());
1223+
1224+
json="[[\"a\"],[\"b\"],[[\"c\"],[\"d\"]]]";
1225+
ostr.str("");
1226+
pHandler->setIndent(0);
1227+
parser.reset();
1228+
parser.parse(json);
1229+
assert (json == ostr.str());
1230+
1231+
json="[{\"1\":\"one\",\"0\":[\"zero\",\"nil\"]}]";
1232+
ostr.str("");
1233+
pHandler->setIndent(0);
1234+
parser.reset();
1235+
parser.parse(json);
1236+
assert (json == ostr.str());
1237+
12231238
}
12241239

12251240

0 commit comments

Comments
 (0)