CBOR-to-JSON: print integers with full precision#292
Conversation
Instead of performing a lossy conversion to double and printing that. It's irrelevant whether the parser on the other side can store this precision, only that it can parse this. That includes numbers outside the range of int64_t, which CBOR does support. We do this by simply removing code from cbortojson.c and instead just relying on what cborpretty.c already has. Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
| QTest::newRow("-2^63") << raw("\x3b\x7f\xff\xff\xff""\xff\xff\xff\xff") << "-9223372036854775808"; | ||
| QTest::newRow("-2^63-1") << raw("\x3b\x80\0\0\0""\0\0\0\0") << "-9223372036854775809"; | ||
| QTest::newRow("-2^64+1") << raw("\x3b\xff\xff\xff\xff""\xff\xff\xff\xfe") << "-18446744073709551615"; | ||
| QTest::newRow("-2^64") << raw("\x3b\xff\xff\xff\xff""\xff\xff\xff\xff") << "-18446744073709551616"; |
There was a problem hiding this comment.
I am not sure I understand the two test cases.
QTest::newRow("2^64-1") << raw("\x1b\xff\xff\xff\xff""\xff\xff\xff\xff") << "18446744073709551615";
and
QTest::newRow("-2^64+1") << raw("\x3b\xff\xff\xff\xff""\xff\xff\xff\xfe") << "-18446744073709551615";
QTest::newRow("-2^64") << raw("\x3b\xff\xff\xff\xff""\xff\xff\xff\xff") << "-18446744073709551616";both of these are larger than the largest int64_t. While uint64_t can hold 2^64-1 it cannot hold the negative value -2^64
I don't remember how cbor handles integers larger than int64 is this just verifying it can handle values that exceed int64? Just asking since those values stood out as possible edge cases.
There was a problem hiding this comment.
nevermind, I was wrong on the limits of int64. I found in the spec that cbor supports limits 2^-64 .. 2^64-1.
anything larger required Bignums Tag which this is not testing.
There was a problem hiding this comment.
You're right that int64_t goes from -2^-63 to 2^63-1. But CBOR integers have one extra bit which gets encoded in the major type, so they run from -2^64 to 2^64-1.
Instead of performing a lossy conversion to double and printing that. It's irrelevant whether the parser on the other side can store this precision, only that it can parse this. That includes numbers outside the range of int64_t, which CBOR does support.
We do this by simply removing code from cbortojson.c and instead just relying on what cborpretty.c already has.