d09423e4cbb6c1ab5a7ea697062dedc4ffabc10e
[buliscores] / qjson / src / 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 Library General Public
7   * License as published by the Free Software Foundation; either
8   * version 2 of the License, or (at your option) any later version.
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   * Library General Public License for more details.
14   *
15   * You should have received a copy of the GNU Library 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 occured while parsing json: %1").arg(parser.errorString());
65     qCritical() << errorText;
66     emit parsingFinished(QVariant(), false, errorText);
67   }
68 }