Skip to content

Commit

Permalink
cherry-pick(release-1.41): fix: rewrite json channel converters
Browse files Browse the repository at this point in the history
PR: #2831
  • Loading branch information
mxschmitt committed Jan 18, 2024
1 parent 6c57fcc commit 6b691d3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 63 deletions.
5 changes: 2 additions & 3 deletions src/Playwright/Transport/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSHandle>(this));
options.Converters.Add(new ChannelOwnerToGuidConverter<ElementHandle>(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<WritableStream>(this));
return options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
: JsonConverter<T>
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<T> : JsonConverter<T>
where T : ChannelOwner
{
private readonly Connection _connection;
Expand All @@ -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);
Expand Down
55 changes: 0 additions & 55 deletions src/Playwright/Transport/Converters/ChannelToGuidConverter.cs

This file was deleted.

0 comments on commit 6b691d3

Please sign in to comment.