Skip to content

Commit cd9209c

Browse files
committed
Move context away from DevToolsJsonOptions
1 parent 1526362 commit cd9209c

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

dotnet/src/webdriver/DevTools/DevToolsSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
284284

285285
LogTrace("Sending {0} {1}: {2}", message.CommandId, message.CommandName, commandParameters.ToString());
286286

287-
string contents = JsonSerializer.Serialize(message, DevToolsJsonOptions.DevToolsSerializerContext.Default.DevToolsCommandData);
287+
string contents = JsonSerializer.Serialize(message, DevToolsSerializerContext.Default.DevToolsCommandData);
288288
this.pendingCommands.TryAdd(message.CommandId, message);
289289
await this.connection.SendData(contents).ConfigureAwait(false);
290290

@@ -411,7 +411,7 @@ private async Task<int> InitializeProtocol(int requestedProtocolVersion)
411411
rawVersionInfo = await client.GetStringAsync("/json/version").ConfigureAwait(false);
412412
}
413413

414-
var versionInfo = JsonSerializer.Deserialize(rawVersionInfo, DevToolsJsonOptions.DevToolsSerializerContext.Default.DevToolsVersionInfo);
414+
var versionInfo = JsonSerializer.Deserialize(rawVersionInfo, DevToolsSerializerContext.Default.DevToolsVersionInfo);
415415
this.websocketAddress = versionInfo.WebSocketDebuggerUrl;
416416

417417
if (requestedProtocolVersion == AutoDetectDevToolsProtocolVersion)

dotnet/src/webdriver/DevTools/Json/DevToolsJsonOptions.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
// </copyright>
1919

2020
using System.Text.Json;
21-
using System.Text.Json.Serialization;
2221

2322
#nullable enable
2423

2524
namespace OpenQA.Selenium.DevTools.Json;
2625

27-
internal static partial class DevToolsJsonOptions
26+
internal static class DevToolsJsonOptions
2827
{
2928
public static JsonSerializerOptions Default { get; } = new JsonSerializerOptions()
3029
{
@@ -33,9 +32,4 @@ internal static partial class DevToolsJsonOptions
3332
new StringConverter(),
3433
}
3534
};
36-
37-
[JsonSerializable(typeof(DevToolsCommandData))]
38-
[JsonSerializable(typeof(DevToolsVersionInfo))]
39-
[JsonSerializable(typeof(DomMutationData))]
40-
internal sealed partial class DevToolsSerializerContext : JsonSerializerContext;
4135
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// <copyright file="DevToolsSerializerContext.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System.Text.Json.Serialization;
21+
22+
#nullable enable
23+
24+
namespace OpenQA.Selenium.DevTools.Json;
25+
26+
[JsonSerializable(typeof(DevToolsCommandData))]
27+
[JsonSerializable(typeof(DevToolsVersionInfo))]
28+
[JsonSerializable(typeof(DomMutationData))]
29+
internal sealed partial class DevToolsSerializerContext : JsonSerializerContext;

dotnet/src/webdriver/JavaScriptEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ private void OnScriptBindingCalled(object sender, BindingCalledEventArgs e)
399399
{
400400
if (e.Name == MonitorBindingName)
401401
{
402-
DomMutationData valueChangeData = JsonSerializer.Deserialize(e.Payload, DevToolsJsonOptions.DevToolsSerializerContext.Default.DomMutationData);
402+
DomMutationData valueChangeData = JsonSerializer.Deserialize(e.Payload, DevToolsSerializerContext.Default.DomMutationData);
403403
var locator = By.CssSelector($"*[data-__webdriver_id='{valueChangeData.TargetId}']");
404404
valueChangeData.Element = driver.FindElements(locator).FirstOrDefault();
405405

dotnet/src/webdriver/Response.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ namespace OpenQA.Selenium
3333
/// </summary>
3434
public class Response
3535
{
36+
private static readonly JsonSerializerOptions s_jsonSerializerOptions = new()
37+
{
38+
TypeInfoResolver = ResponseJsonSerializerContext.Default,
39+
Converters = { new ResponseValueJsonConverter() } // we still need it to make `Object` as `Dictionary`
40+
};
41+
3642
/// <summary>
3743
/// Initializes a new instance of the <see cref="Response"/> class
3844
/// </summary>
@@ -73,7 +79,7 @@ public Response(string? sessionId, object? value, WebDriverResult status)
7379
/// <exception cref="JsonException">If <paramref name="value"/> is not a valid JSON object.</exception>
7480
public static Response FromJson(string value)
7581
{
76-
Dictionary<string, object?> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, ResponseJsonSerializerContext.Default.DictionaryStringObject)
82+
Dictionary<string, object?> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, s_jsonSerializerOptions)
7783
?? throw new WebDriverException("JSON success response returned \"null\" value");
7884

7985
object? contents;
@@ -172,7 +178,7 @@ public WebDriverResult Status
172178
/// <exception cref="WebDriverException">If the JSON dictionary is not in the expected state, per spec.</exception>
173179
public static Response FromErrorJson(string value)
174180
{
175-
Dictionary<string, object?> deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, ResponseJsonSerializerContext.Default.DictionaryStringObject)
181+
Dictionary<string, object?> deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, s_jsonSerializerOptions)
176182
?? throw new WebDriverException("JSON error response returned \"null\" value");
177183

178184
if (!deserializedResponse.TryGetValue("value", out object? valueObject))
@@ -220,6 +226,5 @@ public override string ToString()
220226
}
221227

222228
[JsonSerializable(typeof(Dictionary<string, object>))]
223-
[JsonSourceGenerationOptions(Converters = [typeof(ResponseValueJsonConverter)])] // we still need it to make `Object` as `Dictionary`
224229
internal sealed partial class ResponseJsonSerializerContext : JsonSerializerContext;
225230
}

0 commit comments

Comments
 (0)