QJson home page
parser.cpp
1 /* This file is part of QJson
2  *
3  * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
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 "parser.h"
22 #include "parser_p.h"
23 #include "json_parser.hh"
24 #include "json_scanner.h"
25 
26 #include <QtCore/QBuffer>
27 #include <QtCore/QStringList>
28 #include <QtCore/QTextStream>
29 #include <QtCore/QDebug>
30 
31 using namespace QJson;
32 
33 ParserPrivate::ParserPrivate() :
34  m_scanner(0)
35 {
36  m_specialNumbersAllowed = false;
37  reset();
38 }
39 
40 ParserPrivate::~ParserPrivate()
41 {
42  if (m_scanner)
43  delete m_scanner;
44 }
45 
46 void ParserPrivate::setError(QString errorMsg, int errorLine) {
47  m_error = true;
48  m_errorMsg = errorMsg;
49  m_errorLine = errorLine;
50 }
51 
52 void ParserPrivate::reset()
53 {
54  m_error = false;
55  m_errorLine = 0;
56  m_errorMsg.clear();
57  if (m_scanner) {
58  delete m_scanner;
59  m_scanner = 0;
60  }
61 }
62 
63 Parser::Parser() :
64  d(new ParserPrivate)
65 {
66 }
67 
68 Parser::~Parser()
69 {
70  delete d;
71 }
72 
73 QVariant Parser::parse (QIODevice* io, bool* ok)
74 {
75  d->reset();
76 
77  if (!io->isOpen()) {
78  if (!io->open(QIODevice::ReadOnly)) {
79  if (ok != 0)
80  *ok = false;
81  qCritical ("Error opening device");
82  return QVariant();
83  }
84  }
85 
86  if (!io->isReadable()) {
87  if (ok != 0)
88  *ok = false;
89  qCritical ("Device is not readable");
90  io->close();
91  return QVariant();
92  }
93 
94  if (io->atEnd()) {
95  if (ok != 0)
96  *ok = false;
97  d->setError(QLatin1String("No data"), 0);
98  io->close();
99  return QVariant();
100  }
101 
102  d->m_scanner = new JSonScanner (io);
103  d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed);
104  yy::json_parser parser(d);
105  parser.parse();
106 
107  delete d->m_scanner;
108  d->m_scanner = 0;
109 
110  if (ok != 0)
111  *ok = !d->m_error;
112 
113  io->close();
114  return d->m_result;
115 }
116 
117 QVariant Parser::parse(const QByteArray& jsonString, bool* ok) {
118  QBuffer buffer;
119  buffer.open(QBuffer::ReadWrite | QBuffer::Text);
120  buffer.write(jsonString);
121  buffer.seek(0);
122  return parse (&buffer, ok);
123 }
124 
125 QString Parser::errorString() const
126 {
127  return d->m_errorMsg;
128 }
129 
130 int Parser::errorLine() const
131 {
132  return d->m_errorLine;
133 }
134 
135 void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) {
136  d->m_specialNumbersAllowed = allowSpecialNumbers;
137 }
138 
139 bool Parser::specialNumbersAllowed() const {
140  return d->m_specialNumbersAllowed;
141 }
A Bison parser.
Definition: json_parser.hh:90
void allowSpecialNumbers(bool allowSpecialNumbers)
Definition: parser.cpp:135
virtual int parse()
Definition: json_parser.cc:296

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