diff --git a/opm/json/JsonObject.cpp b/opm/json/JsonObject.cpp index 94c74d624..5b4fd2b01 100644 --- a/opm/json/JsonObject.cpp +++ b/opm/json/JsonObject.cpp @@ -169,6 +169,20 @@ namespace Json { } + double JsonObject::get_double(const std::string& key) const { + JsonObject child = get_scalar_object( key ); + return child.as_double( ); + } + + + double JsonObject::as_double() const { + if (root->type == cJSON_Number) + return root->valuedouble; + 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()) diff --git a/opm/json/JsonObject.hpp b/opm/json/JsonObject.hpp index d7592bbbd..16586da0f 100644 --- a/opm/json/JsonObject.hpp +++ b/opm/json/JsonObject.hpp @@ -45,9 +45,11 @@ namespace Json { std::string as_string() const; bool is_string( ) const; - int get_int(const std::string& key) const; bool is_number( ) const; + int get_int(const std::string& key) const; int as_int() const; + double get_double(const std::string& key) const; + double as_double() const; bool is_array( ) const; bool is_object( ) const; diff --git a/opm/json/tests/jsonTests.cpp b/opm/json/tests/jsonTests.cpp index 653a70448..533c44687 100644 --- a/opm/json/tests/jsonTests.cpp +++ b/opm/json/tests/jsonTests.cpp @@ -18,6 +18,7 @@ */ #include +#include #define BOOST_TEST_MODULE jsonParserTests #include @@ -74,21 +75,36 @@ BOOST_AUTO_TEST_CASE(ParsevalidJSONnotString_asString_throws) { } -BOOST_AUTO_TEST_CASE(ParsevalidJSONint_asInt) { - std::string inline_json = "{\"key\": 100}"; +BOOST_AUTO_TEST_CASE(ParsevalidJSONint_asNumber) { + std::string inline_json = "{\"key1\": 100, \"key2\" : 100.100 }"; Json::JsonObject parser(inline_json); - Json::JsonObject value = parser.get_item("key"); + Json::JsonObject value1 = parser.get_item("key1"); + Json::JsonObject value2 = parser.get_item("key2"); - BOOST_CHECK_EQUAL( 100 , value.as_int() ); + BOOST_CHECK_EQUAL( 100 , value1.as_int() ); + BOOST_CHECK( fabs(100.100 - value2.as_double()) < 0.00001 ); +} + +BOOST_AUTO_TEST_CASE(ParsevalidJSONint_isNumber) { + std::string inline_json = "{\"key1\": 100, \"key2\" : 100.100 , \"key3\": \"string\"}"; + Json::JsonObject parser(inline_json); + Json::JsonObject value1 = parser.get_item("key1"); + Json::JsonObject value2 = parser.get_item("key2"); + Json::JsonObject value3 = parser.get_item("key3"); + + BOOST_CHECK( value1.is_number()) ; + BOOST_CHECK( value2.is_number()) ; + BOOST_CHECK_EQUAL( false , value3.is_number()) ; } -BOOST_AUTO_TEST_CASE(ParsevalidJSONnotint_asint_throws) { +BOOST_AUTO_TEST_CASE(ParsevalidJSONnotNumber_asNumber_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_CHECK_THROW( value.as_int() , std::invalid_argument ); + BOOST_CHECK_THROW( value.as_double() , std::invalid_argument ); }