using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace Manager.YouTube.Util.Converters; public partial class JsonUrlEscapeConverter : JsonConverter { public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { var url = reader.GetString(); if (string.IsNullOrWhiteSpace(url)) { return url; } return UrlPatternRegex().IsMatch(url) ? Uri.UnescapeDataString(url) : url; } public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) { writer.WriteStringValue(value); } [GeneratedRegex("^(https?|ftp)://", RegexOptions.IgnoreCase | RegexOptions.Compiled, "nl-NL")] private static partial Regex UrlPatternRegex(); }