Extended the JsonObject() class

This commit is contained in:
Joakim Hove
2013-07-30 14:08:42 +02:00
parent 66cbb1a5cb
commit 269686a2f5
3 changed files with 231 additions and 23 deletions

View File

@@ -53,8 +53,7 @@ namespace Json {
if (stream) {
std::string content( (std::istreambuf_iterator<char>(stream)),
(std::istreambuf_iterator<char>()));
std::cout << content;
(std::istreambuf_iterator<char>()));
initialize( content );
} else
throw std::invalid_argument("Loading json from file: " + jsonFile.string() + " failed.");
@@ -85,16 +84,33 @@ namespace Json {
}
bool JsonObject::is_array( ) const {
if (root->type == cJSON_Array)
return true;
else
return false;
}
std::string JsonObject::get_string(const std::string& key) {
cJSON * object = cJSON_GetObjectItem( root , key.c_str() );
if (object) {
if (cJSON_GetArraySize( object ))
throw std::invalid_argument("Key: " + key + " is not a scalar object");
else
return object->valuestring;
} else
throw std::invalid_argument("Key: " + key + " does not exist in json object");
bool JsonObject::is_number( ) const {
if (root->type == cJSON_Number)
return true;
else
return false;
}
bool JsonObject::is_string( ) const {
if (root->type == cJSON_String)
return true;
else
return false;
}
bool JsonObject::is_object( ) const {
if (root->type == cJSON_Object)
return true;
else
return false;
}
@@ -104,14 +120,65 @@ namespace Json {
}
JsonObject JsonObject::get_array_item( size_t index ) const {
if (is_array()) {
cJSON * new_c_ptr = cJSON_GetArrayItem( root , index );
if (new_c_ptr)
return JsonObject( new_c_ptr );
else
throw std::invalid_argument("Index is out ouf range.");
} else
throw std::invalid_argument("Object is not an array.");
}
JsonObject JsonObject::get_object(const std::string& key) {
cJSON * object = cJSON_GetObjectItem( root , key.c_str() );
if (object)
return JsonObject( object );
JsonObject JsonObject::get_item(const std::string& key) const {
cJSON * c_ptr = cJSON_GetObjectItem( root , key.c_str() );
if (c_ptr)
return JsonObject( c_ptr );
else
throw std::invalid_argument("Key: " + key + " does not exist in json object");
}
std::string JsonObject::get_string(const std::string& key) const {
JsonObject child = get_scalar_object( key );
return child.as_string();
}
std::string JsonObject::as_string() const {
if (is_string())
return root->valuestring;
else
throw std::invalid_argument("Object is not a string object");
}
int JsonObject::get_int(const std::string& key) const {
JsonObject child = get_scalar_object( key );
return child.as_int( );
}
int JsonObject::as_int() const {
if (root->type == cJSON_Number)
return root->valueint;
else
throw std::invalid_argument("Object is not a number object.");
}
JsonObject JsonObject::get_scalar_object(const std::string& key) const{
JsonObject child = get_item( key );
if (child.size())
throw std::invalid_argument("Key: " + key + " is not a scalar object");
else
return child;
}
}

View File

@@ -38,11 +38,23 @@ namespace Json {
~JsonObject();
bool has_item(const std::string& key) const;
JsonObject get_object(const std::string& key);
std::string get_string(const std::string& key);
size_t size();
JsonObject get_array_item( size_t index ) const;
JsonObject get_item(const std::string& key) const;
std::string get_string(const std::string& key) const;
std::string as_string() const;
bool is_string( ) const;
int get_int(const std::string& key) const;
bool is_number( ) const;
int as_int() const;
bool is_array( ) const;
bool is_object( ) const;
size_t size();
private:
JsonObject get_scalar_object(const std::string& key) const;
void initialize(const std::string& inline_json);
cJSON * root;
bool owner;

View File

@@ -35,6 +35,11 @@ BOOST_AUTO_TEST_CASE(ParseValidJson) {
}
BOOST_AUTO_TEST_CASE(ParseValidJson_fromLiteral) {
BOOST_CHECK_NO_THROW(Json::JsonObject parser("{\"key\": \"value\"}"));
}
BOOST_AUTO_TEST_CASE(ParseInvalidJSON_throw) {
std::string inline_json = "{\"key\": \"value\"";
@@ -43,13 +48,60 @@ BOOST_AUTO_TEST_CASE(ParseInvalidJSON_throw) {
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getValue) {
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getString) {
std::string inline_json = "{\"key\": \"value\"}";
Json::JsonObject parser(inline_json);
BOOST_CHECK_EQUAL( "value" , parser.get_string("key") );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSONString_asString) {
std::string inline_json = "{\"key\": \"value\"}";
Json::JsonObject parser(inline_json);
Json::JsonObject value = parser.get_item("key");
BOOST_CHECK_EQUAL( "value" , value.as_string() );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSONnotString_asString_throws) {
std::string inline_json = "{\"key\": 100}";
Json::JsonObject parser(inline_json);
Json::JsonObject value = parser.get_item("key");
BOOST_CHECK_THROW( value.as_string() , std::invalid_argument );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSONint_asInt) {
std::string inline_json = "{\"key\": 100}";
Json::JsonObject parser(inline_json);
Json::JsonObject value = parser.get_item("key");
BOOST_CHECK_EQUAL( 100 , value.as_int() );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSONnotint_asint_throws) {
std::string inline_json = "{\"key\": \"100X\"}";
Json::JsonObject parser(inline_json);
Json::JsonObject value = parser.get_item("key");
BOOST_CHECK_THROW( value.as_int() , std::invalid_argument );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getInt_OK) {
std::string inline_json = "{\"key1\": 100 , \"key2\" : 200}";
Json::JsonObject parser(inline_json);
BOOST_CHECK_EQUAL( 100 , parser.get_int("key1") );
BOOST_CHECK_EQUAL( 200 , parser.get_int("key2") );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSON_hasItem) {
std::string inline_json = "{\"key\": \"value\"}";
Json::JsonObject parser(inline_json);
@@ -77,15 +129,15 @@ BOOST_AUTO_TEST_CASE(ParsevalidJSON_getNotScalar_throws) {
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getObject) {
std::string inline_json = "{\"key\": \"value\", \"list\": [1,2,3]}";
Json::JsonObject parser(inline_json);
BOOST_CHECK_NO_THROW( Json::JsonObject object = parser.get_object("list") );
BOOST_CHECK_NO_THROW( Json::JsonObject object = parser.get_object("key") );
BOOST_CHECK_NO_THROW( Json::JsonObject object = parser.get_item("list") );
BOOST_CHECK_NO_THROW( Json::JsonObject object = parser.get_item("key") );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getObject_missing_throw) {
std::string inline_json = "{\"key\": \"value\", \"list\": [1,2,3]}";
Json::JsonObject parser(inline_json);
BOOST_CHECK_THROW( parser.get_object("listX") , std::invalid_argument );
BOOST_CHECK_THROW( parser.get_item("listX") , std::invalid_argument );
}
@@ -93,11 +145,85 @@ BOOST_AUTO_TEST_CASE(ParsevalidJSON_getObject_missing_throw) {
BOOST_AUTO_TEST_CASE(ParsevalidJSON_CheckArraySize) {
std::string inline_json = "{\"key\": \"value\", \"list\": [1,2,3]}";
Json::JsonObject parser(inline_json);
Json::JsonObject object = parser.get_object("list");
Json::JsonObject object = parser.get_item("list");
BOOST_CHECK_EQUAL( 3U , object.size() );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSON_isArray){
std::string inline_json = "{\"key\": \"value\", \"list\": [1,2,3]}";
Json::JsonObject parser(inline_json);
Json::JsonObject list = parser.get_item("list");
Json::JsonObject key = parser.get_item("key");
BOOST_CHECK( list.is_array() );
BOOST_CHECK_EQUAL( false , key.is_array( ) );
}
BOOST_AUTO_TEST_CASE(ParsevalidJSON_arrayGet) {
std::string inline_json = "{\"key\": \"value\", \"list\": [1,2,3]}";
Json::JsonObject parser(inline_json);
Json::JsonObject list = parser.get_item("list");
Json::JsonObject key = parser.get_item("key");
BOOST_CHECK_NO_THROW( list.get_array_item( 0U ));
BOOST_CHECK_NO_THROW( list.get_array_item( 1U ));
BOOST_CHECK_NO_THROW( list.get_array_item( 2U ));
BOOST_CHECK_THROW( list.get_array_item( 3U ) , std::invalid_argument );
BOOST_CHECK_THROW( key.get_array_item( 0U ) , std::invalid_argument );
}
BOOST_AUTO_TEST_CASE(parseJSONString_testType) {
std::string inline_json = "{\"item\": \"string\"}";
Json::JsonObject json(inline_json);
Json::JsonObject item = json.get_item( "item" );
BOOST_CHECK( item.is_string() );
BOOST_CHECK_EQUAL( false , item.is_number( ) );
BOOST_CHECK_EQUAL( false , item.is_array( ) );
BOOST_CHECK_EQUAL( false , item.is_object( ) );
}
BOOST_AUTO_TEST_CASE(parseJSONNumber_testType) {
std::string inline_json = "{\"item\": 100}";
Json::JsonObject json(inline_json);
Json::JsonObject item = json.get_item( "item" );
BOOST_CHECK_EQUAL( true , item.is_number( ) );
BOOST_CHECK_EQUAL( false , item.is_string() );
BOOST_CHECK_EQUAL( false , item.is_array( ) );
BOOST_CHECK_EQUAL( false , item.is_object( ) );
}
BOOST_AUTO_TEST_CASE(parseJSONArray_testType) {
std::string inline_json = "{\"item\": [1,2,3]}";
Json::JsonObject json(inline_json);
Json::JsonObject item = json.get_item( "item" );
BOOST_CHECK_EQUAL( false , item.is_number( ) );
BOOST_CHECK_EQUAL( false , item.is_string() );
BOOST_CHECK_EQUAL( true , item.is_array( ) );
BOOST_CHECK_EQUAL( false , item.is_object( ) );
}
BOOST_AUTO_TEST_CASE(parseJSONObject_testType) {
std::string inline_json = "{\"item\": {\"list\": [0,1,2]}}";
Json::JsonObject json(inline_json);
Json::JsonObject item = json.get_item( "item" );
BOOST_CHECK_EQUAL( false , item.is_number( ) );
BOOST_CHECK_EQUAL( false , item.is_string() );
BOOST_CHECK_EQUAL( false , item.is_array( ) );
BOOST_CHECK_EQUAL( true , item.is_object( ) );
}
BOOST_AUTO_TEST_CASE(Parse_fileDoesNotExist_Throws) {
boost::filesystem::path jsonFile("file/does/not/exist");
@@ -110,3 +236,6 @@ BOOST_AUTO_TEST_CASE(Parse_fileExists_OK) {
boost::filesystem::path jsonFile("testdata/json/example1.json");
BOOST_CHECK_THROW( Json::JsonObject parser(jsonFile) , std::invalid_argument);
}