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 
32 using namespace QJson;
33 
34 ParserPrivate::ParserPrivate() :
35  m_scanner(0),
36  m_specialNumbersAllowed(false)
37 {
38  reset();
39 }
40 
41 ParserPrivate::~ParserPrivate()
42 {
43  if (m_scanner)
44  delete m_scanner;
45 }
46 
47 void ParserPrivate::setError(const QString &errorMsg, int errorLine) {
48  m_error = true;
49  m_errorMsg = errorMsg;
50  m_errorLine = errorLine;
51 }
52 
53 void 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 
64 Parser::Parser() :
65  d(new ParserPrivate)
66 {
67 }
68 
69 Parser::~Parser()
70 {
71  delete d;
72 }
73 
74 QVariant 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 
118 QVariant 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 
126 QString Parser::errorString() const
127 {
128  return d->m_errorMsg;
129 }
130 
131 int Parser::errorLine() const
132 {
133  return d->m_errorLine;
134 }
135 
136 void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) {
137  d->m_specialNumbersAllowed = allowSpecialNumbers;
138 }
139 
141  return d->m_specialNumbersAllowed;
142 }
QJson
Definition: json_parser.hh:60
yy::json_parser
A Bison parser.
Definition: json_parser.hh:91
json_parser.hh
QJson::Parser::parse
QVariant parse(QIODevice *io, bool *ok=0)
Definition: parser.cpp:74
QJson::Parser::errorLine
int errorLine() const
Definition: parser.cpp:131
QJson::Parser::allowSpecialNumbers
void allowSpecialNumbers(bool allowSpecialNumbers)
Definition: parser.cpp:136
QJson::Parser::errorString
QString errorString() const
Definition: parser.cpp:126
QJson::Parser::specialNumbersAllowed
bool specialNumbersAllowed() const
Definition: parser.cpp:140
yy::json_parser::parse
virtual int parse()
Definition: json_parser.cc:296

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