Search code examples
c++jsonqtqvariantlong-long

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!


Solution

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

    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.