mirror-chatterino2/src/util/RapidJsonSerializeQString.hpp

50 lines
1.2 KiB
C++
Raw Normal View History

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