From de6e4ab28f6fd62c8db19b12257babb740f78769 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Wed, 17 Jan 2024 21:50:36 +0100 Subject: [PATCH] chore: rewrite json channel converters --- src/Playwright/Transport/Connection.cs | 5 +- ...ter.cs => ChannelOwnerConverterFactory.cs} | 26 +++++++-- .../Converters/ChannelToGuidConverter.cs | 55 ------------------- 3 files changed, 23 insertions(+), 63 deletions(-) rename src/Playwright/Transport/Converters/{ChannelOwnerToGuidConverter.cs => ChannelOwnerConverterFactory.cs} (72%) delete mode 100644 src/Playwright/Transport/Converters/ChannelToGuidConverter.cs diff --git a/src/Playwright/Transport/Connection.cs b/src/Playwright/Transport/Connection.cs index d06297e3d1..75448c570d 100644 --- a/src/Playwright/Transport/Connection.cs +++ b/src/Playwright/Transport/Connection.cs @@ -58,10 +58,9 @@ public Connection(LocalUtils localUtils = null) JsonSerializerOptions NewJsonSerializerOptions(bool keepNulls) { var options = JsonExtensions.GetNewDefaultSerializerOptions(keepNulls); - options.Converters.Add(new ChannelToGuidConverter(this)); - options.Converters.Add(new ChannelOwnerToGuidConverter(this)); - options.Converters.Add(new ChannelOwnerToGuidConverter(this)); + // Workaround for https://github.com/dotnet/runtime/issues/46522 + options.Converters.Add(new ChannelOwnerConverterFactory(this)); // Workaround for https://github.com/dotnet/runtime/issues/46522 options.Converters.Add(new ChannelOwnerListToGuidListConverter(this)); return options; diff --git a/src/Playwright/Transport/Converters/ChannelOwnerToGuidConverter.cs b/src/Playwright/Transport/Converters/ChannelOwnerConverterFactory.cs similarity index 72% rename from src/Playwright/Transport/Converters/ChannelOwnerToGuidConverter.cs rename to src/Playwright/Transport/Converters/ChannelOwnerConverterFactory.cs index 858a2f6fac..61747ae8a6 100644 --- a/src/Playwright/Transport/Converters/ChannelOwnerToGuidConverter.cs +++ b/src/Playwright/Transport/Converters/ChannelOwnerConverterFactory.cs @@ -23,13 +23,32 @@ */ using System; +using System.Runtime.InteropServices.ComTypes; using System.Text.Json; using System.Text.Json.Serialization; namespace Microsoft.Playwright.Transport.Converters; -internal class ChannelOwnerToGuidConverter - : JsonConverter +internal class ChannelOwnerConverterFactory : JsonConverterFactory +{ + private readonly Connection _connection; + + public ChannelOwnerConverterFactory(Connection connection) + { + _connection = connection; + } + + public override bool CanConvert(Type typeToConvert) => typeof(ChannelOwner).IsAssignableFrom(typeToConvert); + + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + return (JsonConverter)Activator.CreateInstance( + typeof(ChannelOwnerToGuidConverter<>).MakeGenericType(new Type[] { typeToConvert }), + new object[] { _connection }); + } +} + +internal class ChannelOwnerToGuidConverter : JsonConverter where T : ChannelOwner { private readonly Connection _connection; @@ -39,9 +58,6 @@ public ChannelOwnerToGuidConverter(Connection connection) _connection = connection; } - public override bool CanConvert(Type type) - => (typeof(T) == typeof(ChannelOwner) && typeof(T).IsAssignableFrom(type)) || type == typeof(T); - public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { using JsonDocument document = JsonDocument.ParseValue(ref reader); diff --git a/src/Playwright/Transport/Converters/ChannelToGuidConverter.cs b/src/Playwright/Transport/Converters/ChannelToGuidConverter.cs deleted file mode 100644 index 1990f5ea30..0000000000 --- a/src/Playwright/Transport/Converters/ChannelToGuidConverter.cs +++ /dev/null @@ -1,55 +0,0 @@ -/* - * MIT License - * - * Copyright (c) Microsoft Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using System; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace Microsoft.Playwright.Transport.Converters; - -internal class ChannelToGuidConverter : JsonConverter -{ - private readonly Connection _connection; - - public ChannelToGuidConverter(Connection connection) - { - _connection = connection; - } - - public override bool CanConvert(Type type) => typeof(ChannelOwner).IsAssignableFrom(type); - - public override ChannelOwner Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - using JsonDocument document = JsonDocument.ParseValue(ref reader); - string guid = document.RootElement.GetProperty("guid").ToString(); - return _connection.GetObject(guid); - } - - public override void Write(Utf8JsonWriter writer, ChannelOwner value, JsonSerializerOptions options) - { - writer.WriteStartObject(); - writer.WriteString("guid", value.Guid); - writer.WriteEndObject(); - } -}