QJsonDocument :: toJson () generates very large text. Is there a way to fix this?
I made a class that converts QList<qreal>
(very large list) to JSON string but generates very large text.
Here is the code:
QJsonObject rootObject;
rootObject.insert("Test",0.05);
qDebug()<<QJsonDocument(rootObject).toJson(QJsonDocument::Compact);
And I tried equal code:
QJsonObject rootObject;
rootObject.insert("Test",QString("0.05").toDouble());
qDebug()<<QJsonDocument(rootObject).toJson(QJsonDocument::Compact);
And debug ouptut is always:
{"Test":0.050000000000000003}
I want to get short output like this:
{"Test":0.05}
Is there a way to fix it QJsonDocument
? Or do multiple decimal places with rounding / limit?
source to share
On OS X I have tried the following: -
QVariant d(0.5);
QJsonValue val = QJsonValue::fromVariant(d);
qDebug() << val.toDouble();
Will print 0.5 as expected.
However, I think the problem is with floating point precision . QJSonDocument represents a number as accurately as possible, but does not have a function to limit the number of decimal places presented, as QString does .
While not ideal, if you really want it to be 0.5, you can write a string value instead of a double.
source to share