Skip to content

Commit 42d963a

Browse files
committed
GH 23: JSON::Object::stringify throw BadCastException
GH issue pocoproject#23 : JSON::Object::stringify throw BadCastException
1 parent 0c4d259 commit 42d963a

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

JSON/src/Stringifier.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ namespace JSON {
4949

5050
void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent)
5151
{
52-
if ( any.type() == typeid(Object::Ptr) )
52+
if ( any.type() == typeid(Object) )
53+
{
54+
const Object& o = any.extract<Object>();
55+
o.stringify(out, indent == 0 ? 0 : indent + 2);
56+
}
57+
else if ( any.type() == typeid(Array) )
58+
{
59+
const Array& a = any.extract<Array>();
60+
a.stringify(out, indent == 0 ? 0 : indent + 2);
61+
}
62+
else if ( any.type() == typeid(Object::Ptr) )
5363
{
5464
const Object::Ptr& o = any.extract<Object::Ptr>();
5565
o->stringify(out, indent == 0 ? 0 : indent + 2);

JSON/testsuite/src/JSONTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ void JSONTest::tearDown()
7474
}
7575

7676

77+
void JSONTest::testStringifier()
78+
{
79+
Object obj;
80+
81+
Array arr;
82+
Object obj2;
83+
84+
obj.set("array", arr);
85+
obj.set("obj2", obj2);
86+
87+
std::ostringstream ostr;
88+
obj.stringify(ostr);
89+
assert (ostr.str() == "{\"array\":[],\"obj2\":{}}");
90+
}
91+
92+
7793
void JSONTest::testNullProperty()
7894
{
7995
std::string json = "{ \"test\" : null }";
@@ -806,6 +822,7 @@ CppUnit::Test* JSONTest::suite()
806822
{
807823
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONTest");
808824

825+
CppUnit_addTest(pSuite, JSONTest, testStringifier);
809826
CppUnit_addTest(pSuite, JSONTest, testNullProperty);
810827
CppUnit_addTest(pSuite, JSONTest, testTrueProperty);
811828
CppUnit_addTest(pSuite, JSONTest, testFalseProperty);

JSON/testsuite/src/JSONTest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class JSONTest: public CppUnit::TestCase
4646
JSONTest(const std::string& name);
4747
~JSONTest();
4848

49+
void testStringifier();
4950
void testNullProperty();
5051
void testTrueProperty();
5152
void testFalseProperty();

0 commit comments

Comments
 (0)