1 #pragma once 2 3 #include "util/RapidJsonSerializeQString.hpp" 4 5 #include <rapidjson/document.h> 6 #include <pajlada/serialize.hpp> 7 8 #include <cassert> 9 #include <string> 10 11 namespace chatterino { 12 namespace rj { 13 14 void addMember(rapidjson::Value &obj, const char *key, 15 rapidjson::Value &&value, 16 rapidjson::Document::AllocatorType &a); 17 void addMember(rapidjson::Value &obj, const char *key, 18 rapidjson::Value &value, 19 rapidjson::Document::AllocatorType &a); 20 21 template <typename Type> set(rapidjson::Value & obj,const char * key,const Type & value,rapidjson::Document::AllocatorType & a)22 void set(rapidjson::Value &obj, const char *key, const Type &value, 23 rapidjson::Document::AllocatorType &a) 24 { 25 assert(obj.IsObject()); 26 27 addMember(obj, key, pajlada::Serialize<Type>::get(value, a), a); 28 } 29 30 template <> set(rapidjson::Value & obj,const char * key,const rapidjson::Value & value,rapidjson::Document::AllocatorType & a)31 inline void set(rapidjson::Value &obj, const char *key, 32 const rapidjson::Value &value, 33 rapidjson::Document::AllocatorType &a) 34 { 35 assert(obj.IsObject()); 36 37 addMember(obj, key, const_cast<rapidjson::Value &>(value), a); 38 } 39 40 template <typename Type> set(rapidjson::Document & obj,const char * key,const Type & value)41 void set(rapidjson::Document &obj, const char *key, const Type &value) 42 { 43 assert(obj.IsObject()); 44 45 auto &a = obj.GetAllocator(); 46 47 addMember(obj, key, pajlada::Serialize<Type>::get(value, a), a); 48 } 49 50 template <> set(rapidjson::Document & obj,const char * key,const rapidjson::Value & value)51 inline void set(rapidjson::Document &obj, const char *key, 52 const rapidjson::Value &value) 53 { 54 assert(obj.IsObject()); 55 56 auto &a = obj.GetAllocator(); 57 58 addMember(obj, key, const_cast<rapidjson::Value &>(value), a); 59 } 60 61 template <typename Type> add(rapidjson::Value & arr,const Type & value,rapidjson::Document::AllocatorType & a)62 void add(rapidjson::Value &arr, const Type &value, 63 rapidjson::Document::AllocatorType &a) 64 { 65 assert(arr.IsArray()); 66 67 arr.PushBack(pajlada::Serialize<Type>::get(value, a), a); 68 } 69 70 bool checkJsonValue(const rapidjson::Value &obj, const char *key); 71 72 template <typename Type> getSafe(const rapidjson::Value & obj,const char * key,Type & out)73 bool getSafe(const rapidjson::Value &obj, const char *key, Type &out) 74 { 75 if (!checkJsonValue(obj, key)) 76 { 77 return false; 78 } 79 80 bool error = false; 81 out = pajlada::Deserialize<Type>::get(obj[key], &error); 82 83 return !error; 84 } 85 86 template <typename Type> getSafe(const rapidjson::Value & value,Type & out)87 bool getSafe(const rapidjson::Value &value, Type &out) 88 { 89 bool error = false; 90 out = pajlada::Deserialize<Type>::get(value, &error); 91 92 return !error; 93 } 94 95 bool getSafeObject(rapidjson::Value &obj, const char *key, 96 rapidjson::Value &out); 97 98 QString stringify(const rapidjson::Value &value); 99 100 } // namespace rj 101 } // namespace chatterino 102