Skip to content

isDouble() returns true for integer number. #1361

Open
@quezadaminter

Description

@quezadaminter

Describe the bug
When integer numbers are used to instantiate a Json::Value object the query for isDouble() on that object returns true.

To Reproduce
I modified a portion of the src/test_lib_json/main.cpp file to force the testing. But this can be easily reproduced with simple code such as:

int main(int argc, char *argv[]) { int v(1234); Json::Value jv(v); if(jv.isDouble() == true) { } }

JSONTEST_ASSERT_EQUAL(Json::Value(1234).isIntegral(), true); //<--- PASSES
JSONTEST_ASSERT_EQUAL(Json::Value(1234).isDouble(), false); //<--- FAILS (320)
JSONTEST_ASSERT_EQUAL(Json::Value(1234).isIntegral(), false); //<--- FAILS (321)
JSONTEST_ASSERT_EQUAL(Json::Value(1234).isDouble(), true); //<--- PASSES

int v(1234);
JSONTEST_ASSERT_EQUAL(Json::Value(v).isIntegral(), true); //<--- PASSES
JSONTEST_ASSERT_EQUAL(Json::Value(v).isDouble(), false); //<--- FAILS (326)
JSONTEST_ASSERT_EQUAL(Json::Value(v).isIntegral(), false); //<--- FAILS (327)
JSONTEST_ASSERT_EQUAL(Json::Value(v).isDouble(), true); //<--- PASSES

std::cout << "With Double" << std::endl;
JSONTEST_ASSERT_EQUAL(Json::Value(1234.56).isIntegral(), false);//<--- PASSES
JSONTEST_ASSERT_EQUAL(Json::Value(1234.56).isDouble(), true); //<--- PASSES
JSONTEST_ASSERT_EQUAL(Json::Value(1234.56).isIntegral(), true); //<--- FAILS (333)
JSONTEST_ASSERT_EQUAL(Json::Value(1234.56).isDouble(), true); //<--- PASSES

Json::Value array1_;
array1_.append(1234);
JSONTEST_ASSERT_EQUAL(array1_[0].isIntegral(), true); //<--- PASSES
JSONTEST_ASSERT_EQUAL(array1_[0].isDouble(), false); //<--- FAILS (337)

Results
src/test_lib_json/main.cpp(320): Json::Value(1234).isDouble() == false
Expected: true
Actual : false
src/test_lib_json/main.cpp(321): Json::Value(1234).isIntegral() == false
Expected: true
Actual : false
src/test_lib_json/main.cpp(326): Json::Value(v).isDouble() == false
Expected: true
Actual : false
src/test_lib_json/main.cpp(327): Json::Value(v).isIntegral() == false
Expected: true
Actual : false
src/test_lib_json/main.cpp(333): Json::Value(1234.56).isIntegral() == true
Expected: false
Actual : true
src/test_lib_json/main.cpp(337): array1_[0].isDouble() == false
Expected: true
Actual : false

Expected behavior
If the number added is an integer then identifying it as a double should fail.

Desktop (please complete the following information):
OS - Rocky Linux
Version - 1.9.5 built with CMake.

Additional context
I am using the jsoncpp library to encode and decode information. When the data is decoded it is imperative that we know the type since our use case has specific logic that runs for different data types. As such, our tests fail when an integer value is encoded with jsoncpp and returns true when queried with isDouble() on the receiving end.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions