forked from Cutehacks/duperagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization.cpp
203 lines (177 loc) · 5.78 KB
/
serialization.cpp
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
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtCore/QStringBuilder>
#include <QtCore/QUrlQuery>
#include <QtQml/QQmlEngine>
#include "serialization.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
#include "jsvalueiterator.h"
#else
#include <QtQml/QJSValueIterator>
typedef QJSValueIterator JSValueIterator;
#endif
namespace com { namespace cutehacks { namespace duperagent {
static const QChar OPEN_SQUARE('[');
static const QChar CLOSE_SQUARE(']');
BodyCodec::BodyCodec(QQmlEngine *engine) : m_engine(engine) {}
JsonCodec::JsonCodec(QQmlEngine *engine) : BodyCodec(engine) {}
QJSValue JsonCodec::parse(const QByteArray &data) {
QJsonDocument doc = QJsonDocument::fromJson(data, 0);
return parseJsonDocument(doc);
}
QJSValue JsonCodec::parseJsonDocument(const QJsonDocument &doc)
{
if (doc.isObject()) {
return parseJsonObject(doc.object());
} else if (doc.isArray()) {
return parseJsonArray(doc.array());
} else if(doc.isNull()){
return QJSValue(QJSValue::NullValue);
} else {
return QJSValue();
}
}
QJSValue JsonCodec::parseJsonArray(const QJsonArray &array) {
QJSValue a = m_engine->newArray(array.size());
int i = 0;
for (QJsonArray::ConstIterator it = array.constBegin(); it != array.constEnd(); it++) {
a.setProperty(i++, parseJsonValue(*it));
}
return a;
}
QJSValue JsonCodec::parseJsonObject(const QJsonObject &object)
{
QJSValue o = m_engine->newObject();
for (QJsonObject::ConstIterator it = object.constBegin(); it != object.constEnd(); it++) {
o.setProperty(it.key(), parseJsonValue(it.value()));
}
return o;
}
QJSValue JsonCodec::parseJsonValue(const QJsonValue &val)
{
switch (val.type()) {
case QJsonValue::Array:
return parseJsonArray(val.toArray());
case QJsonValue::Object:
return parseJsonObject(val.toObject());
case QJsonValue::Bool:
return QJSValue(val.toBool());
case QJsonValue::Double:
return QJSValue(val.toDouble());
case QJsonValue::String:
return QJSValue(val.toString());
case QJsonValue::Null:
return QJSValue(QJSValue::NullValue);
case QJsonValue::Undefined:
default:
return QJSValue(QJSValue::UndefinedValue);
}
}
QByteArray JsonCodec::stringify(const QJSValue &json)
{
QJsonDocument doc;
if (json.isArray()) {
doc.setArray(stringifyArray(json));
} else if (json.isObject()) {
doc.setObject(stringifyObject(json));
}
return doc.toJson(QJsonDocument::Compact);
}
QJsonObject JsonCodec::stringifyObject(const QJSValue &json) const
{
QJsonObject object;
JSValueIterator it(json);
while (it.next()) {
object.insert(it.name(), stringifyValue(it.value()));
}
return object;
}
QJsonArray JsonCodec::stringifyArray(const QJSValue &json) const
{
QJsonArray array;
JSValueIterator it(json);
while (it.next()) {
if (it.hasNext()) // skip last item which is length
array.append(stringifyValue(it.value()));
}
return array;
}
QJsonValue JsonCodec::stringifyValue(const QJSValue &json) const
{
if (json.isArray()) {
return QJsonValue(stringifyArray(json));
} else if (json.isObject()) {
QJSValue toJSON = json.property("toJSON");
if (toJSON.isCallable())
return stringifyValue(toJSON.callWithInstance(json));
else
return QJsonValue(stringifyObject(json));
} else if (json.isBool()) {
return QJsonValue(json.toBool());
} else if (json.isNumber()) {
return QJsonValue(json.toNumber());
} else {
return QJsonValue(json.toString());
}
}
FormUrlEncodedCodec::FormUrlEncodedCodec(QQmlEngine *engine) : BodyCodec(engine)
{ }
QByteArray FormUrlEncodedCodec::stringify(const QJSValue &json)
{
QueryItems items;
JSValueIterator it(json);
while (it.next()) {
QString key = it.name();
items << stringifyValue(key, it.value());
}
QUrlQuery query;
query.setQueryItems(items);
return query.toString(QUrl::FullyEncoded).toUtf8();
}
QueryItems FormUrlEncodedCodec::stringifyObject(const QString& prefix, const QJSValue &json) const
{
QueryItems items;
JSValueIterator it(json);
while (it.next()) {
items << stringifyValue(prefix % OPEN_SQUARE % it.name() % CLOSE_SQUARE, it.value());
}
return items;
}
QueryItems FormUrlEncodedCodec::stringifyArray(const QString& prefix, const QJSValue &json) const
{
QueryItems items;
JSValueIterator it(json);
while (it.next()) {
if (it.hasNext()) //skip last item which is length
items << stringifyValue(prefix % OPEN_SQUARE % it.name() % CLOSE_SQUARE, it.value());
}
return items;
}
QueryItems FormUrlEncodedCodec::stringifyValue(const QString& prefix, const QJSValue &json) const
{
if (json.isArray()) {
return stringifyArray(prefix, json);
} else if (json.isObject()) {
QJSValue toJSON = json.property("toJSON");
if (toJSON.isCallable())
return stringifyValue(prefix, toJSON.callWithInstance(json));
else
return stringifyObject(prefix, json);
} else if (json.isNull()) {
return QueryItems() << QPair<QString, QString>(prefix, QString());
} else if (json.hasProperty("toJSON") && json.property("toJSON").isCallable()){
return QueryItems() << QPair<QString, QString>(
prefix, json.property("toJSON").callWithInstance(json).toString());
} else {
return QueryItems() << QPair<QString, QString>(prefix, json.toString());
}
}
QJSValue FormUrlEncodedCodec::parse(const QByteArray &)
{
QJSValue json = m_engine->newObject();
return json;
}
} } }