diff options
| author | Johannes Grunenberg <nerixdev@outlook.de> | 2024-11-13 17:18:27 +0100 |
|---|---|---|
| committer | Johannes Grunenberg <nerixdev@outlook.de> | 2024-11-21 04:50:25 +0100 |
| commit | 8fabcb642f3a156ac5f97aa82ac722df3472dbfb (patch) | |
| tree | 4a215bc275d32c2655f7028b5d138e50ed43ee34 /src | |
| parent | c20c7814f4f43af00ef14fb41b2b25ca86539abf (diff) | |
QJson: Move writing from QJsonDocument to QJsonValue
JSON values are now written directly by QJsonValue instead of
QJsonDocument. Only objects and arrays are currently supported to be
written.
Change-Id: I413db14c69e8d6e9f06431051c8d716d1fb9d4eb
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/corelib/serialization/qjsonvalue.cpp | 40 | ||||
| -rw-r--r-- | src/corelib/serialization/qjsonvalue.h | 9 |
2 files changed, 48 insertions, 1 deletions
diff --git a/src/corelib/serialization/qjsonvalue.cpp b/src/corelib/serialization/qjsonvalue.cpp index c2dea9817d1..7ccfb782c33 100644 --- a/src/corelib/serialization/qjsonvalue.cpp +++ b/src/corelib/serialization/qjsonvalue.cpp @@ -15,6 +15,7 @@ #include <qdebug.h> #include "qdatastream.h" #include "qjsonparser_p.h" +#include "qjsonwriter_p.h" #include <private/qnumeric_p.h> #include <private/qcborvalue_p.h> @@ -606,7 +607,7 @@ QVariant QJsonValue::toVariant() const Currently, only objects/maps and arrays/lists can be parsed. - \sa QJsonParseError, isUndefined() + \sa QJsonParseError, isUndefined(), toJson() */ QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error) { @@ -617,6 +618,43 @@ QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error) } /*! + \enum QJsonValue::JsonFormat + \since 6.9 + + This value defines the format of the JSON byte array produced + when converting to a QJsonValue using toJson(). + + \value Indented Defines human readable output as follows: + \snippet code/src_corelib_serialization_qjsondocument.cpp 0 + + \value Compact Defines a compact output as follows: + \snippet code/src_corelib_serialization_qjsondocument.cpp 1 + */ + +/*! + \since 6.9 + Converts the QJsonValue to a UTF-8 encoded JSON value in the provided \a format. + + Currently, only objects/maps and arrays/lists can be encoded. + + \sa fromJson(), JsonFormat + */ +#if !defined(QT_JSON_READONLY) || defined(Q_QDOC) +QByteArray QJsonValue::toJson(JsonFormat format) const +{ + QByteArray json; + + const QCborContainerPrivate *container = QJsonPrivate::Value::container(value); + if (isArray()) + QJsonPrivate::Writer::arrayToJson(container, json, 0, (format == Compact)); + else + QJsonPrivate::Writer::objectToJson(container, json, 0, (format == Compact)); + + return json; +} +#endif + +/*! Returns the type of the value. \sa QJsonValue::Type diff --git a/src/corelib/serialization/qjsonvalue.h b/src/corelib/serialization/qjsonvalue.h index e09f5061992..8e0f6d879f9 100644 --- a/src/corelib/serialization/qjsonvalue.h +++ b/src/corelib/serialization/qjsonvalue.h @@ -35,6 +35,11 @@ public: Undefined = 0x80 }; + enum JsonFormat { + Indented, + Compact, + }; + QJsonValue(Type = Null); QJsonValue(bool b); QJsonValue(double n); @@ -71,6 +76,10 @@ public: static QJsonValue fromJson(QByteArrayView json, QJsonParseError *error = nullptr); +#if !defined(QT_JSON_READONLY) || defined(Q_QDOC) + QByteArray toJson(JsonFormat format = JsonFormat::Indented) const; +#endif + Type type() const; inline bool isNull() const { return type() == Null; } inline bool isBool() const { return type() == Bool; } |
