mirror of
				https://github.com/pocoproject/poco.git
				synced 2025-10-25 18:22:59 +02:00 
			
		
		
		
	Poco::JSON::PrintHandler not working for objects in array #766
This commit is contained in:
		| @@ -109,12 +109,14 @@ private: | |||||||
| 	unsigned indent(); | 	unsigned indent(); | ||||||
| 	bool printFlat() const; | 	bool printFlat() const; | ||||||
| 	void arrayValue(); | 	void arrayValue(); | ||||||
|  | 	bool array() const; | ||||||
|  |  | ||||||
| 	std::ostream& _out; | 	std::ostream& _out; | ||||||
| 	unsigned      _indent; | 	unsigned      _indent; | ||||||
| 	std::string   _tab; | 	std::string   _tab; | ||||||
| 	bool          _array; | 	int           _array; | ||||||
| 	bool          _value; | 	bool          _value; | ||||||
|  | 	bool          _objStart; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -124,6 +126,13 @@ inline void PrintHandler::setIndent(unsigned indent) | |||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | inline bool PrintHandler::array() const | ||||||
|  | { | ||||||
|  | 	return _array > 0; | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| }} // namespace Poco::JSON | }} // namespace Poco::JSON | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -26,8 +26,9 @@ namespace JSON { | |||||||
| PrintHandler::PrintHandler(unsigned indent): | PrintHandler::PrintHandler(unsigned indent): | ||||||
| 	_out(std::cout), | 	_out(std::cout), | ||||||
| 	_indent(indent), | 	_indent(indent), | ||||||
| 	_array(false), | 	_array(0), | ||||||
| 	_value(false) | 	_value(false), | ||||||
|  | 	_objStart(false) | ||||||
| { | { | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -35,8 +36,9 @@ PrintHandler::PrintHandler(unsigned indent): | |||||||
| PrintHandler::PrintHandler(std::ostream& out, unsigned indent): | PrintHandler::PrintHandler(std::ostream& out, unsigned indent): | ||||||
| 	_out(out), | 	_out(out), | ||||||
| 	_indent(indent), | 	_indent(indent), | ||||||
| 	_array(false), | 	_array(0), | ||||||
| 	_value(false) | 	_value(false), | ||||||
|  | 	_objStart(false) | ||||||
| { | { | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -50,7 +52,7 @@ void PrintHandler::reset() | |||||||
| { | { | ||||||
| 	_out.flush(); | 	_out.flush(); | ||||||
| 	_tab = ""; | 	_tab = ""; | ||||||
| 	_array = false; | 	_array = 0; | ||||||
| 	_value = false; | 	_value = false; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -78,9 +80,11 @@ unsigned PrintHandler::indent() | |||||||
|  |  | ||||||
| void PrintHandler::startObject() | void PrintHandler::startObject() | ||||||
| { | { | ||||||
|  | 	arrayValue(); | ||||||
| 	_out << '{'; | 	_out << '{'; | ||||||
| 	_out << endLine(); | 	_out << endLine(); | ||||||
| 	_tab.append(indent(), ' '); | 	_tab.append(indent(), ' '); | ||||||
|  | 	_objStart = true; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -90,6 +94,7 @@ void PrintHandler::endObject() | |||||||
| 		_tab.erase(_tab.length() - indent()); | 		_tab.erase(_tab.length() - indent()); | ||||||
|  |  | ||||||
| 	_out << endLine() << _tab << '}'; | 	_out << endLine() << _tab << '}'; | ||||||
|  | 	if (array()) _value = true; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -97,7 +102,7 @@ void PrintHandler::startArray() | |||||||
| { | { | ||||||
| 	_out << '[' << endLine(); | 	_out << '[' << endLine(); | ||||||
| 	_tab.append(indent(), ' '); | 	_tab.append(indent(), ' '); | ||||||
| 	_array = true; | 	++_array; | ||||||
| 	_value = false; | 	_value = false; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -106,7 +111,8 @@ void PrintHandler::endArray() | |||||||
| { | { | ||||||
| 	_tab.erase(_tab.length() - indent()); | 	_tab.erase(_tab.length() - indent()); | ||||||
| 	_out << endLine() << _tab << ']'; | 	_out << endLine() << _tab << ']'; | ||||||
| 	_array = false; | 	--_array; | ||||||
|  | 	poco_assert (_array >= 0); | ||||||
| 	_value = false; | 	_value = false; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -115,9 +121,10 @@ void PrintHandler::key(const std::string& k) | |||||||
| { | { | ||||||
| 	if (_value) | 	if (_value) | ||||||
| 	{ | 	{ | ||||||
| 		comma(); | 		if (!_objStart) comma(); | ||||||
| 		_value = false; | 		_value = false; | ||||||
| 	} | 	} | ||||||
|  | 	_objStart = false; | ||||||
| 	_out << _tab; | 	_out << _tab; | ||||||
| 	Stringifier::formatString(k, _out); | 	Stringifier::formatString(k, _out); | ||||||
| 	if (!printFlat()) _out << ' '; | 	if (!printFlat()) _out << ' '; | ||||||
| @@ -201,7 +208,7 @@ void PrintHandler::comma() | |||||||
|  |  | ||||||
| void PrintHandler::arrayValue() | void PrintHandler::arrayValue() | ||||||
| { | { | ||||||
| 	if (_array) | 	if (array()) | ||||||
| 	{ | 	{ | ||||||
| 		if (_value) comma(); | 		if (_value) comma(); | ||||||
| 		_out << _tab; | 		_out << _tab; | ||||||
|   | |||||||
| @@ -1181,6 +1181,45 @@ void JSONTest::testPrintHandler() | |||||||
| 		"    ]\n" | 		"    ]\n" | ||||||
| 		"}" | 		"}" | ||||||
| 	); | 	); | ||||||
|  |  | ||||||
|  | 	json =  | ||||||
|  | 		"{" | ||||||
|  | 			"\"array\":" | ||||||
|  | 			"[" | ||||||
|  | 				"{" | ||||||
|  | 					"\"key1\":" | ||||||
|  | 					"[" | ||||||
|  | 						"1,2,3," | ||||||
|  | 						"{" | ||||||
|  | 							"\"subkey\":" | ||||||
|  | 							"\"test\"" | ||||||
|  | 						"}" | ||||||
|  | 					"]" | ||||||
|  | 				"}," | ||||||
|  | 				"{" | ||||||
|  | 					"\"key2\":" | ||||||
|  | 					"{" | ||||||
|  | 						"\"anotherSubKey\":" | ||||||
|  | 						"[" | ||||||
|  | 							"1," | ||||||
|  | 							"{" | ||||||
|  | 								"\"subSubKey\":" | ||||||
|  | 								"[" | ||||||
|  | 									"4,5,6" | ||||||
|  | 								"]" | ||||||
|  | 							"}" | ||||||
|  | 						"]" | ||||||
|  | 					"}" | ||||||
|  | 				"}" | ||||||
|  | 			"]" | ||||||
|  | 		"}"; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	ostr.str(""); | ||||||
|  | 	pHandler->setIndent(0); | ||||||
|  | 	parser.reset(); | ||||||
|  | 	parser.parse(json); | ||||||
|  | 	assert (json == ostr.str()); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Alex Fabijanic
					Alex Fabijanic