1 /*
2 * Copyright (C) 2008 Fabien Chereau
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
17 */
18
19 #include "StelJsonParser.hpp"
20 #include <QDebug>
21 #include <QJsonDocument>
22 #include <stdexcept>
23
write(const QVariant & v,QIODevice * output,int indentLevel)24 void StelJsonParser::write(const QVariant& v, QIODevice* output, int indentLevel)
25 {
26 QByteArray json = write(v, indentLevel);
27 output->write(json);
28 }
29
write(const QVariant & jsonObject,int indentLevel)30 QByteArray StelJsonParser::write(const QVariant& jsonObject, int indentLevel)
31 {
32 Q_UNUSED(indentLevel)
33 QJsonDocument doc = QJsonDocument::fromVariant(jsonObject);
34 return doc.toJson();
35 }
36
parse(QIODevice * input)37 QVariant StelJsonParser::parse(QIODevice* input)
38 {
39 QByteArray data = input->readAll();
40 return parse(data);
41 }
42
parse(const QByteArray & aar)43 QVariant StelJsonParser::parse(const QByteArray& aar)
44 {
45 QJsonParseError error;
46 QJsonDocument doc = QJsonDocument::fromJson(aar, &error);
47 if (error.error != QJsonParseError::NoError)
48 {
49 throw std::runtime_error((QString("%1 at offset %2").arg(error.errorString()).arg(error.offset)).toLatin1().constData());
50 }
51 return doc.toVariant();
52 }
53