Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-28560

Number type support in QJsonValue

    XMLWordPrintable

Details

    • edec095cf80b62057116ce75c581b5ca5866bdcc (qt/qtbase/dev)

    Description

      We've recently ported one of our projects from JsonCpp to an early version of the QtJson implementation. The migration went smoothly for the most part, but there is a glaring difference in the APIs – QtJson doesn't support integral types.

      This is a problem for us, as we would like to be able to store 64-bit unsigned integers in some of our JSON snippets (we use JSON to store state about remote systems that use uint64 identifiers). QJsonValue only supports doubles, and in the JSON strings these are truncated via QByteArray::number to %.6g, so the values cannot be represented accurately, e.g. quint64's maximum value is only represented as 1.84467e+19 instead of 18446744073709551615.

      This is an enormous loss of precision. Was there a reason for not supporting these types? Are there plans to extend QJsonValue to support integers (preferably before 5.0)?

      Simple test which shows the problem:

      #include <QCoreApplication>
      #include <QJsonDocument>
      #include <QJsonObject>
      #include <QDateTime>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QDateTime stamp = QDateTime::currentDateTime();
      
          QString jStr = "{\"id\":"+QString::number(stamp.toTime_t())+"}";
          QJsonDocument jDoc = QJsonDocument::fromJson(jStr.toLatin1());
      
          qDebug() << jDoc.object().value("id");
      
          qDebug() << "original time stamp: " << stamp.toString("MM dd yyyy hh:mm:ss z");
          qDebug() << "time stamp from json: " << QDateTime::fromTime_t(jDoc.object().value("id").toDouble()).toString("MM dd yyyy hh:mm:ss z");
      
          qDebug() << "JSON returns same value? " << (stamp == QDateTime::fromTime_t(jDoc.object().value("id").toDouble()));
      
          return 0;
      }
      

      Attachments

        1. JSON_BUG.zip
          1.0 kB
        2. json.patch
          12 kB
        3. jsonlonglong.diff
          15 kB

        Issue Links

          For Gerrit Dashboard: QTBUG-28560
          # Subject Branch Project Status CR V

          Activity

            People

              allan.jensen Allan Sandfeld Jensen
              dlonie David Lonie
              Votes:
              61 Vote for this issue
              Watchers:
              55 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes