using File_Watcher.Models; using System.Text.Json; using System.Text.Json.Serialization; namespace File_Watcher.Helpers; internal static partial class SyncHelper { private record Record(string RelativePath, long Size, long Ticks); private record RelativePath(string Path, Record[] Records); [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(RelativePath))] private partial class RelativePathSourceGenerationContext : JsonSerializerContext { } [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(Dictionary))] private partial class DictionaryStringObjectBSourceGenerationContext : JsonSerializerContext { } internal static bool Check(AppSettings _, ILogger __) => true; internal static string GetReply(string body, Dictionary keyValuePairs) { string? json; RelativePath? relativePath; if (!string.IsNullOrEmpty(body) && body[0] == '{') { File.WriteAllText(".json", body); relativePath = JsonSerializer.Deserialize(body, RelativePathSourceGenerationContext.Default.RelativePath) ?? throw new NullReferenceException(); } else { json = JsonSerializer.Serialize(keyValuePairs, DictionaryStringObjectBSourceGenerationContext.Default.DictionaryStringObject); File.WriteAllText(".json", json); relativePath = JsonSerializer.Deserialize(json, RelativePathSourceGenerationContext.Default.RelativePath) ?? throw new NullReferenceException(); } if (relativePath is null) { } return $"wait-{relativePath?.Records.Length}"; } }