This repository has been archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
json.cpp
139 lines (122 loc) · 4.46 KB
/
json.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
/**
* QPSNProxy is an open source software for downloading PSN packages
* on a PC, then transfer them to a game console via a HTTP proxy.
*
* Copyright (C) 2014 codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "json.h"
#if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0)
#include <QJsonDocument>
QVariantMap json_decode(const QString &jsonStr)
{
QJsonDocument d = QJsonDocument::fromJson(jsonStr.toUtf8());
return d.toVariant().toMap();
}
QByteArray json_encode(const QVariantList &json)
{
return QJsonDocument::fromVariant(json).toJson();
}
QByteArray json_encode(const QVariantMap &json)
{
return QJsonDocument::fromVariant(json).toJson();
}
#else
#include <QScriptEngine>
#include <QScriptValueIterator>
static QVariantMap decodeInner(QScriptValue object);
static QVariantList decodeInnerToList(QScriptValue arrayValue)
{
QVariantList list;
QScriptValueIterator it(arrayValue);
while (it.hasNext()) {
it.next();
if (it.name() == "length")
continue;
if (it.value().isArray())
list.append(QVariant(decodeInnerToList(it.value())));
else if (it.value().isNumber())
list.append(QVariant(it.value().toNumber()));
else if (it.value().isString())
list.append(QVariant(it.value().toString()));
else if (it.value().isNull())
list.append(QVariant());
else if(it.value().isObject())
list.append(QVariant(decodeInner(it.value())));
}
return list;
}
static QVariantMap decodeInner(QScriptValue object)
{
QVariantMap map;
QScriptValueIterator it(object);
while (it.hasNext()) {
it.next();
if (it.value().isArray())
map.insert(it.name(),QVariant(decodeInnerToList(it.value())));
else if (it.value().isNumber())
map.insert(it.name(),QVariant(it.value().toNumber()));
else if (it.value().isString())
map.insert(it.name(),QVariant(it.value().toString()));
else if (it.value().isNull())
map.insert(it.name(),QVariant());
else if(it.value().isObject())
map.insert(it.name(),QVariant(decodeInner(it.value())));
}
return map;
}
static QScriptValue encodeInner(const QVariantMap &map, QScriptEngine* engine)
{
QScriptValue obj = engine->newObject();
QMapIterator<QString, QVariant> i(map);
while (i.hasNext()) {
i.next();
if (i.value().type() == QVariant::String)
obj.setProperty(i.key(), i.value().toString());
else if (i.value().type() == QVariant::Int)
obj.setProperty(i.key(), i.value().toInt());
else if (i.value().type() == QVariant::Double)
obj.setProperty(i.key(), i.value().toDouble());
else if (i.value().type() == QVariant::List)
obj.setProperty(i.key(), qScriptValueFromSequence(engine, i.value().toList()));
else if (i.value().type() == QVariant::Map)
obj.setProperty(i.key(), encodeInner(i.value().toMap(), engine));
}
return obj;
}
QVariantMap json_decode(const QString &jsonStr)
{
QScriptValue object;
QScriptEngine engine;
object = engine.evaluate("(" + jsonStr + ")");
return decodeInner(object);
}
QByteArray json_encode(const QVariantMap &json)
{
QScriptEngine engine;
engine.evaluate("function toString() { return JSON.stringify(this) }");
QScriptValue toString = engine.globalObject().property("toString");
QScriptValue obj = encodeInner(json, &engine);
return qPrintable(toString.call(obj).toString());
}
QByteArray json_encode(const QVariantList &json)
{
QScriptEngine engine;
engine.evaluate("function toString() { return JSON.stringify(this) }");
QScriptValue toString = engine.globalObject().property("toString");
QScriptValue obj = qScriptValueFromSequence(&engine, json);
return qPrintable(toString.call(obj).toString());
}
#endif