StelJsonParser.cpp   StelJsonParser.cpp 
skipping to change at line 22 skipping to change at line 22
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, U SA.
*/ */
#include "StelJsonParser.hpp" #include "StelJsonParser.hpp"
#include <QDebug> #include <QDebug>
#include <QBuffer> #include <QBuffer>
#include <QDateTime>
#include <stdexcept> #include <stdexcept>
class StelJsonParserInstance class StelJsonParserInstance
{ {
public: public:
StelJsonParserInstance(QIODevice* ain) : input(ain), hasNextChar(fal se) StelJsonParserInstance(QIODevice* ain) : input(ain), hasNextChar(fal se)
{ {
cur = buffer; cur = buffer;
last = cur; last = cur;
} }
skipping to change at line 250 skipping to change at line 251
return i; return i;
const double d = str.toDouble(&ok); const double d = str.toDouble(&ok);
if (ok) if (ok)
return d; return d;
if (str=="true") if (str=="true")
return QVariant(true); return QVariant(true);
if (str=="false") if (str=="false")
return QVariant(false); return QVariant(false);
if (str=="null") if (str=="null")
return QVariant(); return QVariant();
QDateTime dt = QDateTime::fromString(str, Qt::ISODate);
if (dt.isValid())
return QVariant(dt);
throw std::runtime_error(qPrintable(QString("Invalid JSON value: \"" )+str+"\"")); throw std::runtime_error(qPrintable(QString("Invalid JSON value: \"" )+str+"\""));
} }
// Parse the given input stream // Parse the given input stream
QVariant StelJsonParserInstance::parse() QVariant StelJsonParserInstance::parse()
{ {
skipJson(); skipJson();
char r; char r;
if (!getChar(&r)) if (!getChar(&r))
skipping to change at line 325 skipping to change at line 330
return QString::fromUtf8(ar.constData(), ar.size()); return QString::fromUtf8(ar.constData(), ar.size());
} }
default: default:
{ {
ungetChar(r); ungetChar(r);
return readOther(); return readOther();
} }
} }
} }
QHash<int, void (*)(const QVariant&, QIODevice*, int)> StelJsonParser::othe
rSerializer;
// Serialize the passed QVariant as JSON into the output QIODevice // Serialize the passed QVariant as JSON into the output QIODevice
void StelJsonParser::write(const QVariant& v, QIODevice* output, int indent Level) void StelJsonParser::write(const QVariant& v, QIODevice* output, int indent Level)
{ {
switch (v.type()) switch (v.type())
{ {
case QVariant::Bool: case QVariant::Bool:
output->write(v.toBool()==true ? "true" : "false"); output->write(v.toBool()==true ? "true" : "false");
break; break;
case QVariant::Invalid: case QVariant::Invalid:
output->write("null"); output->write("null");
skipping to change at line 364 skipping to change at line 371
s.replace('\f', "\\f"); s.replace('\f', "\\f");
s.replace('\r', "\\r"); s.replace('\r', "\\r");
s.replace('\t', "\\t"); s.replace('\t', "\\t");
output->write(QString("\"%1\"").arg(s).toUtf8()); output->write(QString("\"%1\"").arg(s).toUtf8());
break; break;
} }
case QVariant::Int: case QVariant::Int:
case QVariant::Double: case QVariant::Double:
output->write(v.toString().toUtf8()); output->write(v.toString().toUtf8());
break; break;
case QVariant::DateTime:
{
output->write(v.toDateTime().toString(Qt::ISODate).t
oUtf8());
break;
}
case QVariant::List: case QVariant::List:
{ {
output->putChar('['); output->putChar('[');
const QVariantList& l = v.toList(); const QVariantList& l = v.toList();
for (int i=0;i<l.size();++i) for (int i=0;i<l.size();++i)
{ {
// Break line if we start an JSON Object for nice looking // Break line if we start an JSON Object for nice looking
if (l.at(i).type()==QVariant::Map) if (l.at(i).type()==QVariant::Map)
output->putChar('\n'); output->putChar('\n');
write(l.at(i), output, indentLevel); write(l.at(i), output, indentLevel);
skipping to change at line 408 skipping to change at line 420
if (++j!=m.size()) if (++j!=m.size())
output->putChar(','); output->putChar(',');
output->putChar('\n'); output->putChar('\n');
} }
output->write(prepend); output->write(prepend);
output->write("}"); output->write("}");
--indentLevel; --indentLevel;
break; break;
} }
default: default:
output->write("null"); QHash<int, void (*)(const QVariant&, QIODevice*, int
)>::ConstIterator iter = otherSerializer.find(v.userType());
if (iter!=otherSerializer.constEnd())
{
iter.value()(v, output, indentLevel);
}
else
output->write("null");
//qDebug() << v.type() << v.userType() << v.typeName
();
//qWarning() << "Cannot serialize QVariant of type " << v.typeName() << " in JSON"; //qWarning() << "Cannot serialize QVariant of type " << v.typeName() << " in JSON";
break; break;
} }
} }
QByteArray StelJsonParser::write(const QVariant& jsonObject, int indentLeve l) QByteArray StelJsonParser::write(const QVariant& jsonObject, int indentLeve l)
{ {
QByteArray ar; QByteArray ar;
QBuffer buf(&ar); QBuffer buf(&ar);
buf.open(QIODevice::WriteOnly); buf.open(QIODevice::WriteOnly);
 End of changes. 5 change blocks. 
1 lines changed or deleted 24 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/