PDA

View Full Version : qjson int64 range



oop
18th February 2016, 16:25
Hello all,
I am running ina trouble, I need to work with int64 but I did not found a solution except to use they like strings.
In the following program the statement 'Doc.object()["ID"].toDouble();' return very close value but not exact, in my environment.
There is some way to extract the entire number without use the string form ("IDs":"635913899220803700") ?






#include <QtCore/QCoreApplication>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>

int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);

QString message=R"RR({"ID":635913899220803700,"IDs":"635913899220803700"})RR";
QJsonParseError err;

QJsonDocument Doc=QJsonDocument::fromJson(message.toUtf8(),&err);

unsigned long long ia=Doc.object()["ID"].toDouble();
unsigned long long ib=_atoi64(Doc.object()["IDs"].toString().toUtf8().data());
_ASSERT(ia==ib);

return a.exec();
}




Cheers,
Giorgio

anda_skoa
18th February 2016, 16:38
Call toString() on the QJsonValue and then toLongLong() on the QString.

Cheers,
_

oop
18th February 2016, 16:46
Hi,
I tried similar solution but seems that the resulting QString is empty.


unsigned long long ib=Doc.object()["ID"].toString().toLongLong(); // ib is 0


Or also:
QString sval=Doc.object()["ID"].toString();
unsigned long long ib=sval.toLongLong();


There is some think that escape me?

Giorgio

anda_skoa
18th February 2016, 17:07
So it seems that the value returned from object()["ID"] is a JavaScript Number, which is a double precision floating point value.

Since you need integer, you'll have to make the value a string. then you can extract the string and parse as a C++ int64.

Cheers,
_

oop
18th February 2016, 17:29
Yes, I seen that I need to use the string form (IDs as above code) in order to extract the int64 number.
I found other info around internet about JsonValue.


Thank you.
Giorgio