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

Differnt parsing modes for QJsonDocument

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.3.1, 6.5
    • Core: Serialization
    • None
    • Win7 64-bit

    • All

    Description

      In Qt 5.3.x, QJsonDocument accepts string values with real line breaks in them. According to the JSON spec, this is not allowed. Line breaks have to be encoded using \n, so QJsonDocument does not fully comply with the spec:

      All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

      (see e.g. https://tools.ietf.org/html/rfc7159)

      Example:
      #include <QMessageBox>
      #include <QJsonDocument>
      
      int main(int argc, char **argv)
      {
      	QApplication app(argc, argv);
      
      	QString json("{ \"string\": \"this is an \n illegal string\" }");
      	
      	QJsonParseError error;
      	QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8(), &error);
      	
      	QMessageBox::information(NULL, "Result", QString("Error: %1\nString Value: \"%2\"").arg(error.errorString()).arg(doc.object().value("string").toString()));
      				
      	return 0;
      }
      

      The example shows the message box stating "no error occured".

      My suggestion to solve this, is to add different parsing modes similar to QUrl::ParsingMode.
      I would suggest three modes:

      • StrictMode - Sticking strict to the JSON spec, i.e. line breaks, tabs etc. have to be escaped.
      • TolerantMode - Allowing a certain set of control characters within strings.
      • ExtendedMode - Additionally to TolerantMode, this mode also allows comments within JSON documents.
        Common forms of comments are shell style single-line comments # (used by Perl's JSON package) and C++ style single-line // and multi-line comments /* */ (used by different JavaScript libraries, e.g. jsonminify).
        This mode could be implemented in a similar way as described in the jsonminify link: simply remove comments from JSON and then parse it.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              cnn Qt Core & Network
              ignitor Jochen Ulrich
              Vladimir Minenko Vladimir Minenko
              Alex Blasche Alex Blasche
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes