mirror-chatterino2/src/util/RapidJsonSerializeQString.hpp

44 lines
1 KiB
C++
Raw Normal View History

#pragma once
#include <QString>
#include <pajlada/settings/serialize.hpp>
namespace pajlada {
namespace Settings {
template <>
struct Serialize<QString> {
2018-08-06 21:17:03 +02:00
static rapidjson::Value get(const QString &value,
rapidjson::Document::AllocatorType &a)
{
return rapidjson::Value(value.toUtf8(), a);
}
};
template <>
struct Deserialize<QString> {
static QString get(const rapidjson::Value &value, bool *error = nullptr)
{
if (!value.IsString()) {
PAJLADA_REPORT_ERROR(error)
2018-08-06 21:17:03 +02:00
PAJLADA_THROW_EXCEPTION(
"Deserialized rapidjson::Value is not a string");
return QString{};
}
try {
2018-08-06 21:17:03 +02:00
return QString::fromUtf8(value.GetString(),
value.GetStringLength());
2018-06-07 17:43:21 +02:00
} catch (const std::exception &) {
2018-06-06 18:57:22 +02:00
// int x = 5;
} catch (...) {
2018-06-06 18:57:22 +02:00
// int y = 5;
}
return QString{};
}
};
} // namespace Settings
} // namespace pajlada