Skip to content

Commit b96aed0

Browse files
committed
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).
1 parent fa130ef commit b96aed0

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

NEWS.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
array.append( 1234 );
3939
int value = array[0].asInt(); // did not compile previously
4040

41+
- Added float Json::Value::asFloat() to obtain a floating point value as a
42+
float (avoid lost of precision warning caused by used of asDouble()
43+
to initialize a float).
44+
4145
* Tests
4246

4347
- Added test to ensure that the escape sequence "\/" is corrected handled

include/json/value.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ namespace Json {
240240
# endif
241241
Int asInt() const;
242242
UInt asUInt() const;
243+
float asFloat() const;
243244
double asDouble() const;
244245
bool asBool() const;
245246

src/lib_json/json_value.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,35 @@ Value::asDouble() const
772772
return 0; // unreachable;
773773
}
774774

775+
float
776+
Value::asFloat() const
777+
{
778+
switch ( type_ )
779+
{
780+
case nullValue:
781+
return 0.0f;
782+
case intValue:
783+
return static_cast<float>( value_.int_ );
784+
case uintValue:
785+
#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
786+
return static_cast<float>( value_.uint_ );
787+
#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
788+
return static_cast<float>( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1);
789+
#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
790+
case realValue:
791+
return static_cast<float>( value_.real_ );
792+
case booleanValue:
793+
return value_.bool_ ? 1.0f : 0.0f;
794+
case stringValue:
795+
case arrayValue:
796+
case objectValue:
797+
JSON_ASSERT_MESSAGE( false, "Type is not convertible to float" );
798+
default:
799+
JSON_ASSERT_UNREACHABLE;
800+
}
801+
return 0.0f; // unreachable;
802+
}
803+
775804
bool
776805
Value::asBool() const
777806
{

src/test_lib_json/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct ValueTest : JsonTest::TestCase
2828
Json::Value unsignedInteger_;
2929
Json::Value smallUnsignedInteger_;
3030
Json::Value real_;
31+
Json::Value float_;
3132
Json::Value array1_;
3233
Json::Value object1_;
3334
Json::Value emptyString_;
@@ -43,6 +44,7 @@ struct ValueTest : JsonTest::TestCase
4344
, smallUnsignedInteger_( Json::Value::UInt( Json::Value::maxInt ) )
4445
, unsignedInteger_( 34567890u )
4546
, real_( 1234.56789 )
47+
, float_( 0.00390625f )
4648
, emptyString_( "" )
4749
, string1_( "a" )
4850
, string_( "sometext with space" )
@@ -184,6 +186,11 @@ JSONTEST_FIXTURE( ValueTest, accessArray )
184186
}
185187

186188

189+
JSONTEST_FIXTURE( ValueTest, asFloat )
190+
{
191+
JSONTEST_ASSERT_EQUAL( 0.00390625f, float_.asFloat() ) << "Json::Value::asFloat()";
192+
}
193+
187194
void
188195
ValueTest::checkConstMemberCount( const Json::Value &value, unsigned int expectedCount )
189196
{
@@ -259,5 +266,6 @@ int main( int argc, const char *argv[] )
259266
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull );
260267
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, isNull );
261268
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, accessArray );
269+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, asFloat );
262270
return runner.runCommandLine( argc, argv );
263271
}

0 commit comments

Comments
 (0)