Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt : from unsigned long long to QJsonObject

Is it possible to use long long as a value at QJsonObject ? I was forced to change my API from JSON to XML because 1 field I got had BigInt values and aparently I can't extract big numbers from QJsonValue.

Here's my peace of code that may show what is going on:

QJsonObject json;

unsigned long long ulongmax = ULONG_LONG_MAX;

QVariant variant = ulongmax;

qDebug() << variant;
qDebug() << ulongmax;

json.insert( "key", QJsonValue::fromVariant( variant ) );

unsigned long long json_value = json.value("key").toVariant().toULongLong();

qDebug() << json_value;

Output:

QVariant(qulonglong, 18446744073709551615)
18446744073709551615
9223372036854775808

Desired output:

QVariant(qulonglong, 18446744073709551615)
18446744073709551615
18446744073709551615

Am I doing anything wrong? Can anyone help me find out how to make it work properly without external libs? Thank you!

like image 455
Rafael Fontes Avatar asked Oct 14 '25 18:10

Rafael Fontes


1 Answers

My solution to this problem is as simple as to write JSON strings instead of JSON numbers:

  • QString str = QString::number(myLongLong); // then, write str as JSON string
  • qlonglong myLongLong = json["key"].toString().toLongLong(); // convert JSON string to long long

It may make sense to check for errors in the conversion, see the API documentation of the provided links.

A potential problem is that numbers in JSON do not require quotes. So it may be that you have to convert your JSON files first to comply to this string convention.

like image 137
dhaumann Avatar answered Oct 17 '25 09:10

dhaumann