Move TwitchUser deserialization to the proper Deserialize function

instead of ::fromJSON
This commit is contained in:
Rasmus Karlsson 2018-06-27 00:17:05 +00:00
parent f76512c31e
commit e9868fdd84
3 changed files with 58 additions and 27 deletions

View file

@ -106,7 +106,13 @@ void TwitchAccount::loadIgnores()
if (userIt == block.MemberEnd()) {
continue;
}
this->ignores.insert(TwitchUser::fromJSON(userIt->value));
TwitchUser ignoredUser;
if (!rj::getSafe(userIt->value, ignoredUser)) {
Log("Error parsing twitch user JSON {}", rj::stringify(userIt->value));
continue;
}
this->ignores.insert(ignoredUser);
}
}
@ -155,7 +161,12 @@ void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targe
return false;
}
auto ignoredUser = TwitchUser::fromJSON(userIt->value);
TwitchUser ignoredUser;
if (!rj::getSafe(userIt->value, ignoredUser)) {
onFinished(IgnoreResult_Failed,
"Bad JSON data while ignoring user (invalid user) " + targetName);
return false;
}
{
std::lock_guard<std::mutex> lock(this->ignoresMutex);

View file

@ -4,27 +4,4 @@
namespace chatterino {
TwitchUser TwitchUser::fromJSON(const rapidjson::Value &value)
{
TwitchUser user;
if (!value.IsObject()) {
throw std::runtime_error("JSON value is not an object");
}
if (!rj::getSafe(value, "_id", user.id)) {
throw std::runtime_error("Missing ID key");
}
if (!rj::getSafe(value, "name", user.name)) {
throw std::runtime_error("Missing name key");
}
if (!rj::getSafe(value, "display_name", user.displayName)) {
throw std::runtime_error("Missing display name key");
}
return user;
}
} // namespace chatterino

View file

@ -1,7 +1,10 @@
#pragma once
#include "util/RapidjsonHelpers.hpp"
#include <rapidjson/document.h>
#include <QString>
#include <pajlada/settings/serialize.hpp>
#include <cassert>
@ -20,8 +23,6 @@ struct TwitchUser {
this->displayName = other.displayName;
}
static TwitchUser fromJSON(const rapidjson::Value &value);
bool operator<(const TwitchUser &rhs) const
{
return this->id < rhs.id;
@ -29,3 +30,45 @@ struct TwitchUser {
};
} // namespace chatterino
namespace pajlada {
namespace Settings {
template <>
struct Deserialize<chatterino::TwitchUser> {
static chatterino::TwitchUser get(const rapidjson::Value &value, bool *error = nullptr)
{
using namespace chatterino;
TwitchUser user;
if (!value.IsObject()) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Deserialized rapidjson::Value is wrong type");
return user;
}
if (!rj::getSafe(value, "_id", user.id)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing ID key");
return user;
}
if (!rj::getSafe(value, "name", user.name)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing name key");
return user;
}
if (!rj::getSafe(value, "display_name", user.displayName)) {
PAJLADA_REPORT_ERROR(error)
PAJLADA_THROW_EXCEPTION("Missing display name key");
return user;
}
return user;
}
};
} // namespace Settings
} // namespace pajlada