Skip to content

Commit 2d241d0

Browse files
committed
[dotnet] Use source-generated JSON serialization for DevTools types
1 parent 77796f4 commit 2d241d0

File tree

6 files changed

+21
-43
lines changed

6 files changed

+21
-43
lines changed

dotnet/src/webdriver/DevTools/DevToolsSession.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.DevTools.Json;
2021
using OpenQA.Selenium.Internal.Logging;
2122
using System;
2223
using System.Collections.Concurrent;
@@ -283,7 +284,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
283284

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

286-
string contents = JsonSerializer.Serialize(message);
287+
string contents = JsonSerializer.Serialize(message, DevToolsJsonOptions.DevToolsSerializerContext.Default.DevToolsCommandData);
287288
this.pendingCommands.TryAdd(message.CommandId, message);
288289
await this.connection.SendData(contents).ConfigureAwait(false);
289290

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

413-
var versionInfo = JsonSerializer.Deserialize<DevToolsVersionInfo>(rawVersionInfo);
414+
var versionInfo = JsonSerializer.Deserialize<DevToolsVersionInfo>(rawVersionInfo, Json.DevToolsJsonOptions.DevToolsSerializerContext.Default.DevToolsVersionInfo);
414415
this.websocketAddress = versionInfo.WebSocketDebuggerUrl;
415416

416417
if (requestedProtocolVersion == AutoDetectDevToolsProtocolVersion)

dotnet/src/webdriver/DevTools/DevToolsVersionInfo.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ public class DevToolsVersionInfo
6666
/// </summary>
6767
[JsonPropertyName("V8-Version")]
6868
[JsonInclude]
69-
public string V8Version
70-
{
71-
get;
72-
internal set;
73-
}
69+
public string V8Version { get; internal set; }
7470

7571
/// <summary>
7672
/// Gets the URL for the WebSocket connection used for communicating via the DevTools Protocol.

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

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

2020
using System.Text.Json;
21+
using System.Text.Json.Serialization;
2122

2223
#nullable enable
2324

2425
namespace OpenQA.Selenium.DevTools.Json;
2526

26-
internal static class DevToolsJsonOptions
27+
internal static partial class DevToolsJsonOptions
2728
{
2829
public static JsonSerializerOptions Default { get; } = new JsonSerializerOptions()
2930
{
@@ -32,4 +33,9 @@ internal static class DevToolsJsonOptions
3233
new StringConverter(),
3334
}
3435
};
36+
37+
[JsonSerializable(typeof(DevToolsCommandData))]
38+
[JsonSerializable(typeof(DevToolsVersionInfo))]
39+
[JsonSerializable(typeof(DomMutationData))]
40+
internal sealed partial class DevToolsSerializerContext : JsonSerializerContext;
3541
}

dotnet/src/webdriver/DomMutationData.cs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,33 @@ namespace OpenQA.Selenium
2626
/// </summary>
2727
public class DomMutationData
2828
{
29-
private string targetId;
30-
private string attributeName;
31-
private string attributeValue;
32-
private string attributeOriginalValue;
33-
3429
/// <summary>
3530
/// Gets the ID of the element whose value is changing.
3631
/// </summary>
3732
[JsonPropertyName("target")]
3833
[JsonInclude]
39-
public string TargetId
40-
{
41-
get { return this.targetId; }
42-
internal set { this.targetId = value; }
43-
}
34+
public string TargetId { get; internal set; }
4435

4536
/// <summary>
4637
/// Gets the name of the attribute that is changing.
4738
/// </summary>
4839
[JsonPropertyName("name")]
4940
[JsonInclude]
50-
public string AttributeName
51-
{
52-
get { return this.attributeName; }
53-
internal set { this.attributeName = value; }
54-
}
41+
public string AttributeName { get; internal set; }
5542

5643
/// <summary>
5744
/// Gets the value to which the attribute is being changed.
5845
/// </summary>
5946
[JsonPropertyName("value")]
6047
[JsonInclude]
61-
public string AttributeValue
62-
{
63-
get { return this.attributeValue; }
64-
internal set { this.attributeValue = value; }
65-
}
48+
public string AttributeValue { get; internal set; }
6649

6750
/// <summary>
6851
/// Gets the value from which the attribute has been changed.
6952
/// </summary>
7053
[JsonPropertyName("oldValue")]
7154
[JsonInclude]
72-
public string AttributeOriginalValue
73-
{
74-
get { return this.attributeOriginalValue; }
75-
internal set { this.attributeOriginalValue = value; }
76-
}
55+
public string AttributeOriginalValue { get; internal set; }
7756

7857
/// <summary>
7958
/// Stores the element associated with the target ID
@@ -86,7 +65,7 @@ public string AttributeOriginalValue
8665
/// <returns>A string that represents the current object.</returns>
8766
public override string ToString()
8867
{
89-
return string.Format("target: {0}, name: {1}, value: {2}, originalValue: {3}", this.targetId, this.attributeName, this.attributeValue, this.attributeOriginalValue);
68+
return string.Format("target: {0}, name: {1}, value: {2}, originalValue: {3}", this.TargetId, this.AttributeName, this.AttributeValue, this.AttributeOriginalValue);
9069
}
9170
}
9271
}

dotnet/src/webdriver/JavaScriptEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.DevTools;
21+
using OpenQA.Selenium.DevTools.Json;
2122
using OpenQA.Selenium.Internal;
2223
using System;
2324
using System.Collections.Generic;
@@ -398,7 +399,7 @@ private void OnScriptBindingCalled(object sender, BindingCalledEventArgs e)
398399
{
399400
if (e.Name == MonitorBindingName)
400401
{
401-
DomMutationData valueChangeData = JsonSerializer.Deserialize<DomMutationData>(e.Payload);
402+
DomMutationData valueChangeData = JsonSerializer.Deserialize<DomMutationData>(e.Payload, DevToolsJsonOptions.DevToolsSerializerContext.Default.DomMutationData);
402403
var locator = By.CssSelector($"*[data-__webdriver_id='{valueChangeData.TargetId}']");
403404
valueChangeData.Element = driver.FindElements(locator).FirstOrDefault();
404405

dotnet/src/webdriver/Response.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ 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-
4236
/// <summary>
4337
/// Initializes a new instance of the <see cref="Response"/> class
4438
/// </summary>
@@ -79,7 +73,7 @@ public Response(string? sessionId, object? value, WebDriverResult status)
7973
/// <exception cref="JsonException">If <paramref name="value"/> is not a valid JSON object.</exception>
8074
public static Response FromJson(string value)
8175
{
82-
Dictionary<string, object?> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, s_jsonSerializerOptions)
76+
Dictionary<string, object?> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, ResponseJsonSerializerContext.Default.DictionaryStringObject)
8377
?? throw new WebDriverException("JSON success response returned \"null\" value");
8478

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

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

228222
[JsonSerializable(typeof(Dictionary<string, object>))]
223+
[JsonSourceGenerationOptions(Converters = [typeof(ResponseValueJsonConverter)])]
229224
internal sealed partial class ResponseJsonSerializerContext : JsonSerializerContext;
230225
}

0 commit comments

Comments
 (0)