using System; using System.IO; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace PluginManager; public class JsonManager { /// /// Save to JSON file /// /// The class type /// The file path /// The values /// public static async Task SaveToJsonFile(string file, T Data) { var str = new MemoryStream(); await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true } ); await File.WriteAllBytesAsync(file, str.ToArray()); await str.FlushAsync(); str.Close(); } /// /// Convert json text or file to some kind of data /// /// The data type /// The file or json text /// public static async Task ConvertFromJson(string input) { Stream text; if (File.Exists(input)) text = new MemoryStream(await File.ReadAllBytesAsync(input)); else text = new MemoryStream(Encoding.ASCII.GetBytes(input)); text.Position = 0; var obj = await JsonSerializer.DeserializeAsync(text); await text.FlushAsync(); text.Close(); return (obj ?? default)!; } }