summaryrefslogtreecommitdiffstats
path: root/logic/Json.h
blob: d22aa60628d84ca35d096957984f369f3c79ec05 (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
// Licensed under the Apache-2.0 license. See README.md for details.

#pragma once

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QDateTime>
#include <QUrl>
#include <QDir>
#include <QUuid>
#include <QVariant>
#include <memory>

#include "Exception.h"

namespace Json
{
DECLARE_EXCEPTION(Json);

enum Requirement
{
	Required
};

void write(const QJsonDocument &doc, const QString &filename);
void write(const QJsonObject &object, const QString &filename);
void write(const QJsonArray &array, const QString &filename);
QByteArray toBinary(const QJsonObject &obj);
QByteArray toBinary(const QJsonArray &array);
QByteArray toText(const QJsonObject &obj);
QByteArray toText(const QJsonArray &array);

QJsonDocument ensureDocument(const QByteArray &data, const QString &what = "Document");
QJsonDocument ensureDocument(const QString &filename, const QString &what = "Document");
QJsonObject ensureObject(const QJsonDocument &doc, const QString &what = "Document");
QJsonArray ensureArray(const QJsonDocument &doc, const QString &what = "Document");

/////////////////// WRITING ////////////////////

void writeString(QJsonObject & to, const QString &key, const QString &value);
void writeStringList(QJsonObject & to, const QString &key, const QStringList &values);

template <typename T>
void writeObjectList(QJsonObject & to, QString key, QList<std::shared_ptr<T>> values)
{
	if (!values.isEmpty())
	{
		QJsonArray array;
		for (auto value: values)
		{
			array.append(value->toJson());
		}
		to.insert(key, array);
	}
}
template <typename T>
void writeObjectList(QJsonObject & to, QString key, QList<T> values)
{
	if (!values.isEmpty())
	{
		QJsonArray array;
		for (auto value: values)
		{
			array.append(value.toJson());
		}
		to.insert(key, array);
	}
}

template<typename T>
QJsonValue toJson(const T &t)
{
	return QJsonValue(t);
}
template<>
QJsonValue toJson<QUrl>(const QUrl &url);
template<>
QJsonValue toJson<QByteArray>(const QByteArray &data);
template<>
QJsonValue toJson<QDateTime>(const QDateTime &datetime);
template<>
QJsonValue toJson<QDir>(const QDir &dir);
template<>
QJsonValue toJson<QUuid>(const QUuid &uuid);
template<>
QJsonValue toJson<QVariant>(const QVariant &variant);

template<typename T>
QJsonArray toJsonArray(const QList<T> &container)
{
	QJsonArray array;
	for (const T item : container)
	{
		array.append(toJson<T>(item));
	}
	return array;
}

////////////////// READING ////////////////////

template <typename T>
T ensureIsType(const QJsonValue &value, const Requirement requirement = Required, const QString &what = "Value");

template<> double ensureIsType<double>(const QJsonValue &value, const Requirement, const QString &what);
template<> bool ensureIsType<bool>(const QJsonValue &value, const Requirement, const QString &what);
template<> int ensureIsType<int>(const QJsonValue &value, const Requirement, const QString &what);
template<> QJsonObject ensureIsType<QJsonObject>(const QJsonValue &value, const Requirement, const QString &what);
template<> QJsonArray ensureIsType<QJsonArray>(const QJsonValue &value, const Requirement, const QString &what);
template<> QJsonValue ensureIsType<QJsonValue>(const QJsonValue &value, const Requirement, const QString &what);
template<> QByteArray ensureIsType<QByteArray>(const QJsonValue &value, const Requirement, const QString &what);
template<> QDateTime ensureIsType<QDateTime>(const QJsonValue &value, const Requirement, const QString &what);
template<> QVariant ensureIsType<QVariant>(const QJsonValue &value, const Requirement, const QString &what);
template<> QString ensureIsType<QString>(const QJsonValue &value, const Requirement, const QString &what);
template<> QUuid ensureIsType<QUuid>(const QJsonValue &value, const Requirement, const QString &what);
template<> QDir ensureIsType<QDir>(const QJsonValue &value, const Requirement, const QString &what);
template<> QUrl ensureIsType<QUrl>(const QJsonValue &value, const Requirement, const QString &what);

// the following functions are higher level functions, that make use of the above functions for
// type conversion
template <typename T>
T ensureIsType(const QJsonValue &value, const T default_, const QString &what = "Value")
{
	if (value.isUndefined())
	{
		return default_;
	}
	return ensureIsType<T>(value, Required, what);
}
template <typename T>
T ensureIsType(const QJsonObject &parent, const QString &key,
			   const Requirement requirement = Required,
			   const QString &what = "__placeholder__")
{
	const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
	if (!parent.contains(key))
	{
		throw JsonException(localWhat + "s parent does not contain " + localWhat);
	}
	return ensureIsType<T>(parent.value(key), requirement, localWhat);
}
template <typename T>
T ensureIsType(const QJsonObject &parent, const QString &key, const T default_,
			   const QString &what = "__placeholder__")
{
	const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
	if (!parent.contains(key))
	{
		return default_;
	}
	return ensureIsType<T>(parent.value(key), default_, localWhat);
}

template <typename T>
QList<T> ensureIsArrayOf(const QJsonDocument &doc)
{
	const QJsonArray array = ensureArray(doc);
	QList<T> out;
	for (const QJsonValue val : array)
	{
		out.append(ensureIsType<T>(val, Required, "Document"));
	}
	return out;
}
template <typename T>
QList<T> ensureIsArrayOf(const QJsonValue &value, const Requirement = Required,
						 const QString &what = "Value")
{
	const QJsonArray array = ensureIsType<QJsonArray>(value, Required, what);
	QList<T> out;
	for (const QJsonValue val : array)
	{
		out.append(ensureIsType<T>(val, Required, what));
	}
	return out;
}
template <typename T>
QList<T> ensureIsArrayOf(const QJsonValue &value, const QList<T> default_,
						 const QString &what = "Value")
{
	if (value.isUndefined())
	{
		return default_;
	}
	return ensureIsArrayOf<T>(value, Required, what);
}
template <typename T>
QList<T> ensureIsArrayOf(const QJsonObject &parent, const QString &key,
						 const Requirement requirement = Required,
						 const QString &what = "__placeholder__")
{
	const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
	if (!parent.contains(key))
	{
		throw JsonException(localWhat + "s parent does not contain " + localWhat);
	}
	return ensureIsArrayOf<T>(parent.value(key), requirement, localWhat);
}
template <typename T>
QList<T> ensureIsArrayOf(const QJsonObject &parent, const QString &key,
						 const QList<T> &default_, const QString &what = "__placeholder__")
{
	const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
	if (!parent.contains(key))
	{
		return default_;
	}
	return ensureIsArrayOf<T>(parent.value(key), default_, localWhat);
}

// this macro part could be replaced by variadic functions that just pass on their arguments, but that wouldn't work well with IDE helpers
#define JSON_HELPERFUNCTIONS(NAME, TYPE) \
	inline TYPE ensure##NAME(const QJsonValue &value, const Requirement requirement = Required, const QString &what = "Value") \
{ return ensureIsType<TYPE>(value, requirement, what); } \
	inline TYPE ensure##NAME(const QJsonValue &value, const TYPE default_, const QString &what = "Value") \
{ return ensureIsType<TYPE>(value, default_, what); } \
	inline TYPE ensure##NAME(const QJsonObject &parent, const QString &key, const Requirement requirement = Required, const QString &what = "__placeholder__") \
{ return ensureIsType<TYPE>(parent, key, requirement, what); } \
	inline TYPE ensure##NAME(const QJsonObject &parent, const QString &key, const TYPE default_, const QString &what = "__placeholder") \
{ return ensureIsType<TYPE>(parent, key, default_, what); }

JSON_HELPERFUNCTIONS(Array, QJsonArray)
JSON_HELPERFUNCTIONS(Object, QJsonObject)
JSON_HELPERFUNCTIONS(JsonValue, QJsonValue)
JSON_HELPERFUNCTIONS(String, QString)
JSON_HELPERFUNCTIONS(Boolean, bool)
JSON_HELPERFUNCTIONS(Double, double)
JSON_HELPERFUNCTIONS(Integer, int)
JSON_HELPERFUNCTIONS(DateTime, QDateTime)
JSON_HELPERFUNCTIONS(Url, QUrl)
JSON_HELPERFUNCTIONS(ByteArray, QByteArray)
JSON_HELPERFUNCTIONS(Dir, QDir)
JSON_HELPERFUNCTIONS(Uuid, QUuid)
JSON_HELPERFUNCTIONS(Variant, QVariant)

#undef JSON_HELPERFUNCTIONS

}
using JSONValidationError = Json::JsonException;