GH31: JSON implementation bug

fixed GH #31: JSON implementation bug
This commit is contained in:
aleks-f
2012-12-11 20:27:32 -06:00
parent e21d1f5fcc
commit 1b14088283
5 changed files with 41 additions and 6 deletions

View File

@@ -161,7 +161,7 @@ inline bool Object::has(const std::string& key) const
inline bool Object::isArray(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
return it != _values.end() || it->second.type() == typeid(Array::Ptr);
return it != _values.end() && it->second.type() == typeid(Array::Ptr);
}
@@ -175,7 +175,7 @@ inline bool Object::isNull(const std::string& key) const
inline bool Object::isObject(const std::string& key) const
{
ValueMap::const_iterator it = _values.find(key);
return it != _values.end() || it->second.type() == typeid(Object::Ptr);
return it != _values.end() && it->second.type() == typeid(Object::Ptr);
}

View File

@@ -366,8 +366,11 @@ void JSONTest::testObjectProperty()
}
assert(result.type() == typeid(Object::Ptr));
Object::Ptr object = result.extract<Object::Ptr>();
assert (object->isObject("test"));
assert (!object->isArray("test"));
Var test = object->get("test");
assert(test.type() == typeid(Object::Ptr));
object = test.extract<Object::Ptr>();
@@ -379,6 +382,36 @@ void JSONTest::testObjectProperty()
}
void JSONTest::testObjectArray()
{
std::string json = "{ \"test\" : { \"test1\" : [1, 2, 3], \"test2\" : 4 } }";
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));
Object::Ptr object = result.extract<Object::Ptr>();
assert(object->isObject("test"));
object = object->getObject("test");
assert(!object->isObject("test1"));
assert(object->isArray("test1"));
assert(!object->isObject("test2"));
assert(!object->isArray("test2"));
}
void JSONTest::testEmptyArray()
{
std::string json = "[]";
@@ -833,6 +866,7 @@ CppUnit::Test* JSONTest::suite()
CppUnit_addTest(pSuite, JSONTest, testDouble2Property);
CppUnit_addTest(pSuite, JSONTest, testDouble3Property);
CppUnit_addTest(pSuite, JSONTest, testObjectProperty);
CppUnit_addTest(pSuite, JSONTest, testObjectArray);
CppUnit_addTest(pSuite, JSONTest, testEmptyArray);
CppUnit_addTest(pSuite, JSONTest, testNestedArray);
CppUnit_addTest(pSuite, JSONTest, testNullElement);

View File

@@ -57,6 +57,7 @@ public:
void testDouble2Property();
void testDouble3Property();
void testObjectProperty();
void testObjectArray();
void testEmptyArray();
void testNestedArray();
void testNullElement();