mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-01-14 01:47:54 +08:00
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).
This commit is contained in:
parent
fa130ef871
commit
b96aed0f3e
4
NEWS.txt
4
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
|
||||
|
@ -240,6 +240,7 @@ namespace Json {
|
||||
# endif
|
||||
Int asInt() const;
|
||||
UInt asUInt() const;
|
||||
float asFloat() const;
|
||||
double asDouble() const;
|
||||
bool asBool() const;
|
||||
|
||||
|
@ -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<float>( value_.int_ );
|
||||
case uintValue:
|
||||
#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
|
||||
return static_cast<float>( value_.uint_ );
|
||||
#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
|
||||
return static_cast<float>( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1);
|
||||
#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
|
||||
case realValue:
|
||||
return static_cast<float>( 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
|
||||
{
|
||||
|
@ -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 );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user