mirror-chatterino2/src/util/rapidjson-helpers.hpp
pajlada 23cf8cc484
PubSub system (#346)
* Add websocketpp dependency

* Initial pubsub commit

Renamed selection min and max variables to selectionMin and selectionMax
to bypass windows min/max macros being stupid.

TwitchAccount is now initialized with its User ID. It cannot be changed
after it has been initialized.

* Update openssl folder

* Update installation instructions

* Split up websocketpp dependency to its own code only and openssl.pri

* Add missing include to asio steady_timer

* Update dependencies for linux
2018-04-15 15:09:31 +02:00

97 lines
2.3 KiB
C++

#pragma once
#include "util/serialize-custom.hpp"
#include <rapidjson/document.h>
#include <pajlada/settings/serialize.hpp>
#include <cassert>
namespace chatterino {
namespace rj {
void addMember(rapidjson::Value &obj, const char *key, rapidjson::Value &&value,
rapidjson::Document::AllocatorType &a);
void addMember(rapidjson::Value &obj, const char *key, rapidjson::Value &value,
rapidjson::Document::AllocatorType &a);
template <typename Type>
void set(rapidjson::Value &obj, const char *key, const Type &value,
rapidjson::Document::AllocatorType &a)
{
assert(obj.IsObject());
addMember(obj, key, pajlada::Settings::Serialize<Type>::get(value, a), a);
}
template <>
inline void set(rapidjson::Value &obj, const char *key, const rapidjson::Value &value,
rapidjson::Document::AllocatorType &a)
{
assert(obj.IsObject());
addMember(obj, key, const_cast<rapidjson::Value &>(value), a);
}
template <typename Type>
void set(rapidjson::Document &obj, const char *key, const Type &value)
{
assert(obj.IsObject());
auto &a = obj.GetAllocator();
addMember(obj, key, pajlada::Settings::Serialize<Type>::get(value, a), a);
}
template <>
inline void set(rapidjson::Document &obj, const char *key, const rapidjson::Value &value)
{
assert(obj.IsObject());
auto &a = obj.GetAllocator();
addMember(obj, key, const_cast<rapidjson::Value &>(value), a);
}
template <typename Type>
void add(rapidjson::Value &arr, const Type &value, rapidjson::Document::AllocatorType &a)
{
assert(arr.IsArray());
arr.PushBack(pajlada::Settings::Serialize<Type>::get(value, a), a);
}
template <typename Type>
bool getSafe(const rapidjson::Value &obj, const char *key, Type &out)
{
if (!obj.IsObject()) {
return false;
}
if (!obj.HasMember(key)) {
return false;
}
try {
out = pajlada::Settings::Deserialize<Type>::get(obj[key]);
} catch (const std::runtime_error &) {
return false;
}
return true;
}
template <typename Type>
bool getSafe(const rapidjson::Value &value, Type &out)
{
try {
out = pajlada::Settings::Deserialize<Type>::get(value);
} catch (const std::runtime_error &) {
return false;
}
return true;
}
} // namespace rj
} // namespace chatterino