Skip to content

Commit

Permalink
QJson: Move parsing from QJsonDocument to QJsonValue
Browse files Browse the repository at this point in the history
JSON values are now parsed by QJsonValue instead of QJsonDocument. For
parsing, this removes the need for QJsonDocument entirely.

Fixes: QTBUG-109870
Change-Id: I2f9bae3a53e5d445cc9e8437c63a53a2c7b3593e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
Nerixyz committed Nov 20, 2024
1 parent d2a8a0d commit 7be9b59
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 160 deletions.
1 change: 1 addition & 0 deletions src/corelib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ qt_internal_add_module(Core
serialization/qjsoncbor.cpp
serialization/qjsondocument.cpp serialization/qjsondocument.h
serialization/qjsonobject.cpp serialization/qjsonobject.h
serialization/qjsonparseerror.h
serialization/qjsonparser.cpp serialization/qjsonparser_p.h
serialization/qjsonvalue.cpp serialization/qjsonvalue.h
serialization/qjsonwriter.cpp serialization/qjsonwriter_p.h
Expand Down
27 changes: 1 addition & 26 deletions src/corelib/serialization/qjsondocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define QJSONDOCUMENT_H

#include <QtCore/qcompare.h>
#include <QtCore/qjsonparseerror.h>
#include <QtCore/qjsonvalue.h>
#include <QtCore/qscopedpointer.h>

Expand All @@ -17,32 +18,6 @@ class QCborValue;

namespace QJsonPrivate { class Parser; }

struct Q_CORE_EXPORT QJsonParseError
{
enum ParseError {
NoError = 0,
UnterminatedObject,
MissingNameSeparator,
UnterminatedArray,
MissingValueSeparator,
IllegalValue,
TerminationByNumber,
IllegalNumber,
IllegalEscapeSequence,
IllegalUTF8String,
UnterminatedString,
MissingObject,
DeepNesting,
DocumentTooLarge,
GarbageAtEnd
};

QString errorString() const;

int offset = -1;
ParseError error = NoError;
};

class QJsonDocumentPrivate;
class Q_CORE_EXPORT QJsonDocument
{
Expand Down
42 changes: 42 additions & 0 deletions src/corelib/serialization/qjsonparseerror.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QJSONPARSEERROR_H
#define QJSONPARSEERROR_H

#include <QtCore/qtconfigmacros.h>
#include <QtCore/qtcoreexports.h>

QT_BEGIN_NAMESPACE

class QString;

struct Q_CORE_EXPORT QJsonParseError
{
enum ParseError {
NoError = 0,
UnterminatedObject,
MissingNameSeparator,
UnterminatedArray,
MissingValueSeparator,
IllegalValue,
TerminationByNumber,
IllegalNumber,
IllegalEscapeSequence,
IllegalUTF8String,
UnterminatedString,
MissingObject,
DeepNesting,
DocumentTooLarge,
GarbageAtEnd
};

QString errorString() const;

int offset = -1;
ParseError error = NoError;
};

QT_END_NAMESPACE

#endif // QJSONPARSEERROR_H
23 changes: 23 additions & 0 deletions src/corelib/serialization/qjsonvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <qhash.h>
#include <qdebug.h>
#include "qdatastream.h"
#include "qjsonparser_p.h"

#include <private/qnumeric_p.h>
#include <private/qcborvalue_p.h>
Expand Down Expand Up @@ -593,6 +594,28 @@ QVariant QJsonValue::toVariant() const
*/
#endif // !QT_NO_VARIANT

/*!
\since 6.9
Parses \a json as a UTF-8 encoded JSON value, and creates a QJsonValue
from it.
Returns a valid QJsonValue if the parsing succeeds. If it fails, the
returned value will be \l {QJsonValue::isUndefined} {undefined}, and
the optional \a error variable will contain further details about the
error.
Currently, only objects/maps and arrays/lists can be parsed.
\sa QJsonParseError, isUndefined()
*/
QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error)
{
QJsonPrivate::Parser parser(json.constData(), json.size());
QJsonValue result;
result.value = parser.parse(error);
return result;
}

/*!
Returns the type of the value.
Expand Down
3 changes: 3 additions & 0 deletions src/corelib/serialization/qjsonvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QtCore/qcborvalue.h>
#include <QtCore/qcompare.h>
#include <QtCore/qglobal.h>
#include <QtCore/qjsonparseerror.h>
#include <QtCore/qstring.h>
#include <QtCore/qshareddata.h>

Expand Down Expand Up @@ -68,6 +69,8 @@ class Q_CORE_EXPORT QJsonValue
static QJsonValue fromVariant(const QVariant &variant);
QVariant toVariant() const;

static QJsonValue fromJson(QByteArrayView json, QJsonParseError *error = nullptr);

Type type() const;
inline bool isNull() const { return type() == Null; }
inline bool isBool() const { return type() == Bool; }
Expand Down
Loading

0 comments on commit 7be9b59

Please sign in to comment.