Skip to content

Commit 52e1bff

Browse files
committed
Forward QJsonParseError when parsing raw JSON data
1 parent ea74299 commit 52e1bff

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/core/include/qx/core/qx-json.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ class QX_CORE_EXPORT File
9898

9999
class QX_CORE_EXPORT Data
100100
{
101+
private:
102+
QString mDataError;
103+
101104
public:
102-
Data();
105+
Data(const QString& dataError = {});
103106

104107
QString string() const;
105108
};
@@ -812,7 +815,7 @@ JsonError parseJson(T& parsed, const QByteArray& data)
812815
QJsonDocument jd = QJsonDocument::fromJson(data, &jpe);
813816

814817
if(jpe.error != jpe.NoError)
815-
return JsonError(QxJsonPrivate::ERR_READ_DATA, JsonError::InvalidData).withContext(QxJson::Data());
818+
return JsonError(QxJsonPrivate::ERR_READ_DATA, JsonError::InvalidData).withContext(QxJson::Data(jpe.errorString()));
816819

817820
// True parse
818821
return parseJson(parsed, jd).withContext(QxJson::Data());

lib/core/src/qx-json.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,24 @@ QString File::string() const
509509
*/
510510

511511
/*!
512-
* Constructs a data element node.
512+
* Constructs a data element node with data specific error information @a dataError,
513+
* if any.
513514
*/
514-
Data::Data() {}
515+
Data::Data(const QString& dataError) :
516+
mDataError(dataError)
517+
{}
515518

516519
/*!
517520
* Returns the string representation of the node.
518521
*/
519-
QString Data::string() const { return u"Data"_s; }
522+
QString Data::string() const
523+
{
524+
QString str = u"Data:"_s;
525+
if(!mDataError.isEmpty())
526+
str += (u"\n [%1]"_s).arg(mDataError);
527+
528+
return str;
529+
}
520530

521531
/*!
522532
* @class Document

0 commit comments

Comments
 (0)