Added JsonObject as thin wrapper around the cJSON source
This commit is contained in:
8
opm/json/CMakeLists.txt
Normal file
8
opm/json/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
add_subdirectory( tests )
|
||||
|
||||
enable_language( C )
|
||||
set( json_source JsonObject.cpp cjson/cJSON.c )
|
||||
|
||||
add_library(opm-json ${json_source})
|
||||
|
||||
|
||||
112
opm/json/JsonObject.cpp
Normal file
112
opm/json/JsonObject.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
Copyright 2013 Statoil ASA.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <opm/json/JsonObject.hpp>
|
||||
#include <opm/json/cjson/cJSON.h>
|
||||
|
||||
namespace Json {
|
||||
|
||||
void JsonObject::initialize(const std::string& inline_json) {
|
||||
root = cJSON_Parse( inline_json.c_str() );
|
||||
if (!root)
|
||||
throw std::invalid_argument("Parsing json input failed");
|
||||
owner = true;
|
||||
}
|
||||
|
||||
|
||||
JsonObject::JsonObject(const std::string& inline_json) {
|
||||
initialize( inline_json );
|
||||
}
|
||||
|
||||
|
||||
JsonObject::JsonObject(const boost::filesystem::path& jsonFile ) {
|
||||
std::ifstream stream(jsonFile.string().c_str());
|
||||
|
||||
if (stream) {
|
||||
std::string content( (std::istreambuf_iterator<char>(stream)),
|
||||
(std::istreambuf_iterator<char>()));
|
||||
std::cout << content;
|
||||
initialize( content );
|
||||
} else
|
||||
throw std::invalid_argument("Loading json from file: " + jsonFile.string() + " failed.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
JsonObject::JsonObject( cJSON * object ) {
|
||||
root = object;
|
||||
owner = false;
|
||||
}
|
||||
|
||||
|
||||
JsonObject::~JsonObject() {
|
||||
if (owner && root)
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool JsonObject::has_item( const std::string& key) const {
|
||||
cJSON * object = cJSON_GetObjectItem( root , key.c_str() );
|
||||
if (object)
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
size_t JsonObject::size() {
|
||||
int int_size = cJSON_GetArraySize( root );
|
||||
return (size_t) int_size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
JsonObject JsonObject::get_object(const std::string& key) {
|
||||
cJSON * object = cJSON_GetObjectItem( root , key.c_str() );
|
||||
if (object)
|
||||
return JsonObject( object );
|
||||
else
|
||||
throw std::invalid_argument("Key: " + key + " does not exist in json object");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
55
opm/json/JsonObject.hpp
Normal file
55
opm/json/JsonObject.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Copyright 2013 Statoil ASA.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef JSON_OBJECT_HPP
|
||||
#define JSON_OBJECT_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <opm/json/cjson/cJSON.h>
|
||||
|
||||
|
||||
namespace Json {
|
||||
|
||||
class JsonObject {
|
||||
public:
|
||||
JsonObject(const boost::filesystem::path& jsonFile );
|
||||
JsonObject(const std::string& inline_json);
|
||||
JsonObject(cJSON * root);
|
||||
~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();
|
||||
|
||||
private:
|
||||
void initialize(const std::string& inline_json);
|
||||
cJSON * root;
|
||||
bool owner;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
5
opm/json/tests/CMakeLists.txt
Normal file
5
opm/json/tests/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_executable( runjsonTests jsonTests.cpp )
|
||||
target_link_libraries( runjsonTests opm-json ${Boost_LIBRARIES})
|
||||
|
||||
set_source_files_properties( jsonTests.cpp PROPERTIES COMPILE_FLAGS "-Wno-unused-variable")
|
||||
add_test( NAME runjsonTests WORKING_DIRECTORY ${EXECUTABLE_OUPUT_PATH} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runjsonTests )
|
||||
112
opm/json/tests/jsonTests.cpp
Normal file
112
opm/json/tests/jsonTests.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
Copyright 2013 Statoil ASA.
|
||||
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#define BOOST_TEST_MODULE jsonParserTests
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/test_tools.hpp>
|
||||
|
||||
#include <opm/json/JsonObject.hpp>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseValidJson) {
|
||||
std::string inline_json = "{\"key\": \"value\"}";
|
||||
BOOST_CHECK_NO_THROW(Json::JsonObject parser(inline_json));
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseInvalidJSON_throw) {
|
||||
std::string inline_json = "{\"key\": \"value\"";
|
||||
BOOST_CHECK_THROW(Json::JsonObject parser(inline_json) , std::invalid_argument);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getValue) {
|
||||
std::string inline_json = "{\"key\": \"value\"}";
|
||||
Json::JsonObject parser(inline_json);
|
||||
BOOST_CHECK_EQUAL( "value" , parser.get_string("key") );
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParsevalidJSON_hasItem) {
|
||||
std::string inline_json = "{\"key\": \"value\"}";
|
||||
Json::JsonObject parser(inline_json);
|
||||
BOOST_CHECK( parser.has_item("key"));
|
||||
BOOST_CHECK_EQUAL( false , parser.has_item("keyX"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getMissingValue) {
|
||||
std::string inline_json = "{\"key\": \"value\"}";
|
||||
Json::JsonObject parser(inline_json);
|
||||
BOOST_CHECK_THROW( parser.get_string("keyX") , std::invalid_argument );
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParsevalidJSON_getNotScalar_throws) {
|
||||
std::string inline_json = "{\"key\": \"value\", \"list\": [1,2,3]}";
|
||||
Json::JsonObject parser(inline_json);
|
||||
BOOST_CHECK_EQUAL( "value" , parser.get_string("key"));
|
||||
BOOST_CHECK_THROW( parser.get_string("list") , std::invalid_argument );
|
||||
}
|
||||
|
||||
|
||||
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_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_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");
|
||||
BOOST_CHECK_EQUAL( 3U , object.size() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(Parse_fileDoesNotExist_Throws) {
|
||||
boost::filesystem::path jsonFile("file/does/not/exist");
|
||||
BOOST_CHECK_THROW( Json::JsonObject parser(jsonFile) , std::invalid_argument);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
2
testdata/json/example1.json
vendored
Normal file
2
testdata/json/example1.json
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
{"key" : "value" , "list": [1,2,3]}
|
||||
|
||||
Reference in New Issue
Block a user