From 42d963a7d5ac1805b454b976ec0301fcff7b32eb Mon Sep 17 00:00:00 2001 From: aleks-f Date: Sun, 2 Dec 2012 22:12:06 -0600 Subject: [PATCH] GH 23: JSON::Object::stringify throw BadCastException GH issue #23 : JSON::Object::stringify throw BadCastException --- JSON/src/Stringifier.cpp | 12 +++++++++++- JSON/testsuite/src/JSONTest.cpp | 17 +++++++++++++++++ JSON/testsuite/src/JSONTest.h | 1 + 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/JSON/src/Stringifier.cpp b/JSON/src/Stringifier.cpp index e65cd2953..46842e4c8 100644 --- a/JSON/src/Stringifier.cpp +++ b/JSON/src/Stringifier.cpp @@ -49,7 +49,17 @@ namespace JSON { void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent) { - if ( any.type() == typeid(Object::Ptr) ) + if ( any.type() == typeid(Object) ) + { + const Object& o = any.extract(); + o.stringify(out, indent == 0 ? 0 : indent + 2); + } + else if ( any.type() == typeid(Array) ) + { + const Array& a = any.extract(); + a.stringify(out, indent == 0 ? 0 : indent + 2); + } + else if ( any.type() == typeid(Object::Ptr) ) { const Object::Ptr& o = any.extract(); o->stringify(out, indent == 0 ? 0 : indent + 2); diff --git a/JSON/testsuite/src/JSONTest.cpp b/JSON/testsuite/src/JSONTest.cpp index 6b6d9e998..5fe906b73 100644 --- a/JSON/testsuite/src/JSONTest.cpp +++ b/JSON/testsuite/src/JSONTest.cpp @@ -74,6 +74,22 @@ void JSONTest::tearDown() } +void JSONTest::testStringifier() +{ + Object obj; + + Array arr; + Object obj2; + + obj.set("array", arr); + obj.set("obj2", obj2); + + std::ostringstream ostr; + obj.stringify(ostr); + assert (ostr.str() == "{\"array\":[],\"obj2\":{}}"); +} + + void JSONTest::testNullProperty() { std::string json = "{ \"test\" : null }"; @@ -806,6 +822,7 @@ CppUnit::Test* JSONTest::suite() { CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONTest"); + CppUnit_addTest(pSuite, JSONTest, testStringifier); CppUnit_addTest(pSuite, JSONTest, testNullProperty); CppUnit_addTest(pSuite, JSONTest, testTrueProperty); CppUnit_addTest(pSuite, JSONTest, testFalseProperty); diff --git a/JSON/testsuite/src/JSONTest.h b/JSON/testsuite/src/JSONTest.h index b651cbf65..858197ae2 100644 --- a/JSON/testsuite/src/JSONTest.h +++ b/JSON/testsuite/src/JSONTest.h @@ -46,6 +46,7 @@ public: JSONTest(const std::string& name); ~JSONTest(); + void testStringifier(); void testNullProperty(); void testTrueProperty(); void testFalseProperty();