QJson home page
parser.cpp
1/* This file is part of QJson
2 *
3 * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
4 * Copyright (C) 2016 Anton Kudryavtsev <a.kudryavtsev@netris.ru>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software Foundation.
9 *
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "parser.h"
23#include "parser_p.h"
24#include "json_parser.hh"
25#include "json_scanner.h"
26
27#include <QtCore/QBuffer>
28#include <QtCore/QStringList>
29#include <QtCore/QTextStream>
30#include <QtCore/QDebug>
31
32using namespace QJson;
33
34ParserPrivate::ParserPrivate() :
35 m_scanner(0),
36 m_specialNumbersAllowed(false)
37{
38 reset();
39}
40
41ParserPrivate::~ParserPrivate()
42{
43 if (m_scanner)
44 delete m_scanner;
45}
46
47void ParserPrivate::setError(const QString &errorMsg, int errorLine) {
48 m_error = true;
49 m_errorMsg = errorMsg;
50 m_errorLine = errorLine;
51}
52
53void ParserPrivate::reset()
54{
55 m_error = false;
56 m_errorLine = 0;
57 m_errorMsg.clear();
58 if (m_scanner) {
59 delete m_scanner;
60 m_scanner = 0;
61 }
62}
63
64Parser::Parser() :
65 d(new ParserPrivate)
66{
67}
68
69Parser::~Parser()
70{
71 delete d;
72}
73
74QVariant Parser::parse (QIODevice* io, bool* ok)
75{
76 d->reset();
77
78 if (!io->isOpen()) {
79 if (!io->open(QIODevice::ReadOnly)) {
80 if (ok != 0)
81 *ok = false;
82 qCritical ("Error opening device");
83 return QVariant();
84 }
85 }
86
87 if (!io->isReadable()) {
88 if (ok != 0)
89 *ok = false;
90 qCritical ("Device is not readable");
91 io->close();
92 return QVariant();
93 }
94
95 if (io->atEnd()) {
96 if (ok != 0)
97 *ok = false;
98 d->setError(QLatin1String("No data"), 0);
99 io->close();
100 return QVariant();
101 }
102
103 d->m_scanner = new JSonScanner (io);
104 d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed);
105 yy::json_parser parser(d);
106 parser.parse();
107
108 delete d->m_scanner;
109 d->m_scanner = 0;
110
111 if (ok != 0)
112 *ok = !d->m_error;
113
114 io->close();
115 return d->m_result;
116}
117
118QVariant Parser::parse(const QByteArray& jsonString, bool* ok) {
119 QBuffer buffer;
120 buffer.open(QBuffer::ReadWrite | QBuffer::Text);
121 buffer.write(jsonString);
122 buffer.seek(0);
123 return parse (&buffer, ok);
124}
125
126QString Parser::errorString() const
127{
128 return d->m_errorMsg;
129}
130
132{
133 return d->m_errorLine;
134}
135
136void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) {
137 d->m_specialNumbersAllowed = allowSpecialNumbers;
138}
139
141 return d->m_specialNumbersAllowed;
142}
int errorLine() const
Definition parser.cpp:131
QVariant parse(QIODevice *io, bool *ok=0)
Definition parser.cpp:74
void allowSpecialNumbers(bool allowSpecialNumbers)
Definition parser.cpp:136
bool specialNumbersAllowed() const
Definition parser.cpp:140
QString errorString() const
Definition parser.cpp:126
A Bison parser.
virtual int parse()

SourceForge Logo hosts this site. Send comments to:
QJson Developers