#include #include #include "indexer.h" struct IndexedFile; using Writer = rapidjson::PrettyWriter; using Reader = rapidjson::Document; template void Serialize(Writer& writer, const char* key, Id id) { if (key) writer.Key(key); writer.Uint64(id.id); } template void Serialize(Writer& writer, const char* key, optional> id) { if (id) { Serialize(writer, key, id.value()); } } template void Serialize(Writer& writer, const char* key, const std::vector>& ids) { if (ids.size() == 0) return; if (key) writer.Key(key); writer.StartArray(); for (Id id : ids) Serialize(writer, nullptr, id); writer.EndArray(); } template void Serialize(Writer& writer, const char* key, Ref ref) { if (key) writer.Key(key); std::string s = std::to_string(ref.id.id) + "@" + ref.loc.ToString(); writer.String(s.c_str()); } template void Serialize(Writer& writer, const char* key, const std::vector>& refs) { if (refs.size() == 0) return; if (key) writer.Key(key); writer.StartArray(); for (Ref ref : refs) Serialize(writer, nullptr, ref); writer.EndArray(); } void Serialize(Writer& writer, const char* key, Location location); void Serialize(Writer& writer, const char* key, optional location); void Serialize(Writer& writer, const char* key, const std::vector& locs); void Serialize(Writer& writer, const char* key, const std::string& value); void Serialize(Writer& writer, const char* key, const std::vector& value); void Serialize(Writer& writer, const char* key, uint64_t value); void Serialize(Writer& writer, IndexedFile* file); template void Deserialize(const rapidjson::GenericValue>& document, const char* name, optional>& output) { auto it = document.FindMember(name); if (it != document.MemberEnd()) output = Id(it->value.GetUint64()); } template void Deserialize(const rapidjson::GenericValue>& document, const char* name, std::vector>& output) { auto it = document.FindMember(name); if (it != document.MemberEnd()) { for (auto& array_value : it->value.GetArray()) output.push_back(Id(array_value.GetUint64())); } } template void Deserialize(const rapidjson::GenericValue>& document, const char* name, std::vector>& output) { auto it = document.FindMember(name); if (it != document.MemberEnd()) { for (auto& array_value : it->value.GetArray()) { const char* str_value = array_value.GetString(); uint64_t id = atoi(str_value); const char* loc_string = strchr(str_value, '@') + 1; output.push_back(Ref(Id(id), Location(loc_string))); } } } void Deserialize(const rapidjson::GenericValue>& document, const char* name, std::string& output); void Deserialize(const rapidjson::GenericValue>& document, const char* name, std::vector& output); void Deserialize(const rapidjson::GenericValue>& document, const char* name, optional& output); void Deserialize(const rapidjson::GenericValue>& document, const char* name, std::vector& output); void Deserialize(const Reader& reader, IndexedFile* file); std::string Serialize(IndexedFile* file); IndexedFile Deserialize(std::string path, std::string serialized);