v2.0.0
Loading...
Searching...
No Matches
mna_file_ref.cpp
Go to the documentation of this file.
1//=============================================================================================================
20
21//=============================================================================================================
22// INCLUDES
23//=============================================================================================================
24
25#include "mna_file_ref.h"
26
27#include <QJsonValue>
28#include <QCborValue>
29#include <QCborArray>
30
31//=============================================================================================================
32// USED NAMESPACES
33//=============================================================================================================
34
35using namespace MNALIB;
36
37//=============================================================================================================
38// DEFINE METHODS
39//=============================================================================================================
40
41QJsonObject MnaFileRef::toJson() const
42{
43 QJsonObject json = extras;
44 json[QLatin1String("role")] = mnaFileRoleToString(role);
45 json[QLatin1String("path")] = path;
46 json[QLatin1String("sha256")] = sha256;
47 json[QLatin1String("format")] = format;
48 json[QLatin1String("size_bytes")] = sizeBytes;
49 json[QLatin1String("embedded")] = embedded;
50 if (embedded && !data.isEmpty()) {
51 json[QLatin1String("data")] = QString::fromLatin1(data.toBase64());
52 }
53 return json;
54}
55
56//=============================================================================================================
57
58MnaFileRef MnaFileRef::fromJson(const QJsonObject& json)
59{
60 MnaFileRef ref;
61 ref.role = mnaFileRoleFromString(json[QLatin1String("role")].toString());
62 ref.path = json[QLatin1String("path")].toString();
63 ref.sha256 = json[QLatin1String("sha256")].toString();
64 ref.format = json[QLatin1String("format")].toString();
65 ref.sizeBytes = static_cast<qint64>(json[QLatin1String("size_bytes")].toDouble());
66 ref.embedded = json[QLatin1String("embedded")].toBool();
67 if (ref.embedded) {
68 ref.data = QByteArray::fromBase64(json[QLatin1String("data")].toString().toLatin1());
69 }
70
71 static const QSet<QString> knownKeys = {
72 QStringLiteral("role"), QStringLiteral("path"),
73 QStringLiteral("sha256"), QStringLiteral("format"),
74 QStringLiteral("size_bytes"), QStringLiteral("embedded"),
75 QStringLiteral("data")};
76 for (auto it = json.constBegin(); it != json.constEnd(); ++it) {
77 if (!knownKeys.contains(it.key()))
78 ref.extras.insert(it.key(), it.value());
79 }
80
81 return ref;
82}
83
84//=============================================================================================================
85
86QCborMap MnaFileRef::toCbor() const
87{
88 QCborMap cbor = QCborMap::fromJsonObject(extras);
89 cbor[QLatin1String("role")] = mnaFileRoleToString(role);
90 cbor[QLatin1String("path")] = path;
91 cbor[QLatin1String("sha256")] = sha256;
92 cbor[QLatin1String("format")] = format;
93 cbor[QLatin1String("size_bytes")] = sizeBytes;
94 cbor[QLatin1String("embedded")] = embedded;
95 if (embedded && !data.isEmpty()) {
96 cbor[QLatin1String("data")] = QCborValue(data);
97 }
98 return cbor;
99}
100
101//=============================================================================================================
102
103MnaFileRef MnaFileRef::fromCbor(const QCborMap& cbor)
104{
105 MnaFileRef ref;
106 ref.role = mnaFileRoleFromString(cbor[QLatin1String("role")].toString());
107 ref.path = cbor[QLatin1String("path")].toString();
108 ref.sha256 = cbor[QLatin1String("sha256")].toString();
109 ref.format = cbor[QLatin1String("format")].toString();
110 ref.sizeBytes = cbor[QLatin1String("size_bytes")].toInteger();
111 ref.embedded = cbor[QLatin1String("embedded")].toBool();
112 if (ref.embedded) {
113 ref.data = cbor[QLatin1String("data")].toByteArray();
114 }
115
116 static const QSet<QString> knownKeys = {
117 QStringLiteral("role"), QStringLiteral("path"),
118 QStringLiteral("sha256"), QStringLiteral("format"),
119 QStringLiteral("size_bytes"), QStringLiteral("embedded"),
120 QStringLiteral("data")};
121 QJsonObject cborJson = cbor.toJsonObject();
122 for (auto it = cborJson.constBegin(); it != cborJson.constEnd(); ++it) {
123 if (!knownKeys.contains(it.key()))
124 ref.extras.insert(it.key(), it.value());
125 }
126
127 return ref;
128}
Reference to a file inside an MNA project — relative path, semantic role, SHA-256 hash,...
MNE Analysis Container Format (mna/mnx).
MnaFileRole mnaFileRoleFromString(const QString &str)
Definition mna_types.h:192
QString mnaFileRoleToString(MnaFileRole role)
Definition mna_types.h:146
static MnaFileRef fromJson(const QJsonObject &json)
QCborMap toCbor() const
QJsonObject toJson() const
static MnaFileRef fromCbor(const QCborMap &cbor)
QJsonObject extras