Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty replies from Resrobot server #272

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/parser/parser_abstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ void ParserAbstract::sendHttpRequest(QUrl url, QByteArray data, const QList<QPai
connect(lastRequest, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(networkReplyDownloadProgress(qint64,qint64)));
}

QVariantMap ParserAbstract::parseJson(const QByteArray &json) const
QVariant ParserAbstract::parseJson(const QByteArray &json) const
{
QVariantMap doc;
QVariant doc;
#ifdef BUILD_FOR_QT5
doc = QJsonDocument::fromJson(json).toVariant().toMap();
doc = QJsonDocument::fromJson(json).toVariant();
#else
QString utf8(QString::fromUtf8(json));

Expand All @@ -160,17 +160,17 @@ QVariantMap ParserAbstract::parseJson(const QByteArray &json) const
return doc;

QScriptEngine *engine = new QScriptEngine();
doc = engine->evaluate("(" + utf8 + ")").toVariant().toMap();
doc = engine->evaluate("(" + utf8 + ")").toVariant();
delete engine;
#endif

return doc;
}

#ifdef BUILD_FOR_QT5
QByteArray ParserAbstract::serializeToJson(const QVariantMap& doc) const
QByteArray ParserAbstract::serializeToJson(const QVariant& doc) const
{
return QJsonDocument(QJsonObject::fromVariantMap(doc)).toJson(QJsonDocument::Indented);
return QJsonDocument::fromVariant(doc).toJson(QJsonDocument::Indented);
}
#else
QByteArray toJson(const QVariant& value)
Expand Down Expand Up @@ -212,7 +212,7 @@ QByteArray toJson(const QVariant& value)
}
}

QByteArray ParserAbstract::serializeToJson(const QVariantMap& doc) const
QByteArray ParserAbstract::serializeToJson(const QVariant& doc) const
{
return toJson(doc);
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parser_abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected slots:
virtual void parseJourneyDetails(QNetworkReply *networkReply);
void sendHttpRequest(QUrl url, QByteArray data, const QList<QPair<QByteArray,QByteArray> > &additionalHeaders = QList<QPair<QByteArray,QByteArray> >());
void sendHttpRequest(QUrl url);
QVariantMap parseJson(const QByteArray &data) const;
QByteArray serializeToJson(const QVariantMap &doc) const;
QVariant parseJson(const QByteArray &data) const;
QByteArray serializeToJson(const QVariant &doc) const;
QByteArray gzipDecompress(QByteArray compressData);

#ifdef BUILD_FOR_UBUNTU
Expand Down
6 changes: 3 additions & 3 deletions src/parser/parser_finland_matka.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void ParserFinlandMatka::parseStationsByName(QNetworkReply *networkReply)
}
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData);
QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
Expand Down Expand Up @@ -299,7 +299,7 @@ void ParserFinlandMatka::parseTimeTable(QNetworkReply *networkReply)
}
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData);
QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
Expand Down Expand Up @@ -664,7 +664,7 @@ void ParserFinlandMatka::parseSearchJourney(QNetworkReply *networkReply)
}
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData);
QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/parser/parser_ninetwo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void ParserNinetwo::parseTimeTable(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "REPLY:>>>>>>>>>>>>\n" << allData;

QVariantMap doc = parseJson(allData);
QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
Expand Down Expand Up @@ -287,7 +287,7 @@ void ParserNinetwo::parseStationsByName(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "REPLY:>>>>>>>>>>>>\n" << allData;

QVariantMap doc = parseJson(allData);
QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
Expand Down Expand Up @@ -346,7 +346,7 @@ void ParserNinetwo::parseSearchJourney(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
qDebug() << "REPLY:>>>>>>>>>>>>\n" << allData;

QVariantMap doc = parseJson(allData);
QVariantMap doc = parseJson(allData).toMap();
if (doc.isEmpty()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
Expand Down
37 changes: 19 additions & 18 deletions src/parser/parser_resrobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,17 @@ void ParserResRobot::parseTimeTable(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData);
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}

QVariantMap replyMap = reply.toMap();
QVariantList departures;
if (timetableSearchMode == Arrival)
departures = doc.value("Arrival").toList();
departures = replyMap.value("Arrival").toList();
else
departures = doc.value("Departure").toList();
departures = replyMap.value("Departure").toList();
TimetableEntriesList timetable;
foreach (QVariant departureData, departures) {
TimetableEntry resultItem;
Expand Down Expand Up @@ -439,12 +439,13 @@ void ParserResRobot::parseStationsByName(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData);
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}
QVariantList stations = doc.value("StopLocation").toList();
QVariantMap replyMap = reply.toMap();
QVariantList stations = replyMap.value("StopLocation").toList();
StationsList result;
foreach (QVariant stationData, stations) {
const QVariantMap& station = stationData.toMap();
Expand All @@ -464,13 +465,13 @@ void ParserResRobot::parseStationsByCoordinates(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData);
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}

QVariantList stations = doc.value("StopLocation").toList();
QVariantMap replyMap = reply.toMap();
QVariantList stations = replyMap.value("StopLocation").toList();
StationsList result;
foreach (QVariant stationData, stations) {
const QVariantMap& station = stationData.toMap();
Expand All @@ -490,15 +491,15 @@ void ParserResRobot::parseSearchJourney(QNetworkReply *networkReply)
QByteArray allData = networkReply->readAll();
// qDebug() << "Reply:\n" << allData;

QVariantMap doc = parseJson(allData);
if (doc.isEmpty()) {
QVariant reply = parseJson(allData);
if (!reply.isValid()) {
emit errorOccured(tr("Cannot parse reply from the server"));
return;
}

searchEarlierReference = doc.value("scrB").toString();
searchLaterReference = doc.value("scrF").toString();
QVariantList journeyListData = doc.value("Trip").toList();
QVariantMap replyMap = reply.toMap();
searchEarlierReference = replyMap.value("scrB").toString();
searchLaterReference = replyMap.value("scrF").toString();
QVariantList journeyListData = replyMap.value("Trip").toList();

cachedResults.clear();

Expand Down