Get rid of Newtonsoft

This commit is contained in:
Aleh Khantsevich
2024-07-14 20:35:17 +02:00
parent fbdd068274
commit 403e5e5de0
18 changed files with 43 additions and 199 deletions

View File

@@ -1,9 +1,9 @@
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Nito.AsyncEx;
using Wino.Core.Domain;
using Wino.Core.Domain.Entities;
@@ -63,14 +63,14 @@ namespace Wino.Core.Authenticators
if (!response.IsSuccessStatusCode)
throw new GoogleAuthenticationException(Translator.Exception_GoogleAuthorizationCodeExchangeFailed);
var parsed = JObject.Parse(responseString);
var parsed = JsonNode.Parse(responseString).AsObject();
if (parsed.ContainsKey("error"))
throw new GoogleAuthenticationException(parsed["error"]["message"].Value<string>());
throw new GoogleAuthenticationException(parsed["error"]["message"].GetValue<string>());
var accessToken = parsed["access_token"].Value<string>();
var refreshToken = parsed["refresh_token"].Value<string>();
var expiresIn = parsed["expires_in"].Value<long>();
var accessToken = parsed["access_token"].GetValue<string>();
var refreshToken = parsed["refresh_token"].GetValue<string>();
var expiresIn = parsed["expires_in"].GetValue<long>();
var expirationDate = DateTime.UtcNow.AddSeconds(expiresIn);
@@ -81,12 +81,12 @@ namespace Wino.Core.Authenticators
var userinfoResponse = await client.GetAsync(UserInfoEndpoint);
string userinfoResponseContent = await userinfoResponse.Content.ReadAsStringAsync();
var parsedUserInfo = JObject.Parse(userinfoResponseContent);
var parsedUserInfo = JsonNode.Parse(userinfoResponseContent).AsObject();
if (parsedUserInfo.ContainsKey("error"))
throw new GoogleAuthenticationException(parsedUserInfo["error"]["message"].Value<string>());
throw new GoogleAuthenticationException(parsedUserInfo["error"]["message"].GetValue<string>());
var username = parsedUserInfo["emailAddress"].Value<string>();
var username = parsedUserInfo["emailAddress"].GetValue<string>();
return new TokenInformation()
{
@@ -182,14 +182,13 @@ namespace Wino.Core.Authenticators
var response = await client.PostAsync(RefreshTokenEndpoint, content);
string responseString = await response.Content.ReadAsStringAsync();
var parsed = JObject.Parse(responseString);
var parsed = JsonNode.Parse(responseString).AsObject();
// TODO: Error parsing is incorrect.
if (parsed.ContainsKey("error"))
throw new GoogleAuthenticationException(parsed["error_description"].Value<string>());
throw new GoogleAuthenticationException(parsed["error_description"].GetValue<string>());
var accessToken = parsed["access_token"].Value<string>();
var accessToken = parsed["access_token"].GetValue<string>();
string activeRefreshToken = refresh_token;
@@ -199,10 +198,10 @@ namespace Wino.Core.Authenticators
if (parsed.ContainsKey("refresh_token"))
{
activeRefreshToken = parsed["refresh_token"].Value<string>();
activeRefreshToken = parsed["refresh_token"].GetValue<string>();
}
var expiresIn = parsed["expires_in"].Value<long>();
var expiresIn = parsed["expires_in"].GetValue<long>();
var expirationDate = DateTime.UtcNow.AddSeconds(expiresIn);
return new TokenInformationBase()

View File

@@ -1,36 +0,0 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Wino.Core.Http
{
/// <summary>
/// We need to generate HttpRequestMessage for batch requests, and sometimes we need to
/// serialize content as json. However, some of the fields like 'ODataType' must be ignored
/// in order PATCH requests to succeed. Therefore Microsoft account synchronizer uses
/// special JsonSerializerSettings for ignoring some of the properties.
/// </summary>
public class MicrosoftJsonContractResolver : DefaultContractResolver
{
private readonly HashSet<string> ignoreProps = new HashSet<string>()
{
"ODataType"
};
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (ignoreProps.Contains(property.PropertyName))
{
property.ShouldSerialize = _ => false;
}
return property;
}
}
}

View File

@@ -1,9 +1,9 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Wino.Core.Domain.Interfaces;
namespace Wino.Core.Domain.Models.Requests
@@ -43,7 +43,7 @@ namespace Wino.Core.Domain.Models.Requests
{
var content = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<TResponse>(content) ?? throw new InvalidOperationException("Invalid Http Response Deserialization");
return JsonSerializer.Deserialize<TResponse>(content) ?? throw new InvalidOperationException("Invalid Http Response Deserialization");
}
public override string ToString()

View File

@@ -1,7 +1,7 @@
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Serilog;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.AutoDiscovery;
@@ -43,7 +43,7 @@ namespace Wino.Core.Services
{
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<AutoDiscoverySettings>(content);
return JsonSerializer.Deserialize<AutoDiscoverySettings>(content);
}
catch (Exception ex)
{

View File

@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Messaging;
using Newtonsoft.Json;
using Serilog;
using Wino.Core.Domain;
using Wino.Core.Domain.Enums;
@@ -44,7 +44,7 @@ namespace Wino.Core.Services
var stremValue = await new StreamReader(resourceStream).ReadToEndAsync().ConfigureAwait(false);
var translationLookups = JsonConvert.DeserializeObject<Dictionary<string, string>>(stremValue);
var translationLookups = JsonSerializer.Deserialize<Dictionary<string, string>>(stremValue);
// Insert new translation key-value pairs.
// Overwrite existing values for the same keys.

View File

@@ -19,7 +19,6 @@
<PackageReference Include="Microsoft.Identity.Client" Version="4.61.3" />
<PackageReference Include="MimeKit" Version="4.7.1" />
<PackageReference Include="morelinq" Version="4.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Nito.AsyncEx.Tasks" Version="5.1.2" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />

View File

@@ -24,7 +24,6 @@
<PackageReference Include="Microsoft.Identity.Client" Version="4.47.2" />
<PackageReference Include="MimeKit" Version="4.6.0" />
<PackageReference Include="morelinq" Version="4.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Nito.AsyncEx.Tasks" Version="5.1.2" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />