open-source-parsers / open-source-parsers/jsoncpp
isDouble() returns true for integer number.
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 8.9k
- Forks
- 2.7k
- Avg merge
- 31m
- Merged PRs (30d)
- 1
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproducer and assertions in src/test_lib_json/main.cpp, then trace the Json::Value type predicates involved in integer, double, and array values. Run the relevant CMake tests and verify that integer values report isIntegral() true and isDouble() false, while fractional values retain the expected double behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100