3

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!

Rafael Fontes
  • 1,195
  • 11
  • 19

1 Answers1

7

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.

dhaumann
  • 1,590
  • 13
  • 25
  • It's probabbly because I did not express properlly, but my problem is parsing a JSON from a webservice that I do not own. I can't change the way it's built. Should I ask them to? I think I will. – Rafael Fontes Sep 10 '15 at 11:19
  • 1
    Does the webservice write longlong numbers in JSON? That would already be kind of against the standard, as I understand. The question is: Are number written with or without quotes? It's probably a good idea to ask to just write them as string in this particular case. This gives more power to how the numbers look like. – dhaumann Sep 10 '15 at 11:46