Annotation: toJsonValue / fromJsonValue

Change-Id: I743176a90cf617a6bcdb988f3c34d0fc232a001d
Reviewed-by: Aleksei German <aleksei.german@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Michael Winkelmann
2021-05-03 22:17:20 +02:00
parent fb26650d86
commit 93e4794114
2 changed files with 57 additions and 3 deletions

View File

@@ -25,12 +25,13 @@
#pragma once
#include <QObject>
#include <QDebug>
#include <QDataStream>
#include <QDebug>
#include <QJsonObject>
#include <QObject>
#include "qmldesignercorelib_global.h"
#include "nodeinstanceglobal.h"
#include "qmldesignercorelib_global.h"
namespace QmlDesigner {
@@ -97,6 +98,8 @@ public:
bool isEmpty() const;
QString toQString() const;
QJsonValue toJsonValue() const;
bool fromJsonValue(QJsonValue const &);
friend QDebug &operator<<(QDebug &stream, const Comment &comment);
@@ -130,6 +133,8 @@ public:
QString toQString() const;
void fromQString(const QString &str);
QJsonValue toJsonValue() const;
bool fromJsonValue(QJsonValue const &);
friend QDebug &operator<<(QDebug &stream, const Annotation &annotation);

View File

@@ -26,6 +26,7 @@
#include "annotation.h"
#include <QDateTime>
#include <QJsonArray>
#include <QString>
namespace QmlDesigner {
@@ -150,6 +151,27 @@ QString Comment::toQString() const
return result.join(s_sep);
}
QJsonValue Comment::toJsonValue() const
{
return QJsonObject{
{{"title", m_title}, {"author", m_author}, {"text", m_text}, {"timestamp", m_timestamp}}};
};
bool Comment::fromJsonValue(QJsonValue const &v)
{
if (!v.isObject())
return false;
auto obj = v.toObject();
Comment comment;
comment.m_title = obj["title"].toString();
comment.m_author = obj["author"].toString();
comment.m_text = obj["text"].toString();
comment.m_timestamp = obj["timestamp"].toInt();
*this = comment;
return true;
}
QDebug &operator<<(QDebug &stream, const Comment &comment)
{
stream << "\"title: " << comment.m_title << "\" ";
@@ -299,6 +321,33 @@ void Annotation::fromQString(const QString &str)
}
}
QJsonValue Annotation::toJsonValue() const
{
QJsonObject obj;
QJsonArray comments;
for (auto &comment : m_comments)
comments.push_back(comment.toJsonValue());
obj["comments"] = comments;
return obj;
}
bool Annotation::fromJsonValue(const QJsonValue &v)
{
if (!v.isObject())
return false;
auto obj = v.toObject();
auto jsonComments = obj["comments"].toArray();
m_comments.clear();
for (auto json : jsonComments) {
Comment comment;
if (comment.fromJsonValue(json))
m_comments.push_back(comment);
}
return true;
}
QDebug &operator<<(QDebug &stream, const Annotation &annotation)
{
stream << "\"Annotation: " << annotation.m_comments << "\" ";