QJson - a Qt based library for mapping JSON data to QVariant objects

QJson home page

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List

parserrunnable.cpp
1 /* This file is part of qjson
2 *
3 * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License version 2.1, as published by the Free Software Foundation.
8 *
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "parserrunnable.h"
22
23 #include "parser.h"
24
25 #include <QtCore/QDebug>
26 #include <QtCore/QVariant>
27
28 using namespace QJson;
29
30 class QJson::ParserRunnable::Private
31 {
32 public:
33 QByteArray m_data;
34 };
35
36 ParserRunnable::ParserRunnable(QObject* parent)
37 : QObject(parent),
38 QRunnable(),
39 d(new Private)
40 {
41 qRegisterMetaType<QVariant>("QVariant");
42 }
43
44 ParserRunnable::~ParserRunnable()
45 {
46 delete d;
47 }
48
49 void ParserRunnable::setData( const QByteArray& data ) {
50 d->m_data = data;
51 }
52
53 void ParserRunnable::run()
54 {
55 qDebug() << Q_FUNC_INFO;
56
57 bool ok;
58 Parser parser;
59 QVariant result = parser.parse (d->m_data, &ok);
60 if (ok) {
61 qDebug() << "successfully converted json item to QVariant object";
62 emit parsingFinished(result, true, QString());
63 } else {
64 const QString errorText = tr("An error occurred while parsing json: %1").arg(parser.errorString());
65 qCritical() << errorText;
66 emit parsingFinished(QVariant(), false, errorText);
67 }
68 }
QJson::Parser::errorString
QString errorString() const
Definition: parser.cpp:109
QJson::Parser::parse
QVariant parse(QIODevice *io, bool *ok=0)
Definition: parser.cpp:63
QJson::Parser
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:39
QJson
Definition: json_parser.hh:66

| hosts this site. |  | Send comments to: QJson Developers