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 };
77 for (auto it = json.constBegin(); it != json.constEnd(); ++it) {
78 if (!knownKeys.contains(it.key()))
79 ref.extras.insert(it.key(), it.value());
80 }
81
82 return ref;
83}
84
85//=============================================================================================================
86
87QCborMap MnaFileRef::toCbor() const
88{
89 QCborMap cbor = QCborMap::fromJsonObject(extras);
90 cbor[QLatin1String("role")] = mnaFileRoleToString(role);
91 cbor[QLatin1String("path")] = path;
92 cbor[QLatin1String("sha256")] = sha256;
93 cbor[QLatin1String("format")] = format;
94 cbor[QLatin1String("size_bytes")] = sizeBytes;
95 cbor[QLatin1String("embedded")] = embedded;
96 if(embedded && !data.isEmpty()) {
97 cbor[QLatin1String("data")] = QCborValue(data);
98 }
99 return cbor;
100}
101
102//=============================================================================================================
103
104MnaFileRef MnaFileRef::fromCbor(const QCborMap& cbor)
105{
106 MnaFileRef ref;
107 ref.role = mnaFileRoleFromString(cbor[QLatin1String("role")].toString());
108 ref.path = cbor[QLatin1String("path")].toString();
109 ref.sha256 = cbor[QLatin1String("sha256")].toString();
110 ref.format = cbor[QLatin1String("format")].toString();
111 ref.sizeBytes = cbor[QLatin1String("size_bytes")].toInteger();
112 ref.embedded = cbor[QLatin1String("embedded")].toBool();
113 if(ref.embedded) {
114 ref.data = cbor[QLatin1String("data")].toByteArray();
115 }
116
117 static const QSet<QString> knownKeys = {
118 QStringLiteral("role"), QStringLiteral("path"),
119 QStringLiteral("sha256"), QStringLiteral("format"),
120 QStringLiteral("size_bytes"), QStringLiteral("embedded"),
121 QStringLiteral("data")
122 };
123 QJsonObject cborJson = cbor.toJsonObject();
124 for (auto it = cborJson.constBegin(); it != cborJson.constEnd(); ++it) {
125 if (!knownKeys.contains(it.key()))
126 ref.extras.insert(it.key(), it.value());
127 }
128
129 return ref;
130}
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:162
QString mnaFileRoleToString(MnaFileRole role)
Definition mna_types.h:136
static MnaFileRef fromJson(const QJsonObject &json)
QCborMap toCbor() const
QJsonObject toJson() const
static MnaFileRef fromCbor(const QCborMap &cbor)
QJsonObject extras