TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
parser.cpp
Go to the documentation of this file.
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 
31 using namespace QJson;
32 
34  m_scanner(0),
35  m_specialNumbersAllowed(false)
36 {
37  reset();
38 }
39 
41 {
42  if (m_scanner)
43  delete m_scanner;
44 }
45 
46 void ParserPrivate::setError(const QString &errorMsg, int errorLine)
47 {
48  m_error = true;
49  m_errorMsg = errorMsg;
50  m_errorLine = errorLine;
51 }
52 
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 
65 {
67 }
68 
69 QVariant Parser::parse(QIODevice* io, bool* ok)
70 {
71  TTK_D(Parser);
72  d->reset();
73 
74  if (!io->isOpen()) {
75  if (!io->open(QIODevice::ReadOnly)) {
76  if (ok != 0)
77  *ok = false;
78  qCritical ("Error opening device");
79  return QVariant();
80  }
81  }
82 
83  if (!io->isReadable()) {
84  if (ok != 0)
85  *ok = false;
86  qCritical ("Device is not readable");
87  io->close();
88  return QVariant();
89  }
90 
91  if (io->atEnd()) {
92  if (ok != 0)
93  *ok = false;
94  d->setError(QLatin1String("No data"), 0);
95  io->close();
96  return QVariant();
97  }
98 
99  d->m_scanner = new JSonScanner (io);
100  d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed);
101  yy::json_parser parser(d);
102  parser.parse();
103 
104  delete d->m_scanner;
105  d->m_scanner = 0;
106 
107  if (ok != 0)
108  *ok = !d->m_error;
109 
110  io->close();
111  return d->m_result;
112 }
113 
114 QVariant Parser::parse(const QByteArray &jsonString, bool* ok)
115 {
116  QBuffer buffer;
117  buffer.open(QIODevice::ReadWrite | QIODevice::Text);
118  buffer.write(jsonString);
119  buffer.seek(0);
120  return parse (&buffer, ok);
121 }
122 
123 QString Parser::errorString() const
124 {
125  TTK_D(Parser);
126  return d->m_errorMsg;
127 }
128 
129 int Parser::errorLine() const
130 {
131  TTK_D(Parser);
132  return d->m_errorLine;
133 }
134 
135 void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers)
136 {
137  TTK_D(Parser);
138  d->m_specialNumbersAllowed = allowSpecialNumbers;
139 }
140 
142 {
143  TTK_D(Parser);
144  return d->m_specialNumbersAllowed;
145 }
QString errorString() const
This method returns the error message.
Definition: parser.cpp:123
void setError(const QString &errorMsg, int line)
Definition: parser.cpp:46
JSonScanner * m_scanner
Definition: parser_p.h:51
bool specialNumbersAllowed() const
Definition: parser.cpp:141
QString m_errorMsg
Definition: parser_p.h:54
Namespace used by QJson.
Definition: parser.h:34
QVariant parse(QIODevice *io, bool *ok=0)
Read JSON string from the I/O Device and converts it to a QVariant object.
Definition: parser.cpp:69
void allowSpecialNumbers(bool allowSpecialNumbers)
Sets whether special numbers (Infinity, -Infinity, NaN) are allowed as an extension to the standard...
Definition: parser.cpp:135
int errorLine() const
This method returns line number where the error occurred.
Definition: parser.cpp:129
#define TTK_INIT_PRIVATE(Class)
Definition: ttkprivate.h:33
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
#define TTK_D(Class)
Definition: ttkprivate.h:41