00001 /* This file is part of QJson 00002 * 00003 * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License version 2.1, as published by the Free Software Foundation. 00008 * 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "parser.h" 00022 #include "parser_p.h" 00023 #include "json_parser.hh" 00024 #include "json_scanner.h" 00025 00026 #include <QtCore/QBuffer> 00027 #include <QtCore/QStringList> 00028 #include <QtCore/QTextStream> 00029 #include <QtCore/QDebug> 00030 00031 using namespace QJson; 00032 00033 ParserPrivate::ParserPrivate() : 00034 m_scanner(0) 00035 { 00036 m_specialNumbersAllowed = false; 00037 reset(); 00038 } 00039 00040 ParserPrivate::~ParserPrivate() 00041 { 00042 if (m_scanner) 00043 delete m_scanner; 00044 } 00045 00046 void ParserPrivate::setError(QString errorMsg, int errorLine) { 00047 m_error = true; 00048 m_errorMsg = errorMsg; 00049 m_errorLine = errorLine; 00050 } 00051 00052 void ParserPrivate::reset() 00053 { 00054 m_error = false; 00055 m_errorLine = 0; 00056 m_errorMsg.clear(); 00057 if (m_scanner) { 00058 delete m_scanner; 00059 m_scanner = 0; 00060 } 00061 } 00062 00063 Parser::Parser() : 00064 d(new ParserPrivate) 00065 { 00066 } 00067 00068 Parser::~Parser() 00069 { 00070 delete d; 00071 } 00072 00073 QVariant Parser::parse (QIODevice* io, bool* ok) 00074 { 00075 d->reset(); 00076 00077 if (!io->isOpen()) { 00078 if (!io->open(QIODevice::ReadOnly)) { 00079 if (ok != 0) 00080 *ok = false; 00081 qCritical ("Error opening device"); 00082 return QVariant(); 00083 } 00084 } 00085 00086 if (!io->isReadable()) { 00087 if (ok != 0) 00088 *ok = false; 00089 qCritical ("Device is not readable"); 00090 io->close(); 00091 return QVariant(); 00092 } 00093 00094 if (io->atEnd()) { 00095 if (ok != 0) 00096 *ok = false; 00097 d->setError(QLatin1String("No data"), 0); 00098 io->close(); 00099 return QVariant(); 00100 } 00101 00102 d->m_scanner = new JSonScanner (io); 00103 d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed); 00104 yy::json_parser parser(d); 00105 parser.parse(); 00106 00107 delete d->m_scanner; 00108 d->m_scanner = 0; 00109 00110 if (ok != 0) 00111 *ok = !d->m_error; 00112 00113 io->close(); 00114 return d->m_result; 00115 } 00116 00117 QVariant Parser::parse(const QByteArray& jsonString, bool* ok) { 00118 QBuffer buffer; 00119 buffer.open(QBuffer::ReadWrite | QBuffer::Text); 00120 buffer.write(jsonString); 00121 buffer.seek(0); 00122 return parse (&buffer, ok); 00123 } 00124 00125 QString Parser::errorString() const 00126 { 00127 return d->m_errorMsg; 00128 } 00129 00130 int Parser::errorLine() const 00131 { 00132 return d->m_errorLine; 00133 } 00134 00135 void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) { 00136 d->m_specialNumbersAllowed = allowSpecialNumbers; 00137 } 00138 00139 bool Parser::specialNumbersAllowed() const { 00140 return d->m_specialNumbersAllowed; 00141 }
|
hosts this site. |
Send comments to: QJson Developers |