Skip to content

Commit

Permalink
QJson: Move writing from QJsonDocument to QJsonValue
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Nerixyz committed Nov 21, 2024
1 parent c20c781 commit 8fabcb6
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 38 deletions.
40 changes: 39 additions & 1 deletion src/corelib/serialization/qjsonvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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)
{
Expand All @@ -616,6 +617,43 @@ QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error)
return result;
}

/*!
\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.
Expand Down
9 changes: 9 additions & 0 deletions src/corelib/serialization/qjsonvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Q_CORE_EXPORT QJsonValue
Undefined = 0x80
};

enum JsonFormat {
Indented,
Compact,
};

QJsonValue(Type = Null);
QJsonValue(bool b);
QJsonValue(double n);
Expand Down Expand Up @@ -71,6 +76,10 @@ class Q_CORE_EXPORT QJsonValue

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; }
Expand Down
Loading

0 comments on commit 8fabcb6

Please sign in to comment.