Skip to content

Commit 77010cd

Browse files
authored
[dotnet] Migration from Newtonsoft.Json to System.Text.Json package (#14292)
1 parent 5dd385c commit 77010cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+434
-375
lines changed

MODULE.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use_repo(esbuild, "esbuild_toolchains")
7878
register_toolchains("@esbuild_toolchains//:all")
7979

8080
dotnet = use_extension("@rules_dotnet//dotnet:extensions.bzl", "dotnet")
81-
dotnet.toolchain(dotnet_version = "7.0.400")
81+
dotnet.toolchain(dotnet_version = "8.0.203")
8282
use_repo(dotnet, "dotnet_toolchains")
8383

8484
selenium_paket = use_extension("//dotnet:paket.nuget_extension.bzl", "nuget_extension")

dotnet/paket.dependencies

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ nuget System.Diagnostics.Tools 4.3.0
2424
nuget System.Drawing.Common 7.0.0
2525
nuget System.Runtime 4.3.1
2626
nuget System.Runtime.InteropServices 4.3.0
27-
nuget System.Text.Json 6.0.9
27+
nuget System.Text.Json 8.0.4
2828
nuget Runfiles 0.12.0

dotnet/paket.lock

+11-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ NUGET
1515
System.Reflection.TypeExtensions (>= 4.4)
1616
Humanizer.Core (2.8.26)
1717
Microsoft.AspNetCore.App.Ref (6.0.9)
18-
Microsoft.Bcl.AsyncInterfaces (7.0) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
18+
Microsoft.Bcl.AsyncInterfaces (8.0) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
1919
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< netstandard2.1)) (== netstandard2.0)
2020
Microsoft.CSharp (4.5)
2121
Microsoft.Extensions.DependencyInjection (3.1.9)
@@ -42,7 +42,7 @@ NUGET
4242
NETStandard.Library (>= 2.0)
4343
NUnit (3.13.2)
4444
Runfiles (0.12)
45-
System.Buffers (4.5.1) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
45+
System.Buffers (4.5.1) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
4646
System.Diagnostics.EventLog (7.0)
4747
System.Security.Principal.Windows (>= 5.0) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
4848
System.Diagnostics.Tools (4.3)
@@ -61,7 +61,7 @@ NUGET
6161
System.Buffers (>= 4.5.1) - restriction: || (&& (== net7.0) (>= monotouch)) (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp2.0)) (&& (== net7.0) (< netstandard1.1)) (&& (== net7.0) (< netstandard2.0)) (&& (== net7.0) (>= xamarinios)) (&& (== net7.0) (>= xamarinmac)) (&& (== net7.0) (>= xamarintvos)) (&& (== net7.0) (>= xamarinwatchos)) (== netstandard2.0)
6262
System.Numerics.Vectors (>= 4.4) - restriction: || (&& (== net7.0) (< netcoreapp2.0)) (== netstandard2.0)
6363
System.Runtime.CompilerServices.Unsafe (>= 4.5.3) - restriction: || (&& (== net7.0) (>= monotouch)) (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp2.0)) (&& (== net7.0) (< netcoreapp2.1)) (&& (== net7.0) (< netstandard1.1)) (&& (== net7.0) (< netstandard2.0)) (&& (== net7.0) (>= uap10.1)) (&& (== net7.0) (>= xamarinios)) (&& (== net7.0) (>= xamarinmac)) (&& (== net7.0) (>= xamarintvos)) (&& (== net7.0) (>= xamarinwatchos)) (== netstandard2.0)
64-
System.Numerics.Vectors (4.5) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
64+
System.Numerics.Vectors (4.5) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< netcoreapp2.0)) (== netstandard2.0)
6565
System.Reflection (4.3)
6666
Microsoft.NETCore.Platforms (>= 1.1)
6767
Microsoft.NETCore.Targets (>= 1.1)
@@ -79,7 +79,7 @@ NUGET
7979
System.Runtime (4.3.1)
8080
Microsoft.NETCore.Platforms (>= 1.1.1)
8181
Microsoft.NETCore.Targets (>= 1.1.3)
82-
System.Runtime.CompilerServices.Unsafe (6.0)
82+
System.Runtime.CompilerServices.Unsafe (6.0) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
8383
System.Runtime.Handles (4.3)
8484
Microsoft.NETCore.Platforms (>= 1.1)
8585
Microsoft.NETCore.Targets (>= 1.1)
@@ -100,14 +100,13 @@ NUGET
100100
System.Buffers (>= 4.5.1) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
101101
System.Memory (>= 4.5.5) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
102102
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
103-
System.Text.Json (6.0.9)
104-
Microsoft.Bcl.AsyncInterfaces (>= 6.0) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
105-
System.Buffers (>= 4.5.1) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
106-
System.Memory (>= 4.5.4) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
107-
System.Numerics.Vectors (>= 4.5) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
108-
System.Runtime.CompilerServices.Unsafe (>= 6.0)
109-
System.Text.Encodings.Web (>= 6.0)
110-
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net7.0) (>= net461)) (&& (== net7.0) (< netcoreapp3.1)) (== netstandard2.0)
103+
System.Text.Json (8.0.4)
104+
Microsoft.Bcl.AsyncInterfaces (>= 8.0) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
105+
System.Buffers (>= 4.5.1) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
106+
System.Memory (>= 4.5.5) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
107+
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
108+
System.Text.Encodings.Web (>= 8.0)
109+
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net7.0) (>= net462)) (&& (== net7.0) (< net6.0)) (== netstandard2.0)
111110
System.Threading.Tasks (4.3)
112111
Microsoft.NETCore.Platforms (>= 1.1)
113112
Microsoft.NETCore.Targets (>= 1.1)

