1

I'm reading, parsing and writing back a JSON file in a QT project. Part of the requirements is that all entry's should be the same written out as declared in the source file.

One of the entries looks somewhat like this:

"SomeVal": 1.23141241242140

When I read the JSON object, the last zero (0) is removed. That does make sense, since it is not needed for normal use cases. But, since I need to preserve the whole correct number, including that last zero, this is incorrect for me. Any clue on how I can bypass this problem?

The code that reads the JSON file:

QString rawJson = "";
QFile jsonFile(filePath);
if(jsonFile.exists())
{
    jsonFile.open(QFile::ReadOnly|QFile::Text);
    rawJson = jsonFile.readAll();
    jsonFile.close();
}
else
{
    return false;
}

QJsonParseError json_parse_error;
QJsonDocument json_doc = QJsonDocument::fromJson(rawJson.toUtf8(), &json_parse_error);
if(json_parse_error.error != QJsonParseError::NoError)
{
    emit SignalMessageCritical(QString(json_parse_error.errorString()));
    return false;
}
this->jsonTestFile = json_doc.object();
scopchanov
  • 7,966
  • 10
  • 40
  • 68
Mathlight
  • 6,436
  • 17
  • 62
  • 107
  • 1
    I think you will be out of luck. The JSON standard doesn't require particular formatting for numbers, and there doesn't seem to be a way of influencing how `QJsonDocument` formats numbers – Caleth Dec 10 '20 at 12:55
  • 1
    Could you save it as a string and then restore it back to real? – scopchanov Dec 10 '20 at 12:58
  • I was afraid it wasn't possible.. I think I can write my own "parser" for the given files and do some magic with strings and reals indeed, but was hoping that there was some kind of flag I could set which I didn't found. – Mathlight Dec 10 '20 at 13:03

0 Answers0