using System.Collections.Generic; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; namespace Wabbajack.DTOs.JsonConverters; public class DTOSerializer { public readonly JsonSerializerOptions Options; public DTOSerializer(IEnumerable converters) { Options = new JsonSerializerOptions(); Options.NumberHandling = JsonNumberHandling.AllowReadingFromString; foreach (var c in converters) Options.Converters.Add(c); } public T? Deserialize(string text) { return JsonSerializer.Deserialize(text, Options); } public ValueTask DeserializeAsync(Stream stream, CancellationToken? token = null) { return JsonSerializer.DeserializeAsync(stream, Options, token ?? CancellationToken.None); } public string Serialize(T data, bool writeIndented = false) { var options = Options; if (writeIndented) options = new JsonSerializerOptions(Options) { WriteIndented = true }; return JsonSerializer.Serialize(data, options); } public async Task Serialize(T data, Stream of, bool writeIndented = false) { var options = Options; if (writeIndented) options = new JsonSerializerOptions(Options) { WriteIndented = true }; await JsonSerializer.SerializeAsync(of, data, options); } }