Skip to content

Commit

Permalink
Use classes from json.h to squeeze JSON parsing code
Browse files Browse the repository at this point in the history
  • Loading branch information
KitsuneRal committed Aug 15, 2016
1 parent 85866fd commit 4f8edd0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
5 changes: 3 additions & 2 deletions jobs/passwordlogin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QtNetwork/QNetworkReply>

#include "../connectiondata.h"
#include "../json.h"

using namespace QMatrixClient;

Expand Down Expand Up @@ -82,8 +83,8 @@ QJsonObject PasswordLogin::data() const

BaseJob::Status PasswordLogin::parseJson(const QJsonDocument& data)
{
QJsonObject json = data.object();
if( !json.contains("access_token") || !json.contains("home_server") || !json.contains("user_id") )
JsonObject json { data };
if( !json.containsAll({"access_token", "home_server", "user_id"}) )
{
return { UserDefinedError, "No expected data" };
}
Expand Down
19 changes: 9 additions & 10 deletions jobs/syncjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@

#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonValue>
#include <QtCore/QJsonArray>
#include <QtCore/QDebug>

#include "../room.h"
#include "../connectiondata.h"
#include "../events/event.h"
#include "../json.h"

using namespace QMatrixClient;

Expand Down Expand Up @@ -112,11 +111,10 @@ QUrlQuery SyncJob::query() const

BaseJob::Status SyncJob::parseJson(const QJsonDocument& data)
{
QJsonObject json = data.object();
const JsonObject json { data };
d->nextBatch = json.value("next_batch").toString();
// TODO: presence
// TODO: account_data
QJsonObject rooms = json.value("rooms").toObject();

const struct { QString jsonKey; JoinState enumVal; } roomStates[]
{
Expand All @@ -126,21 +124,22 @@ BaseJob::Status SyncJob::parseJson(const QJsonDocument& data)
};
for (auto roomState: roomStates)
{
const QJsonObject rs = rooms.value(roomState.jsonKey).toObject();
const JsonObject rs = json["rooms"][roomState.jsonKey];
d->roomData.reserve(rs.size());
for( auto r = rs.begin(); r != rs.end(); ++r )
for( auto r: rs )
{
d->roomData.push_back({r.key(), r.value().toObject(), roomState.enumVal});
d->roomData.push_back(
SyncRoomData(r.key(), r.value().toObject(), roomState.enumVal));
}
}

return Success;
}

void SyncRoomData::EventList::fromJson(const QJsonObject& roomContents)
void SyncRoomData::EventList::fromJson(const JsonObject& allLists)
{
auto l = eventListFromJson(roomContents[jsonKey].toObject()["events"].toArray());
swap(l);
auto l = eventListFromJson(allLists[jsonKey]["events"].toArray());
swap(l); // Only swaps QList representations, not affecting jsonKey
}

SyncRoomData::SyncRoomData(QString roomId_, const QJsonObject& room_, JoinState joinState_)
Expand Down
3 changes: 2 additions & 1 deletion jobs/syncjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace QMatrixClient
{
class Event;
class JsonObject;

class SyncRoomData
{
Expand All @@ -36,7 +37,7 @@ namespace QMatrixClient
QString jsonKey;
public:
explicit EventList(QString k) : jsonKey(k) { }
void fromJson(const QJsonObject& roomContents);
void fromJson(const JsonObject& allLists);
};

QString roomId;
Expand Down
18 changes: 16 additions & 2 deletions json.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ namespace QMatrixClient

// A couple of examples how to specialise JsonValue::to() in your code
template <>
QUrl JsonValue::to() const
inline QUrl JsonValue::to() const
{
return QUrl(toString());
}

template <>
QDateTime JsonValue::to() const
inline QDateTime JsonValue::to() const
{
return QDateTime::fromMSecsSinceEpoch( qint64(toDouble()) );
}
Expand Down Expand Up @@ -123,6 +123,20 @@ namespace QMatrixClient

JsonValue operator[] (const QString &key) const { return value(key); }

template <typename ContT>
bool containsAll(const ContT& keyList) const
{
return std::all_of(keyList.begin(), keyList.end(), &JsonObject::contains);
}

// A special overload for std::initializer_list because it's not
// automatically deduced
template <typename T>
bool containsAll(const std::initializer_list<T>& keyList) const
{
return containsAll(keyList);
}

// STL compatibility
using value_type = JsonPair;
using key_type = JsonPair::first_type;
Expand Down

0 comments on commit 4f8edd0

Please sign in to comment.