2 Copyright 2013-2015 Mats Sjöberg
4 This file is part of the Pumpa programme.
6 Pumpa is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Pumpa is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pumpa. If not, see <http://www.gnu.org/licenses/>.
23 #include <QJsonDocument>
24 #include <QJsonObject>
28 #include <qjson/parser.h>
29 #include <qjson/serializer.h>
34 //------------------------------------------------------------------------------
36 QVariantMap
parseJson(QByteArray data
) {
38 return QJsonDocument::fromJson(data
).object().toVariantMap();
43 QVariantMap json
= parser
.parse(data
, &ok
).toMap();
45 qDebug() << "WARNING: Unable to parse JSON!" << data
;
50 //------------------------------------------------------------------------------
52 QByteArray
serializeJson(QVariantMap json
) {
54 QJsonDocument
jd(QJsonObject::fromVariantMap(json
));
57 QJson::Serializer serializer
;
58 QByteArray data
= serializer
.serialize(json
);
63 //------------------------------------------------------------------------------
65 const char* serializeJsonC(QVariantMap json
) {
66 return QString(serializeJson(json
)).toLatin1().data();
69 //------------------------------------------------------------------------------
71 QString
debugDumpJson(QVariantMap json
, QString name
, QString indent
) {
74 QVariantMap::const_iterator it
= json
.constBegin();
75 for (; it
!= json
.constEnd(); ++it
) {
76 ret
+= "\n" + indent
+ " " + it
.key() + ": ";
77 ret
+= debugDumpJson(it
.value(), it
.key(), indent
);
80 ret
+= "\n" + indent
+ "}";
82 ret
+= " // end of " + name
;
86 //------------------------------------------------------------------------------
88 QString
debugDumpJson(QVariantList json
, QString name
, QString indent
) {
91 QVariantList::const_iterator it
= json
.constBegin();
92 for (; it
!= json
.constEnd(); ++it
)
93 ret
+= debugDumpJson(*it
, "", indent
);
95 ret
+= "\n" + indent
+ "]";
97 ret
+= " // end of " + name
;
101 //------------------------------------------------------------------------------
103 QString
debugDumpJson(QVariant json
, QString name
, QString indent
) {
104 int str_max_length
= 70-indent
.length();
107 QVariant::Type type
= json
.type();
108 if (type
== QVariant::Map
) {
109 ret
+= debugDumpJson(json
.toMap(), name
, indent
+ " ");
110 } else if (json
.canConvert
<QString
>()) {
111 int ml
= str_max_length
- name
.length();
112 json
.convert(QVariant::String
);
113 QString s
= json
.toString();
115 s
= s
.left(ml
-5) + " ... ";
117 } else if (type
== QVariant::List
) {
118 ret
+= debugDumpJson(json
.toList(), name
, indent
+ " ");
120 ret
+= "[" + QString(json
.typeName()) + "]";