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 , m_negate(false) 00036 , m_error(false) 00037 , m_errorLine(0) 00038 , m_specialNumbersAllowed(false) 00039 { 00040 } 00041 00042 ParserPrivate::~ParserPrivate() 00043 { 00044 delete m_scanner; 00045 } 00046 00047 void ParserPrivate::setError(QString errorMsg, int errorLine) { 00048 m_error = true; 00049 m_errorMsg = errorMsg; 00050 m_errorLine = errorLine; 00051 } 00052 00053 Parser::Parser() : 00054 d(new ParserPrivate) 00055 { 00056 } 00057 00058 Parser::~Parser() 00059 { 00060 delete d; 00061 } 00062 00063 QVariant Parser::parse (QIODevice* io, bool* ok) 00064 { 00065 d->m_errorMsg.clear(); 00066 delete d->m_scanner; 00067 d->m_scanner = 0; 00068 00069 if (!io->isOpen()) { 00070 if (!io->open(QIODevice::ReadOnly)) { 00071 if (ok != 0) 00072 *ok = false; 00073 qCritical ("Error opening device"); 00074 return QVariant(); 00075 } 00076 } 00077 00078 if (!io->isReadable()) { 00079 if (ok != 0) 00080 *ok = false; 00081 qCritical ("Device is not readable"); 00082 io->close(); 00083 return QVariant(); 00084 } 00085 00086 d->m_scanner = new JSonScanner (io); 00087 d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed); 00088 yy::json_parser parser(d); 00089 parser.parse(); 00090 00091 delete d->m_scanner; 00092 d->m_scanner = 0; 00093 00094 if (ok != 0) 00095 *ok = !d->m_error; 00096 00097 io->close(); 00098 return d->m_result; 00099 } 00100 00101 QVariant Parser::parse(const QByteArray& jsonString, bool* ok) { 00102 QBuffer buffer; 00103 buffer.open(QBuffer::ReadWrite); 00104 buffer.write(jsonString); 00105 buffer.seek(0); 00106 return parse (&buffer, ok); 00107 } 00108 00109 QString Parser::errorString() const 00110 { 00111 return d->m_errorMsg; 00112 } 00113 00114 int Parser::errorLine() const 00115 { 00116 return d->m_errorLine; 00117 } 00118 00119 void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) { 00120 d->m_specialNumbersAllowed = allowSpecialNumbers; 00121 } 00122 00123 bool Parser::specialNumbersAllowed() const { 00124 return d->m_specialNumbersAllowed; 00125 }
|
hosts this site. |
Send comments to: QJson Developers |