69 CP_SDK_NO_DEF_CTORS(
Json);
72 using UTF8 = rapidjson::UTF8<char>;
73 using UTF16 = rapidjson::UTF16<char16_t>;
74 using U16Value = rapidjson::GenericValue<UTF16>;
75 using U16Document = rapidjson::GenericDocument<UTF16>;
76 using U8GenericStringBuffer = rapidjson::GenericStringBuffer<UTF8>;
77 using U16GenericStringBuffer = rapidjson::GenericStringBuffer<UTF16>;
79 template <
typename t_Writter>
using U16Writter = rapidjson::Writer<t_Writter, UTF16, UTF16>;
80 template <
typename t_Writter>
using U16ToU8Writter = rapidjson::Writer<t_Writter, UTF16, UTF8>;
81 template <
typename t_Writter>
using U16PrettyWritter = rapidjson::PrettyWriter<t_Writter, UTF16, UTF16>;
82 template <
typename t_Writter>
using U16ToU8PrettyWritter = rapidjson::PrettyWriter<t_Writter, UTF16, UTF8>;
84 using TAllocator = U16Document::AllocatorType;
92 static bool TryFromU8String(std::string_view p_Str, U16Document& p_Document, std::u16string* p_Error =
nullptr);
98 static bool TryFromU8Stream(std::ifstream& p_Stream, U16Document& p_Document, std::u16string* p_Error =
nullptr);
104 static bool TryFromU16String(std::u16string_view p_Str, U16Document& p_Document, std::u16string* p_Error =
nullptr);
111 static std::string
ToU8String(U16Value& p_Value,
bool p_Pretty);
117 static bool TryToU8Stream(U16Value& p_Value,
bool p_Pretty, std::ofstream& p_Stream);
122 static std::u16string
ToU16String(U16Value& p_Value,
bool p_Pretty);
125 static void SerializeBool (U16Value& p_Document, TAllocator& p_Allocator,
bool p_Value,
const char16_t* p_Name);
126 static void SerializeColor32 (U16Value& p_Document, TAllocator& p_Allocator, UnityEngine::Color32 p_Value,
const char16_t* p_Name);
127 static void SerializeColor (U16Value& p_Document, TAllocator& p_Allocator, UnityEngine::Color p_Value,
const char16_t* p_Name);
128 static void SerializeFloat (U16Value& p_Document, TAllocator& p_Allocator,
float p_Value,
const char16_t* p_Name);
129 static void SerializeInt32 (U16Value& p_Document, TAllocator& p_Allocator, int32_t p_Value,
const char16_t* p_Name);
130 static void SerializeInt64 (U16Value& p_Document, TAllocator& p_Allocator, int64_t p_Value,
const char16_t* p_Name);
131 static void SerializeQuaternion(U16Value& p_Document, TAllocator& p_Allocator, UnityEngine::Quaternion p_Value,
const char16_t* p_Name);
132 static void SerializeString (U16Value& p_Document, TAllocator& p_Allocator,
const std::u16string& p_Value,
const char16_t* p_Name);
133 static void SerializeUInt32 (U16Value& p_Document, TAllocator& p_Allocator, uint32_t p_Value,
const char16_t* p_Name);
134 static void SerializeUInt64 (U16Value& p_Document, TAllocator& p_Allocator, uint64_t p_Value,
const char16_t* p_Name);
135 static void SerializeVector2 (U16Value& p_Document, TAllocator& p_Allocator, UnityEngine::Vector2 p_Value,
const char16_t* p_Name);
136 static void SerializeVector3 (U16Value& p_Document, TAllocator& p_Allocator, UnityEngine::Vector3 p_Value,
const char16_t* p_Name);
138 template<
typename t_Enum>
139 static void SerializeEnum(U16Value& p_Document, TAllocator& p_Allocator,
typename t_Enum::E p_Value,
const char16_t* p_Name)
141 if (p_Document.HasMember(p_Name))
142 throw std::runtime_error(
"_Serialize<T>: Field " + U16StrToStr(p_Name) +
" is already serialized");
144 auto l_Str = t_Enum::ToStr(p_Value);
145 p_Document.AddMember(rapidjson::StringRef(p_Name), rapidjson::StringRef(l_Str.data(), l_Str.size()), p_Allocator);
147 template<
typename t_Object>
148 static void SerializeObject(U16Value& p_Document, TAllocator& p_Allocator, std::shared_ptr<t_Object>& p_Object,
const char16_t* p_Name)
150 if (p_Document.HasMember(p_Name))
151 throw std::runtime_error(
"_Serialize<T>: Field " + U16StrToStr(p_Name) +
" is already serialized");
154 l_Object.SetObject();
156 p_Object->Serialize(l_Object, p_Allocator);
158 p_Document.AddMember(rapidjson::StringRef(p_Name), l_Object, p_Allocator);
160 template<
typename t_Object>
requires(!std::is_pointer_v<t_Object>)
161 static void SerializeObject(U16Value& p_Document, TAllocator& p_Allocator, std::optional<t_Object>& p_Object,
const char16_t* p_Name)
163 if (!p_Object.has_value())
165 if (p_Document.HasMember(p_Name))
166 throw std::runtime_error(
"_Serialize<T>: Field " + U16StrToStr(p_Name) +
" is already serialized");
169 l_Object.SetObject();
171 p_Object.value().Serialize(l_Object, p_Allocator);
173 p_Document.AddMember(rapidjson::StringRef(p_Name), l_Object, p_Allocator);
175 template<
typename t_Object>
176 static void SerializeObjectArray(U16Value& p_Document, TAllocator& p_Allocator, std::vector<std::shared_ptr<t_Object>>& p_Array,
const char16_t* p_Name)
178 if (p_Document.HasMember(p_Name))
179 throw std::runtime_error(
"_Serialize<T>: Field " + U16StrToStr(p_Name) +
" is already serialized");
184 for (
auto& l_Current : p_Array)
187 l_Object.SetObject();
188 l_Current->Serialize(l_Object, p_Allocator);
189 l_Array.PushBack(l_Object, p_Allocator);
192 p_Document.AddMember(rapidjson::StringRef(p_Name), l_Array, p_Allocator);
196 static void UnserializeBool (U16Value& p_Document,
bool& p_Value,
const char16_t* p_Name);
197 static void UnserializeColor32 (U16Value& p_Document, UnityEngine::Color32& p_Value,
const char16_t* p_Name);
198 static void UnserializeColor (U16Value& p_Document, UnityEngine::Color& p_Value,
const char16_t* p_Name);
199 static void UnserializeFloat (U16Value& p_Document,
float& p_Value,
const char16_t* p_Name);
200 static void UnserializeInt32 (U16Value& p_Document, int32_t& p_Value,
const char16_t* p_Name);
201 static void UnserializeInt64 (U16Value& p_Document, int64_t& p_Value,
const char16_t* p_Name);
202 static void UnserializeQuaternion(U16Value& p_Document, UnityEngine::Quaternion& p_Value,
const char16_t* p_Name);
203 static void UnserializeString (U16Value& p_Document, std::u16string& p_Value,
const char16_t* p_Name);
204 static void UnserializeUInt32 (U16Value& p_Document, uint32_t& p_Value,
const char16_t* p_Name);
205 static void UnserializeUInt64 (U16Value& p_Document, uint64_t& p_Value,
const char16_t* p_Name);
206 static void UnserializeVector2 (U16Value& p_Document, UnityEngine::Vector2& p_Value,
const char16_t* p_Name);
207 static void UnserializeVector3 (U16Value& p_Document, UnityEngine::Vector3& p_Value,
const char16_t* p_Name);
209 template<
typename t_Enum>
210 static void UnserializeEnum(U16Value& p_Document,
typename t_Enum::E& p_Value,
const char16_t* p_Name,
typename t_Enum::E p_Default)
212 if (!p_Document.HasMember(p_Name))
217 CP_SDK_JSON_ENSURE_TYPE(IsString);
219 auto l_U16String = p_Document[p_Name].GetString();
220 p_Value = t_Enum::ToEnum(l_U16String);
222 if (t_Enum::ToStr(p_Value) != l_U16String)
225 template<
typename t_Object>
226 static void UnserializeObject(U16Value& p_Document, std::shared_ptr<t_Object>& p_Object,
const char16_t* p_Name)
228 if (!p_Document.HasMember(p_Name))
return;
229 if (p_Document[p_Name].IsNull())
234 CP_SDK_JSON_ENSURE_TYPE(IsObject);
236 auto l_Object = p_Document[p_Name].GetObject();
238 p_Object->Unserialize(l_Object);
240 template<
typename t_Object>
requires(!std::is_pointer_v<t_Object>)
241 static void UnserializeObject(U16Value& p_Document, std::optional<t_Object>& p_Object,
const char16_t* p_Name)
243 if (!p_Document.HasMember(p_Name))
return;
244 if (p_Document[p_Name].IsNull())
246 p_Object = std::nullopt;
249 CP_SDK_JSON_ENSURE_TYPE(IsObject);
251 auto l_Object = p_Document[p_Name].GetObject();
253 if (!p_Object.has_value())
254 p_Object = t_Object {};
256 p_Object.value().Unserialize(l_Object);
258 template<
typename t_Object>
259 static void UnserializeObjectArray(U16Value& p_Document, std::vector<std::shared_ptr<t_Object>>& p_Array,
const char16_t* p_Name)
261 if (!p_Document.HasMember(p_Name))
return;
262 CP_SDK_JSON_ENSURE_TYPE(IsArray);
264 auto l_Array = p_Document[p_Name].GetArray();
266 p_Array.reserve(l_Array.Size());
268 for (
auto l_I = 0; l_I < l_Array.Size(); ++l_I)
270 auto l_Object = l_Array[l_I].IsNull() ? nullptr : std::make_shared<t_Object>();
271 if (l_Object) l_Object->Unserialize(l_Array[l_I]);
273 p_Array.push_back(l_Object);