diff --git a/NEWS.txt b/NEWS.txt index 250b730..ded333f 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -38,6 +38,10 @@ array.append( 1234 ); int value = array[0].asInt(); // did not compile previously + - Added float Json::Value::asFloat() to obtain a floating point value as a + float (avoid lost of precision warning caused by used of asDouble() + to initialize a float). + * Tests - Added test to ensure that the escape sequence "\/" is corrected handled diff --git a/include/json/value.h b/include/json/value.h index e9632a2..8d0d4c1 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -240,6 +240,7 @@ namespace Json { # endif Int asInt() const; UInt asUInt() const; + float asFloat() const; double asDouble() const; bool asBool() const; diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index a7b7328..15a1140 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -772,6 +772,35 @@ Value::asDouble() const return 0; // unreachable; } +float +Value::asFloat() const +{ + switch ( type_ ) + { + case nullValue: + return 0.0f; + case intValue: + return static_cast( value_.int_ ); + case uintValue: +#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return static_cast( value_.uint_ ); +#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return static_cast( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1); +#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + case realValue: + return static_cast( value_.real_ ); + case booleanValue: + return value_.bool_ ? 1.0f : 0.0f; + case stringValue: + case arrayValue: + case objectValue: + JSON_ASSERT_MESSAGE( false, "Type is not convertible to float" ); + default: + JSON_ASSERT_UNREACHABLE; + } + return 0.0f; // unreachable; +} + bool Value::asBool() const { diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 30d039c..de64200 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -28,6 +28,7 @@ struct ValueTest : JsonTest::TestCase Json::Value unsignedInteger_; Json::Value smallUnsignedInteger_; Json::Value real_; + Json::Value float_; Json::Value array1_; Json::Value object1_; Json::Value emptyString_; @@ -43,6 +44,7 @@ struct ValueTest : JsonTest::TestCase , smallUnsignedInteger_( Json::Value::UInt( Json::Value::maxInt ) ) , unsignedInteger_( 34567890u ) , real_( 1234.56789 ) + , float_( 0.00390625f ) , emptyString_( "" ) , string1_( "a" ) , string_( "sometext with space" ) @@ -184,6 +186,11 @@ JSONTEST_FIXTURE( ValueTest, accessArray ) } +JSONTEST_FIXTURE( ValueTest, asFloat ) +{ + JSONTEST_ASSERT_EQUAL( 0.00390625f, float_.asFloat() ) << "Json::Value::asFloat()"; +} + void ValueTest::checkConstMemberCount( const Json::Value &value, unsigned int expectedCount ) { @@ -259,5 +266,6 @@ int main( int argc, const char *argv[] ) JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, accessArray ); + JSONTEST_REGISTER_FIXTURE( runner, ValueTest, asFloat ); return runner.runCommandLine( argc, argv ); }