dotnet/paket.nuget.bzl

+2-2
Large diffs are not rendered by default.

dotnet/src/support/BUILD.bazel

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
load(
22
"//dotnet:defs.bzl",
33
"csharp_library",
4-
"framework",
54
"generated_assembly_info",
65
"nuget_pack",
76
)
@@ -43,8 +42,7 @@ csharp_library(
4342
"//dotnet:__subpackages__",
4443
],
4544
deps = [
46-
"//dotnet/src/webdriver",
47-
framework("nuget", "NETStandard.Library"),
45+
"//dotnet/src/webdriver:webdriver-netstandard2.0",
4846
],
4947
)
5048

@@ -83,8 +81,7 @@ csharp_library(
8381
"//dotnet:__subpackages__",
8482
],
8583
deps = [
86-
"//dotnet/src/webdriver:webdriver-strongnamed",
87-
framework("nuget", "NETStandard.Library"),
84+
"//dotnet/src/webdriver:webdriver-netstandard2.0-strongnamed",
8885
],
8986
)
9087

dotnet/src/webdriver/BUILD.bazel

+68-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ generated_assembly_info(
2626
)
2727

2828
csharp_library(
29-
name = "webdriver",
29+
name = "webdriver-netstandard2.0",
3030
srcs = [
3131
":assembly-info",
3232
] + glob([
@@ -36,7 +36,7 @@ csharp_library(
3636
internals_visible_to = [
3737
"WebDriver.Common.Tests",
3838
],
39-
langversion = "10.0",
39+
langversion = "12.0",
4040
resources = [
4141
"//javascript/atoms/fragments:find-elements.js",
4242
"//javascript/atoms/fragments:is-displayed.js",
@@ -52,20 +52,50 @@ csharp_library(
5252
],
5353
deps = [
5454
framework("nuget", "NETStandard.Library"),
55-
framework("nuget", "Newtonsoft.Json"),
55+
framework("nuget", "System.Memory"),
56+
framework("nuget", "System.Text.Json"),
5657
],
5758
)
5859

5960
csharp_library(
60-
name = "webdriver-strongnamed",
61+
name = "webdriver-net8.0",
62+
srcs = [
63+
":assembly-info",
64+
] + glob([
65+
"**/*.cs",
66+
]) + devtools_version_targets(),
67+
out = "WebDriver",
68+
internals_visible_to = [
69+
"WebDriver.Common.Tests",
70+
],
71+
langversion = "12.0",
72+
resources = [
73+
"//javascript/atoms/fragments:find-elements.js",
74+
"//javascript/atoms/fragments:is-displayed.js",
75+
"//javascript/cdp-support:mutation-listener.js",
76+
"//javascript/webdriver/atoms:get-attribute.js",
77+
"//third_party/js/selenium:webdriver_json",
78+
],
79+
target_frameworks = [
80+
"net8.0",
81+
],
82+
visibility = [
83+
"//dotnet:__subpackages__",
84+
],
85+
deps = [
86+
],
87+
)
88+
89+
csharp_library(
90+
name = "webdriver-netstandard2.0-strongnamed",
6191
srcs = [
6292
":assembly-info",
6393
] + glob([
6494
"**/*.cs",
6595
]) + devtools_version_targets(),
6696
out = "WebDriver.StrongNamed",
6797
keyfile = "//dotnet:WebDriver.snk",
68-
langversion = "10.0",
98+
langversion = "12.0",
6999
resources = [
70100
"//javascript/atoms/fragments:find-elements.js",
71101
"//javascript/atoms/fragments:is-displayed.js",
@@ -81,7 +111,35 @@ csharp_library(
81111
],
82112
deps = [
83113
framework("nuget", "NETStandard.Library"),
84-
framework("nuget", "Newtonsoft.Json"),
114+
framework("nuget", "System.Memory"),
115+
framework("nuget", "System.Text.Json"),
116+
],
117+
)
118+
119+
csharp_library(
120+
name = "webdriver-net8.0-strongnamed",
121+
srcs = [
122+
":assembly-info",
123+
] + glob([
124+
"**/*.cs",
125+
]) + devtools_version_targets(),
126+
out = "WebDriver.StrongNamed",
127+
keyfile = "//dotnet:WebDriver.snk",
128+
langversion = "12.0",
129+
resources = [
130+
"//javascript/atoms/fragments:find-elements.js",
131+
"//javascript/atoms/fragments:is-displayed.js",
132+
"//javascript/cdp-support:mutation-listener.js",
133+
"//javascript/webdriver/atoms:get-attribute.js",
134+
"//third_party/js/selenium:webdriver_json",
135+
],
136+
target_frameworks = [
137+
"net8.0",
138+
],
139+
visibility = [
140+
"//dotnet:__subpackages__",
141+
],
142+
deps = [
85143
],
86144
)
87145

@@ -137,7 +195,8 @@ nuget_pack(
137195
},
138196
id = "Selenium.WebDriver",
139197
libs = {
140-
":webdriver": "WebDriver",
198+
":webdriver-net8.0": "WebDriver",
199+
":webdriver-netstandard2.0": "WebDriver",
141200
},
142201
nuspec_template = "WebDriver.nuspec",
143202
tags = [
@@ -162,7 +221,8 @@ nuget_pack(
162221
},
163222
id = "Selenium.WebDriver.StrongNamed",
164223
libs = {
165-
":webdriver-strongnamed": "WebDriver.StrongNamed",
224+
":webdriver-net8.0-strongnamed": "WebDriver.StrongNamed",
225+
":webdriver-netstandard2.0-strongnamed": "WebDriver.StrongNamed",
166226
},
167227
nuspec_template = "WebDriver.StrongNamed.nuspec",
168228
property_group_vars = {

dotnet/src/webdriver/Chromium/ChromiumNetworkConditions.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
// limitations under the License.
1717
// </copyright>
1818

19-
using Newtonsoft.Json;
2019
using System;
2120
using System.Collections.Generic;
21+
using System.Text.Json.Serialization;
2222

2323
namespace OpenQA.Selenium.Chromium
2424
{
2525
/// <summary>
2626
/// Provides manipulation of getting and setting network conditions from Chromium.
2727
/// </summary>
28-
[JsonObject(MemberSerialization.OptIn)]
2928
public class ChromiumNetworkConditions
3029
{
3130
private bool offline;
@@ -36,7 +35,7 @@ public class ChromiumNetworkConditions
3635
/// <summary>
3736
/// Gets or sets a value indicating whether the network is offline. Defaults to <see langword="false"/>.
3837
/// </summary>
39-
[JsonProperty("offline")]
38+
[JsonPropertyName("offline")]
4039
public bool IsOffline
4140
{
4241
get { return this.offline; }
@@ -46,6 +45,7 @@ public bool IsOffline
4645
/// <summary>
4746
/// Gets or sets the simulated latency of the connection. Typically given in milliseconds.
4847
/// </summary>
48+
[JsonIgnore]
4949
public TimeSpan Latency
5050
{
5151
get { return this.latency; }
@@ -55,7 +55,7 @@ public TimeSpan Latency
5555
/// <summary>
5656
/// Gets or sets the throughput of the network connection in bytes/second for downloading.
5757
/// </summary>
58-
[JsonProperty("download_throughput")]
58+
[JsonPropertyName("download_throughput")]
5959
public long DownloadThroughput
6060
{
6161
get { return this.downloadThroughput; }
@@ -73,7 +73,7 @@ public long DownloadThroughput
7373
/// <summary>
7474
/// Gets or sets the throughput of the network connection in bytes/second for uploading.
7575
/// </summary>
76-
[JsonProperty("upload_throughput")]
76+
[JsonPropertyName("upload_throughput")]
7777
public long UploadThroughput
7878
{
7979
get { return this.uploadThroughput; }
@@ -88,7 +88,9 @@ public long UploadThroughput
8888
}
8989
}
9090

91-
[JsonProperty("latency", NullValueHandling = NullValueHandling.Ignore)]
91+
[JsonPropertyName("latency")]
92+
[JsonInclude]
93+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
9294
internal long? SerializableLatency
9395
{
9496
get

dotnet/src/webdriver/Command.cs

+12-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
// limitations under the License.
1717
// </copyright>
1818

19-
using Newtonsoft.Json;
2019
using OpenQA.Selenium.Internal;
2120
using System.Collections.Generic;
21+
using System.Text.Json;
22+
using System.Text.Json.Serialization;
2223

2324
namespace OpenQA.Selenium
2425
{
@@ -27,6 +28,11 @@ namespace OpenQA.Selenium
2728
/// </summary>
2829
public class Command
2930
{
31+
private readonly static JsonSerializerOptions s_jsonSerializerOptions = new()
32+
{
33+
Converters = { new ResponseValueJsonConverter() }
34+
};
35+
3036
private SessionId commandSessionId;
3137
private string commandName;
3238
private Dictionary<string, object> commandParameters = new Dictionary<string, object>();
@@ -61,7 +67,7 @@ public Command(SessionId sessionId, string name, Dictionary<string, object> para
6167
/// <summary>
6268
/// Gets the SessionID of the command
6369
/// </summary>
64-
[JsonProperty("sessionId")]
70+
[JsonPropertyName("sessionId")]
6571
public SessionId SessionId
6672
{
6773
get { return this.commandSessionId; }
@@ -70,7 +76,7 @@ public SessionId SessionId
7076
/// <summary>
7177
/// Gets the command name
7278
/// </summary>
73-
[JsonProperty("name")]
79+
[JsonPropertyName("name")]
7480
public string Name
7581
{
7682
get { return this.commandName; }
@@ -79,7 +85,7 @@ public string Name
7985
/// <summary>
8086
/// Gets the parameters of the command
8187
/// </summary>
82-
[JsonProperty("parameters")]
88+
[JsonPropertyName("parameters")]
8389
public Dictionary<string, object> Parameters
8490
{
8591
get { return this.commandParameters; }
@@ -95,7 +101,7 @@ public string ParametersAsJsonString
95101
string parametersString = string.Empty;
96102
if (this.commandParameters != null && this.commandParameters.Count > 0)
97103
{
98-
parametersString = JsonConvert.SerializeObject(this.commandParameters);
104+
parametersString = JsonSerializer.Serialize(this.commandParameters);
99105
}
100106

101107
if (string.IsNullOrEmpty(parametersString))
@@ -123,7 +129,7 @@ public override string ToString()
123129
/// <returns>A <see cref="Dictionary{K, V}"/> with a string keys, and an object value. </returns>
124130
private static Dictionary<string, object> ConvertParametersFromJson(string value)
125131
{
126-
Dictionary<string, object> parameters = JsonConvert.DeserializeObject<Dictionary<string, object>>(value, new ResponseValueJsonConverter());
132+
Dictionary<string, object> parameters = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions);
127133
return parameters;
128134
}
129135
}

0 commit comments

Comments
 (0)