Browse Source

GH #118: JSON::Object::stringify endless loop

aleks-f 13 years ago
parent
commit
727e3cd28d

+ 4 - 2
CHANGELOG

@@ -13,10 +13,12 @@ Release 1.5.2 (2013-03-??)
 - merged GH #86: Invalid pointers to vector internals (by Adrian Imboden)
 - automatic library initialization macros
 - fixed GH #110: WebSocket accept() fails when Connection header contains multiple tokens
-- fixed GH# 71: WebSocket and broken Timeouts (POCO_BROKEN_TIMEOUTS)
+- fixed GH #71: WebSocket and broken Timeouts (POCO_BROKEN_TIMEOUTS)
 - fixed a warning in Poco/Crypto/OpenSSLInitializer.h
-- fixed GH# 109: Bug in Poco::Net::SMTPClientSession::loginUsingPlain
+- fixed GH #109: Bug in Poco::Net::SMTPClientSession::loginUsingPlain
 - added clang libc++ build configurations for Darwin and iPhone (Andrea Bigagli)
+- fixed GH #116: Wrong timezone parsing in DateTimeParse (fix by Matej Knopp)
+- fixed GH #118: JSON::Object::stringify endless loop
 
 Release 1.5.1 (2013-01-11)
 ==========================

+ 1 - 0
CONTRIBUTORS

@@ -32,6 +32,7 @@ Roger Meier
 Mathaus Mendel
 Arturo Castro
 Adrian Imboden
+Matej Knopp
 
 --
 $Id$

+ 1 - 0
JSON/include/Poco/JSON/Object.h

@@ -146,6 +146,7 @@ public:
 		/// Removes the property with the given key
 
 private:
+	//TODO: unordered map
 	typedef std::map<std::string, Dynamic::Var> ValueMap;
 	ValueMap _values;
 };

+ 5 - 24
JSON/src/Array.cpp

@@ -123,41 +123,22 @@ bool Array::isObject(unsigned int index) const
 void Array::stringify(std::ostream& out, unsigned int indent) const
 {
 	out << "[";
-	if ( indent > 0 )
-		out << std::endl;
 
-	for(ValueVec::const_iterator it = _values.begin(); it != _values.end();)
+	if (indent > 0) out << std::endl;
+
+	for (ValueVec::const_iterator it = _values.begin(); it != _values.end();)
 	{
-		for(int i = 0; i < indent; i++)
-		{
-			out << ' ';
-		}
+		for(int i = 0; i < indent; i++) out << ' ';
 
 		Stringifier::stringify(*it, out, indent);
 
 		if ( ++it != _values.end() )
 		{
 			out << ",";
-			if ( indent > 0 )
-			{
-				out << std::endl;
-			}
+			if ( indent > 0 ) out << std::endl;
 		}
 	}
 
-	if ( indent > 0 )
-	{
-		out << std::endl;
-	}
-
-	if ( indent > 0 )
-		indent -= 2;
-
-	for(int i = 0; i < indent; i++)
-	{
-		out << ' ';
-	}
-
 	out << "]";
 }
 

+ 7 - 25
JSON/src/Object.cpp

@@ -117,41 +117,23 @@ void Object::getNames(std::vector<std::string>& names) const
 void Object::stringify(std::ostream& out, unsigned int indent) const
 {
 	out << '{';
-	if ( indent > 0 )
-	{
-		out << std::endl;
-	}
 
-	for(ValueMap::const_iterator it = _values.begin(); it != _values.end();)
+	if (indent > 0) out << std::endl;
+
+	for (ValueMap::const_iterator it = _values.begin(); it != _values.end();)
 	{
-		for(int i = 0; i < indent; i++)
-		{
-			out << ' ';
-		}
+		for(int i = 0; i < indent; i++) out << ' ';
 
 		out << '"' << it->first << '"';
 		out << (( indent > 0 ) ? " : " : ":");
 
 		Stringifier::stringify(it->second, out, indent);
 
-		if ( ++it != _values.end() )
-		{
-			out << ',';
-		}
+		if ( ++it != _values.end() ) out << ',';
 
-		if ( indent > 0 )
-		{
-			out << std::endl;
-		}
+		if ( indent > 0 ) out << std::endl;
 	}
-
-	if ( indent > 0 )
-		indent -= 2;
-	for(int i = 0; i < indent; i++)
-	{
-		out << ' ';
-	}
-
+	
 	out << '}';
 }
 

+ 30 - 2
JSON/testsuite/src/JSONTest.cpp

@@ -195,7 +195,7 @@ void JSONTest::testNumberProperty()
 
 void JSONTest::testUnsignedNumberProperty()
 {
-    // 4294967295 == unsigned(-1)
+	// 4294967295 == unsigned(-1)
 	std::string json = "{ \"test\" : 4294967295 }";
 	Parser parser;
 	Var result;
@@ -256,7 +256,7 @@ void JSONTest::testNumber64Property()
 
 void JSONTest::testUnsignedNumber64Property()
 {
-    // 18446744073709551615 == UInt64(-1)
+	// 18446744073709551615 == UInt64(-1)
 	std::string json = "{ \"test\" : 18446744073709551615 }";
 	Parser parser;
 	Var result;
@@ -801,6 +801,33 @@ void JSONTest::testQuery()
 }
 
 
+void JSONTest::testStringify()
+{
+	std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }";
+	Parser parser;
+	Var result;
+
+	try
+	{
+		DefaultHandler handler;
+		parser.setHandler(&handler);
+		parser.parse(json);
+		result = handler.result();
+	}
+	catch(JSONException& jsone)
+	{
+		std::cout << jsone.message() << std::endl;
+		assert(false);
+	}
+
+	assert(result.type() == typeid(Object::Ptr));
+	std::ostringstream ostr;
+	Stringifier::stringify(result, ostr);
+	//TODO: need map that does not order for internal container
+	assert (ostr.str() == "{\"name\":\"Franky\",\"children\":[\"Jonas\",\"Ellen\"]}");
+}
+
+
 void JSONTest::testValidJanssonFiles()
 {
 	Poco::Path pathPattern(getTestFilesPath("valid"));
@@ -1044,6 +1071,7 @@ CppUnit::Test* JSONTest::suite()
 	CppUnit_addTest(pSuite, JSONTest, testDoubleElement);
 	CppUnit_addTest(pSuite, JSONTest, testOptValue);
 	CppUnit_addTest(pSuite, JSONTest, testQuery);
+	CppUnit_addTest(pSuite, JSONTest, testStringify);
 	CppUnit_addTest(pSuite, JSONTest, testValidJanssonFiles);
 	CppUnit_addTest(pSuite, JSONTest, testInvalidJanssonFiles);
 	CppUnit_addTest(pSuite, JSONTest, testInvalidUnicodeJanssonFiles);

+ 2 - 0
JSON/testsuite/src/JSONTest.h

@@ -73,6 +73,8 @@ public:
 	void testDoubleElement();
 	void testOptValue();
 	void testQuery();
+	void testStringify();
+
 	void testValidJanssonFiles();
 	void testInvalidJanssonFiles();
 	void testTemplate();