15#include <unordered_set>
17#include <rapidjson/document.h>
18#include <rapidjson/reader.h>
19#include <rapidjson/stringbuffer.h>
20#include <rapidjson/writer.h>
31bool Deserialize(
const rapidjson::Value& value, std::string& result)
33 if (!value.IsString())
36 result = value.GetString();
46 result = value.GetInt();
56 if (
sizeof(
long) ==
sizeof(int64_t))
57 result = value.GetInt64();
59 result = value.GetInt();
64bool Deserialize(
const rapidjson::Value& value,
long long& result)
69 result = value.GetInt64();
79 result = value.GetBool();
86 const rapidjson::Value& value, std::string_view zKey, T& result);
93 const rapidjson::Value& value, std::string_view zKey, std::vector<T>& result)
95 if (!value.IsObject() || !value.HasMember(zKey.data()))
103 if (!value.IsObject())
120 urls = std::move(tempUrls);
127 if (!value.IsObject())
147 urls = std::move(tempVersion);
154 if (!value.IsObject())
165 urls = std::move(tempBlock);
172 if (!value.IsObject())
201 snapshotInfo = std::move(tempSnapshot);
208 if (!value.IsObject())
241 projectInfo = std::move(tempProject);
248 if (!value.IsObject())
262 urls = std::move(tempState);
269 if (!value.IsObject())
283 reponse = std::move(tempResponse);
290 if (!value.IsObject())
307 info = std::move(tempInfo);
315 if (!value.IsObject())
326 response = std::move(tempResponse);
333 const rapidjson::Value& value, std::string_view zKey, T& result)
335 if (!value.IsObject() || !value.HasMember(zKey.data()))
344 if (!value.IsArray())
348 result.reserve(value.Size());
350 for (
const auto& item : value.GetArray())
357 result.push_back(std::move(value));
366 rapidjson::Document document;
367 document.Parse(data.data(), data.size());
369 if (document.HasParseError())
379 using namespace rapidjson;
382 document.SetObject();
384 if (!form.
Name.empty())
386 "name", StringRef(form.
Name.c_str()), document.GetAllocator());
391 document.GetAllocator());
393 Value hashesArray(kArrayType);
395 for (
const auto& hash : form.
Hashes)
396 hashesArray.PushBack(StringRef(hash.c_str()), document.GetAllocator());
398 document.AddMember(
"blocks", hashesArray, document.GetAllocator());
401 document.AddMember(
"force", form.
Force, document.GetAllocator());
404 Writer<StringBuffer> writer(buffer);
405 document.Accept(writer);
407 return buffer.GetString();
410std::optional<ProjectSyncState>
416 return std::move(result);
421std::optional<CreateSnapshotResponse>
427 return std::move(result);
432std::optional<PaginatedProjectsResponse>
438 return std::move(result);
448 return std::move(result);
458 return std::move(result);
467 static const std::unordered_set<wxChar> invalidChars {
468 L
'/', L
'\\', L
':', L
'*', L
'?', L
'"', L
'\'', L
'<', L
'>', L
'|', L
'@',
469 L
'&',
'$', L
'%', L
'^', L
';', L
'~', L
'`', L
'[', L
']', L
'{', L
'}',
472 for (
size_t i = 0; i <
name.length(); ++i)
474 if (invalidChars.find(
name[i]) != invalidChars.end())
485 const auto safeName =
SafeName(projectName);
490 while (path.FileExists())
491 path.SetName(wxString::Format(
"%s_%d", safeName, ++iteration));
493 return path.GetFullPath();
498 using namespace rapidjson;
501 document.SetObject();
503 document.AddMember(
"is_download", stats.
IsDownload, document.GetAllocator());
504 document.AddMember(
"bytes", stats.
Bytes, document.GetAllocator());
505 document.AddMember(
"blocks", stats.
Blocks, document.GetAllocator());
506 document.AddMember(
"mixes", stats.
Mixes, document.GetAllocator());
507 document.AddMember(
"files", stats.
Files, document.GetAllocator());
510 Writer<StringBuffer> writer(buffer);
511 document.Accept(writer);
513 return buffer.GetString();
bool Deserialize(std::string_view data, T &result)
wxString SafeName(wxString name)
bool DeserializeArray(const rapidjson::Value &value, std::string_view zKey, std::vector< T > &result)
std::optional< ProjectSyncState > DeserializeProjectSyncState(const std::string &data)
std::optional< ProjectInfo > DeserializeProjectInfo(const std::string &data)
wxString MakeSafeProjectPath(const wxString &rootDir, const wxString &projectName)
std::string Serialize(const ProjectForm &form)
std::optional< PaginatedProjectsResponse > DeserializePaginatedProjectsResponse(const std::string &data)
std::optional< SnapshotInfo > DeserializeSnapshotInfo(const std::string &data)
std::optional< CreateSnapshotResponse > DeserializeCreateSnapshotResponse(const std::string &data)
ProjectSyncState SyncState
std::vector< ProjectInfo > Items
PaginationInfo Pagination
SnapshotInfo HeadSnapshot
std::string LastSyncedSnapshotId
std::vector< UploadUrls > MissingBlocks
std::vector< SnapshotBlockInfo > Blocks