[SOLVED] QJsonValue has wrong number type?
-
Qt Doc says:
@The QJsonValue class encapsulates a value in JSON.A value in JSON can be one of 6 basic types:
JSON is a format to store structured data. It has 6 basic data types:
bool QJsonValue::Bool
double QJsonValue::Double
string QJsonValue::String
array QJsonValue::Array
object QJsonValue::Object
null QJsonValue::Null
@Why its number type is double? What about other number types like int, long, long long etc... ?
JSON RFC tells nothing about double!
bq. A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.
The simple JSON text: { "id" : 13587828937364 } turns to unusable QJsonValue(double, 1.35878e+13) how should I convert it to long? simple casting doesn't work of course..
Qt5 has so many BUG's and it is so unstable.. it just can't be called "release" version.. no way... beta, - maximum...
-
There is not just a SINGLE number type in JSON. There is nothing said about "single type" of number. Number can be int, long and double and it lays on implementator how to separate this number types.
JavaScript is not C++, this is clear like 2+2. You can't compare number type usage implementation in JavaScript and in C/C++. It is simply different, so it needs different implementation.
Or you want to say what parsing 13587828937364 to 1.35878e+13 is correct? It is not! And it is work in JavaScript, Java and C# JSON implementation!
-
bq. How would you express rational numbers when not using double (and an integer type instead), as there is just a single number type in JSON.
Didn't saw you have updated your post...
But one more time, nowhere is said you should use only one type in implementation! If it is needed(and it is needed in C/C++) you can parse number type in long type or in double! On output you will in any way parse whole types in plain text JSON structure! There is absolutely no prohibition to do what at all! -
If the standard allows for multiple internal storage types the implementation in Qt should definitly reflect this behaviour. -Please file a "bug report":https://bugreports.qt-project.org/.-
There already is https://bugreports.qt-project.org/browse/QTBUG-28560
-
Yep, voted already....
-
I have looked into the QJson code and found what the separation between numbers with and without floating point is already there(in private classes), but it is not exposed and used in the QJsonValue class.
So I have simple changed "Double" enum type to "Number"(like it actually should be) enum type, added toLongLong, isDouble functions, some other check states and now it works like it should and you can still directly use toDouble without checking if it is a double or not.
Maybe it is a good idea to change this in Qt5 source itself?
patch can be downloaded from here: "Qt5 JSON patch":http://vip2006.net/json.patch or from here "Qt5 JSON patch":http://xplatforms.de/json.patch -
Of course it is! ;-)
But be aware that the QtProject does not accept any patches not provided through "Gerrit":https://codereview.qt-project.org/ (for legal reasons).