summaryrefslogtreecommitdiffstats
path: root/api/logic/Json.cpp
blob: f2cbc8a3a52ebd6eccd22a39a52ffc5ee2286642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Licensed under the Apache-2.0 license. See README.md for details.

#include "Json.h"

#include <QFile>

#include "FileSystem.h"
#include <math.h>

namespace Json
{
void write(const QJsonDocument &doc, const QString &filename)
{
	FS::write(filename, doc.toJson());
}
void write(const QJsonObject &object, const QString &filename)
{
	write(QJsonDocument(object), filename);
}
void write(const QJsonArray &array, const QString &filename)
{
	write(QJsonDocument(array), filename);
}

QByteArray toBinary(const QJsonObject &obj)
{
	return QJsonDocument(obj).toBinaryData();
}
QByteArray toBinary(const QJsonArray &array)
{
	return QJsonDocument(array).toBinaryData();
}
QByteArray toText(const QJsonObject &obj)
{
	return QJsonDocument(obj).toJson(QJsonDocument::Compact);
}
QByteArray toText(const QJsonArray &array)
{
	return QJsonDocument(array).toJson(QJsonDocument::Compact);
}

static bool isBinaryJson(const QByteArray &data)
{
	decltype(QJsonDocument::BinaryFormatTag) tag = QJsonDocument::BinaryFormatTag;
	return memcmp(data.constData(), &tag, sizeof(QJsonDocument::BinaryFormatTag)) == 0;
}
QJsonDocument requireDocument(const QByteArray &data, const QString &what)
{
	if (isBinaryJson(data))
	{
		QJsonDocument doc = QJsonDocument::fromBinaryData(data);
		if (doc.isNull())
		{
			throw JsonException(what + ": Invalid JSON (binary JSON detected)");
		}
		return doc;
	}
	else
	{
		QJsonParseError error;
		QJsonDocument doc = QJsonDocument::fromJson(data, &error);
		if (error.error != QJsonParseError::NoError)
		{
			throw JsonException(what + ": Error parsing JSON: " + error.errorString());
		}
		return doc;
	}
}
QJsonDocument requireDocument(const QString &filename, const QString &what)
{
	return requireDocument(FS::read(filename), what);
}
QJsonObject requireObject(const QJsonDocument &doc, const QString &what)
{
	if (!doc.isObject())
	{
		throw JsonException(what + " is not an object");
	}
	return doc.object();
}
QJsonArray requireArray(const QJsonDocument &doc, const QString &what)
{
	if (!doc.isArray())
	{
		throw JsonException(what + " is not an array");
	}
	return doc.array();
}

void writeString(QJsonObject &to, const QString &key, const QString &value)
{
	if (!value.isEmpty())
	{
		to.insert(key, value);
	}
}

void writeStringList(QJsonObject &to, const QString &key, const QStringList &values)
{
	if (!values.isEmpty())
	{
		QJsonArray array;
		for(auto value: values)
		{
			array.append(value);
		}
		to.insert(key, array);
	}
}

template<>
QJsonValue toJson<QUrl>(const QUrl &url)
{
	return QJsonValue(url.toString(QUrl::FullyEncoded));
}
template<>
QJsonValue toJson<QByteArray>(const QByteArray &data)
{
	return QJsonValue(QString::fromLatin1(data.toHex()));
}
template<>
QJsonValue toJson<QDateTime>(const QDateTime &datetime)
{
	return QJsonValue(datetime.toString(Qt::ISODate));
}
template<>
QJsonValue toJson<QDir>(const QDir &dir)
{
	return QDir::current().relativeFilePath(dir.absolutePath());
}
template<>
QJsonValue toJson<QUuid>(const QUuid &uuid)
{
	return uuid.toString();
}
template<>
QJsonValue toJson<QVariant>(const QVariant &variant)
{
	return QJsonValue::fromVariant(variant);
}


template<> QByteArray requireIsType<QByteArray>(const QJsonValue &value, const QString &what)
{
	const QString string = ensureIsType<QString>(value, what);
	// ensure that the string can be safely cast to Latin1
	if (string != QString::fromLatin1(string.toLatin1()))
	{
		throw JsonException(what + " is not encodable as Latin1");
	}
	return QByteArray::fromHex(string.toLatin1());
}

template<> QJsonArray requireIsType<QJsonArray>(const QJsonValue &value, const QString &what)
{
	if (!value.isArray())
	{
		throw JsonException(what + " is not an array");
	}
	return value.toArray();
}


template<> QString requireIsType<QString>(const QJsonValue &value, const QString &what)
{
	if (!value.isString())
	{
		throw JsonException(what + " is not a string");
	}
	return value.toString();
}

template<> bool requireIsType<bool>(const QJsonValue &value, const QString &what)
{
	if (!value.isBool())
	{
		throw JsonException(what + " is not a bool");
	}
	return value.toBool();
}

template<> double requireIsType<double>(const QJsonValue &value, const QString &what)
{
	if (!value.isDouble())
	{
		throw JsonException(what + " is not a double");
	}
	return value.toDouble();
}

template<> int requireIsType<int>(const QJsonValue &value, const QString &what)
{
	const double doubl = requireIsType<double>(value, what);
	if (fmod(doubl, 1) != 0)
	{
		throw JsonException(what + " is not an integer");
	}
	return int(doubl);
}

template<> QDateTime requireIsType<QDateTime>(const QJsonValue &value, const QString &what)
{
	const QString string = requireIsType<QString>(value, what);
	const QDateTime datetime = QDateTime::fromString(string, Qt::ISODate);
	if (!datetime.isValid())
	{
		throw JsonException(what + " is not a ISO formatted date/time value");
	}
	return datetime;
}

template<> QUrl requireIsType<QUrl>(const QJsonValue &value, const QString &what)
{
	const QString string = ensureIsType<QString>(value, what);
	if (string.isEmpty())
	{
		return QUrl();
	}
	const QUrl url = QUrl(string, QUrl::StrictMode);
	if (!url.isValid())
	{
		throw JsonException(what + " is not a correctly formatted URL");
	}
	return url;
}

template<> QDir requireIsType<QDir>(const QJsonValue &value, const QString &what)
{
	const QString string = requireIsType<QString>(value, what);
	// FIXME: does not handle invalid characters!
	return QDir::current().absoluteFilePath(string);
}

template<> QUuid requireIsType<QUuid>(const QJsonValue &value, const QString &what)
{
	const QString string = requireIsType<QString>(value, what);
	const QUuid uuid = QUuid(string);
	if (uuid.toString() != string) // converts back => valid
	{
		throw JsonException(what + " is not a valid UUID");
	}
	return uuid;
}

template<> QJsonObject requireIsType<QJsonObject>(const QJsonValue &value, const QString &what)
{
	if (!value.isObject())
	{
		throw JsonException(what + " is not an object");
	}
	return value.toObject();
}

template<> QVariant requireIsType<QVariant>(const QJsonValue &value, const QString &what)
{
	if (value.isNull() || value.isUndefined())
	{
		throw JsonException(what + " is null or undefined");
	}
	return value.toVariant();
}

template<> QJsonValue requireIsType<QJsonValue>(const QJsonValue &value, const QString &what)
{
	if (value.isNull() || value.isUndefined())
	{
		throw JsonException(what + " is null or undefined");
	}
	return value;
}

}