Skip to content

Commit 71d03fb

Browse files
committed
system.text.json: update oauth
Update OAuth-related code to use System.Text.Json rather than Json.NET for JSON serialization.
1 parent a155176 commit 71d03fb

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/shared/Core/Authentication/OAuth/Json/DeviceAuthorizationEndpointResponseJson.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
using System;
2-
using Newtonsoft.Json;
2+
using System.Text.Json.Serialization;
33

44
namespace GitCredentialManager.Authentication.OAuth.Json
55
{
66
public class DeviceAuthorizationEndpointResponseJson
77
{
8-
[JsonProperty("device_code", Required = Required.Always)]
8+
[JsonRequired]
9+
[JsonPropertyName("device_code")]
910
public string DeviceCode { get; set; }
1011

11-
[JsonProperty("user_code", Required = Required.Always)]
12+
[JsonRequired]
13+
[JsonPropertyName("user_code")]
1214
public string UserCode { get; set; }
1315

14-
[JsonProperty("verification_uri", Required = Required.Always)]
16+
[JsonRequired]
17+
[JsonPropertyName("verification_uri")]
1518
public Uri VerificationUri { get; set; }
1619

17-
[JsonProperty("expires_in")]
20+
[JsonPropertyName("expires_in")]
1821
public long ExpiresIn { get; set; }
1922

20-
[JsonProperty("interval")]
23+
[JsonPropertyName("interval")]
2124
public long PollingInterval { get; set; }
2225

2326
public OAuth2DeviceCodeResult ToResult()

src/shared/Core/Authentication/OAuth/Json/ErrorResponseJson.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
using System;
22
using System.Text;
3-
using Newtonsoft.Json;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
45

56
namespace GitCredentialManager.Authentication.OAuth.Json
67
{
78
public class ErrorResponseJson
89
{
9-
[JsonProperty("error", Required = Required.Always)]
10+
[JsonRequired]
11+
[JsonPropertyName("error")]
1012
public string Error { get; set; }
1113

12-
[JsonProperty("error_description")]
14+
[JsonPropertyName("error_description")]
1315
public string Description { get; set; }
1416

15-
[JsonProperty("error_uri")]
17+
[JsonPropertyName("error_uri")]
1618
public Uri Uri { get; set; }
1719

1820
public OAuth2Exception ToException(Exception innerException = null)

src/shared/Core/Authentication/OAuth/OAuth2Client.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using GitCredentialManager.Authentication.OAuth.Json;
8-
using Newtonsoft.Json;
8+
using System.Text.Json;
99

1010
namespace GitCredentialManager.Authentication.OAuth
1111
{
@@ -318,7 +318,10 @@ public async Task<OAuth2TokenResult> GetTokenByDeviceCodeAsync(OAuth2DeviceCodeR
318318
return result;
319319
}
320320

321-
var error = JsonConvert.DeserializeObject<ErrorResponseJson>(json);
321+
var error = JsonSerializer.Deserialize<ErrorResponseJson>(json, new JsonSerializerOptions
322+
{
323+
PropertyNameCaseInsensitive = true
324+
});
322325

323326
switch (error.Error)
324327
{
@@ -408,7 +411,7 @@ protected static bool TryDeserializeJson<T>(string json, out T obj)
408411
{
409412
try
410413
{
411-
obj = JsonConvert.DeserializeObject<T>(json);
414+
obj = JsonSerializer.Deserialize<T>(json);
412415
return true;
413416
}
414417
catch

src/shared/TestInfrastructure/Objects/TestOAuth2Server.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Threading.Tasks;
99
using GitCredentialManager.Authentication.OAuth;
1010
using GitCredentialManager.Authentication.OAuth.Json;
11-
using Newtonsoft.Json;
11+
using System.Text.Json;
1212
using Xunit;
1313

1414
namespace GitCredentialManager.Tests.Objects
@@ -159,7 +159,7 @@ private async Task<HttpResponseMessage> OnDeviceAuthorizationEndpointAsync(HttpR
159159
VerificationUri = _deviceCodeVerificationUri,
160160
};
161161

162-
string responseJson = JsonConvert.SerializeObject(deviceResp);
162+
string responseJson = JsonSerializer.Serialize(deviceResp);
163163

164164
return new HttpResponseMessage(HttpStatusCode.OK)
165165
{
@@ -234,7 +234,7 @@ private async Task<HttpResponseMessage> OnTokenEndpointAsync(HttpRequestMessage
234234
Error = OAuth2Constants.DeviceAuthorization.Errors.AuthorizationPending
235235
};
236236

237-
var errorJson = JsonConvert.SerializeObject(errorResp);
237+
var errorJson = JsonSerializer.Serialize(errorResp);
238238

239239
return new HttpResponseMessage(HttpStatusCode.BadRequest)
240240
{
@@ -248,7 +248,7 @@ private async Task<HttpResponseMessage> OnTokenEndpointAsync(HttpRequestMessage
248248
throw new Exception($"Unknown grant type '{grantType}'");
249249
}
250250

251-
string responseJson = JsonConvert.SerializeObject(tokenResp);
251+
string responseJson = JsonSerializer.Serialize(tokenResp);
252252

253253
return new HttpResponseMessage(HttpStatusCode.OK)
254254
{

0 commit comments

Comments
 (0)