Merge branch 'master' of ssh://pumpa.branchable.com
[larjonas-pumpa.git] / src / json.cpp
blob2edd0f9806727e9ce898176ce6b79f78cb37f27a
1 /*
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/>.
20 #include "json.h"
22 #ifdef QT5
23 #include <QJsonDocument>
24 #include <QJsonObject>
25 #include <QJsonArray>
26 #else
27 #include <QVariant>
28 #include <qjson/parser.h>
29 #include <qjson/serializer.h>
30 #endif
32 #include <QDebug>
34 //------------------------------------------------------------------------------
36 QVariantMap parseJson(QByteArray data) {
37 #ifdef QT5
38 return QJsonDocument::fromJson(data).object().toVariantMap();
39 #else
40 QJson::Parser parser;
41 bool ok;
43 QVariantMap json = parser.parse(data, &ok).toMap();
44 if (!ok)
45 qDebug() << "WARNING: Unable to parse JSON!" << data;
46 return json;
47 #endif
50 //------------------------------------------------------------------------------
52 QByteArray serializeJson(QVariantMap json) {
53 #ifdef QT5
54 QJsonDocument jd(QJsonObject::fromVariantMap(json));
55 return jd.toJson();
56 #else
57 QJson::Serializer serializer;
58 QByteArray data = serializer.serialize(json);
59 return data;
60 #endif
63 //------------------------------------------------------------------------------
65 const char* serializeJsonC(QVariantMap json) {
66 return QString(serializeJson(json)).toLatin1().data();
69 //------------------------------------------------------------------------------
71 QString debugDumpJson(QVariantMap json, QString name, QString indent) {
72 QString ret = "{";
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 + "}";
81 if (!name.isEmpty())
82 ret += " // end of " + name;
83 return ret;
86 //------------------------------------------------------------------------------
88 QString debugDumpJson(QVariantList json, QString name, QString indent) {
89 QString ret = "[";
91 QVariantList::const_iterator it = json.constBegin();
92 for (; it != json.constEnd(); ++it)
93 ret += debugDumpJson(*it, "", indent);
95 ret += "\n" + indent + "]";
96 if (!name.isEmpty())
97 ret += " // end of " + name;
98 return ret;
101 //------------------------------------------------------------------------------
103 QString debugDumpJson(QVariant json, QString name, QString indent) {
104 int str_max_length = 70-indent.length();
105 QString ret;
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();
114 if (s.length() > ml)
115 s = s.left(ml-5) + " ... ";
116 ret += s;
117 } else if (type == QVariant::List) {
118 ret += debugDumpJson(json.toList(), name, indent + " ");
119 } else
120 ret += "[" + QString(json.typeName()) + "]";
122 return ret;