diff --git a/dotnet/src/webdriver/BiDi/BiDi.cs b/dotnet/src/webdriver/BiDi/BiDi.cs index 6fe8805abc216..5dfa8534c986a 100644 --- a/dotnet/src/webdriver/BiDi/BiDi.cs +++ b/dotnet/src/webdriver/BiDi/BiDi.cs @@ -31,25 +31,11 @@ public sealed class BiDi : IAsyncDisposable { private readonly ConcurrentDictionary _modules = new(); - private readonly JsonSerializerOptions _jsonOptions; - private BiDi(string url) { var uri = new Uri(url); Broker = new Broker(this, uri); - - _jsonOptions = new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - - Converters = - { - new DateTimeOffsetConverter(), - } - }; } private Broker Broker { get; } @@ -101,6 +87,20 @@ public async ValueTask DisposeAsync() public T AsModule() where T : Module, new() { - return (T)_modules.GetOrAdd(typeof(T), _ => Module.Create(this, Broker, _jsonOptions)); + return (T)_modules.GetOrAdd(typeof(T), _ => Module.Create(this, Broker, CreateDefaultJsonOptions())); + } + + private static JsonSerializerOptions CreateDefaultJsonOptions() + { + return new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + Converters = + { + new DateTimeOffsetConverter(), + } + }; } } diff --git a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs index 8addbc886a9fc..a160d4312a990 100644 --- a/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs +++ b/dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs @@ -81,15 +81,9 @@ public async Task SetDownloadBehaviorDeniedAsync(SetD protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var browserOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new BrowserUserContextConverter(BiDi), - } - }; - - _jsonContext = new BrowserJsonSerializerContext(browserOptions); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + + _jsonContext = new BrowserJsonSerializerContext(jsonSerializerOptions); } } diff --git a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs index 6eb12eb0f2cac..160c306154ae8 100644 --- a/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs +++ b/dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs @@ -255,18 +255,12 @@ public async Task OnUserPromptClosedAsync(Action SetGeolocationPositionErrorOverr protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var emulationOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new BrowsingContextConverter(BiDi), - new BrowserUserContextConverter(BiDi), - } - }; - - _jsonContext = new EmulationJsonSerializerContext(emulationOptions); + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + + _jsonContext = new EmulationJsonSerializerContext(jsonSerializerOptions); } } diff --git a/dotnet/src/webdriver/BiDi/Input/InputModule.cs b/dotnet/src/webdriver/BiDi/Input/InputModule.cs index 4773c75bb47eb..6f6f88ee8c4da 100644 --- a/dotnet/src/webdriver/BiDi/Input/InputModule.cs +++ b/dotnet/src/webdriver/BiDi/Input/InputModule.cs @@ -52,16 +52,10 @@ public async Task SetFilesAsync(BrowsingContext.BrowsingContext protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var inputOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new BrowsingContextConverter(BiDi), - new HandleConverter(BiDi), - } - }; + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new HandleConverter(BiDi)); - _jsonContext = new InputJsonSerializerContext(inputOptions); + _jsonContext = new InputJsonSerializerContext(jsonSerializerOptions); } } diff --git a/dotnet/src/webdriver/BiDi/Log/LogModule.cs b/dotnet/src/webdriver/BiDi/Log/LogModule.cs index 06b918641df5f..34da57d0330ee 100644 --- a/dotnet/src/webdriver/BiDi/Log/LogModule.cs +++ b/dotnet/src/webdriver/BiDi/Log/LogModule.cs @@ -41,18 +41,12 @@ public async Task OnEntryAddedAsync(Action handler, Subs protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var logOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new BrowsingContextConverter(BiDi), - new RealmConverter(BiDi), - new InternalIdConverter(BiDi), - new HandleConverter(BiDi), - } - }; + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new RealmConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new InternalIdConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new HandleConverter(BiDi)); - _jsonContext = new LogJsonSerializerContext(logOptions); + _jsonContext = new LogJsonSerializerContext(jsonSerializerOptions); } } diff --git a/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs b/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs index ed50ef1fd42f4..6110b7432f663 100644 --- a/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs +++ b/dotnet/src/webdriver/BiDi/Network/NetworkModule.cs @@ -178,18 +178,12 @@ public async Task OnAuthRequiredAsync(Action SetPermissionAsync(PermissionDescriptor d protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var permissionsOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new BrowserUserContextConverter(BiDi), - } - }; - - _jsonContext = new PermissionsJsonSerializerContext(permissionsOptions); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); + + _jsonContext = new PermissionsJsonSerializerContext(jsonSerializerOptions); } } diff --git a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs index 92b86750d6ef3..ca935daa2a29c 100644 --- a/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Script/ScriptModule.cs @@ -119,19 +119,13 @@ public async Task OnRealmDestroyedAsync(Action EndAsync(EndOptions? options = null) protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var sessionOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new BrowsingContextConverter(BiDi), - new BrowserUserContextConverter(BiDi), - } - }; + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); - _jsonContext = new SessionJsonSerializerContext(sessionOptions); + _jsonContext = new SessionJsonSerializerContext(jsonSerializerOptions); } } diff --git a/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs b/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs index 21d627a0b087d..ed53de3ef7d51 100644 --- a/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs +++ b/dotnet/src/webdriver/BiDi/Storage/StorageModule.cs @@ -51,16 +51,10 @@ public async Task SetCookieAsync(PartialCookie cookie, SetCooki protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var storageOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new BrowsingContextConverter(BiDi), - new BrowserUserContextConverter(BiDi), - } - }; + jsonSerializerOptions.Converters.Add(new BrowsingContextConverter(BiDi)); + jsonSerializerOptions.Converters.Add(new BrowserUserContextConverter(BiDi)); - _jsonContext = new StorageJsonSerializerContext(storageOptions); + _jsonContext = new StorageJsonSerializerContext(jsonSerializerOptions); } } diff --git a/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs b/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs index a7a31906f4ab7..5555a7db4ea2a 100644 --- a/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs +++ b/dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs @@ -44,15 +44,9 @@ public async Task UninstallAsync(Extension extension, Uninstall protected override void Initialize(JsonSerializerOptions jsonSerializerOptions) { - var webExtensionOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Converters = - { - new WebExtensionConverter(BiDi) - } - }; + jsonSerializerOptions.Converters.Add(new WebExtensionConverter(BiDi)); - _jsonContext = new WebExtensionJsonSerializerContext(webExtensionOptions); + _jsonContext = new WebExtensionJsonSerializerContext(jsonSerializerOptions); } }