forked from Cutehacks/duperagent
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.cpp
More file actions
Latest commit
203 lines (177 loc) · 5.78 KB
/
Copy pathserialization.cpp
File metadata and controls
203 lines (177 loc) · 5.78 KB
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
namespacecom { namespacecutehacks { namespaceduperagent {
staticconst QChar OPEN_SQUARE('[');
staticconst 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);
returnparseJsonDocument(doc);
}
QJSValue JsonCodec::parseJsonDocument(const QJsonDocument &doc)
{
if (doc.isObject()) {
returnparseJsonObject(doc.object());
} elseif (doc.isArray()) {
returnparseJsonArray(doc.array());
} elseif(doc.isNull()){
returnQJSValue(QJSValue::NullValue);
} else {
returnQJSValue();
}
}
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:
returnparseJsonArray(val.toArray());
case QJsonValue::Object:
returnparseJsonObject(val.toObject());
case QJsonValue::Bool:
returnQJSValue(val.toBool());
case QJsonValue::Double:
returnQJSValue(val.toDouble());
case QJsonValue::String:
returnQJSValue(val.toString());
case QJsonValue::Null:
returnQJSValue(QJSValue::NullValue);
case QJsonValue::Undefined:
default:
returnQJSValue(QJSValue::UndefinedValue);
}
}
QByteArray JsonCodec::stringify(const QJSValue &json)
{
QJsonDocument doc;
if (json.isArray()) {
doc.setArray(stringifyArray(json));
} elseif (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()) {
returnQJsonValue(stringifyArray(json));
} elseif (json.isObject()) {
QJSValue toJSON = json.property("toJSON");
if (toJSON.isCallable())
returnstringifyValue(toJSON.callWithInstance(json));
else
returnQJsonValue(stringifyObject(json));
} elseif (json.isBool()) {
returnQJsonValue(json.toBool());
} elseif (json.isNumber()) {
returnQJsonValue(json.toNumber());
} else {
returnQJsonValue(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()) {
returnstringifyArray(prefix, json);
} elseif (json.isObject()) {
QJSValue toJSON = json.property("toJSON");
if (toJSON.isCallable())
returnstringifyValue(prefix, toJSON.callWithInstance(json));
else
returnstringifyObject(prefix, json);
} elseif (json.isNull()) {
returnQueryItems() << QPair<QString, QString>(prefix, QString());
} elseif (json.hasProperty("toJSON") && json.property("toJSON").isCallable()){
returnQueryItems() << QPair<QString, QString>(
prefix, json.property("toJSON").callWithInstance(json).toString());
} else {
returnQueryItems() << QPair<QString, QString>(prefix, json.toString());
}
}
QJSValue FormUrlEncodedCodec::parse(const QByteArray &)
{
QJSValue json = m_engine->newObject();
return json;
}
} } }