Fully migrate to system.text.json

This commit is contained in:
Zoe Roux 2024-03-22 23:34:19 +01:00
parent 7194dcb2c7
commit e7bedd6a29
No known key found for this signature in database
6 changed files with 10 additions and 19 deletions

View File

@ -19,27 +19,27 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using Kyoo.Abstractions.Models;
using Newtonsoft.Json.Linq;
namespace Kyoo.Models;
public class Patch<T> : Dictionary<string, JToken>
public class Patch<T> : Dictionary<string, JsonDocument>
where T : class, IResource
{
public Guid? Id => this.GetValueOrDefault(nameof(IResource.Id))?.ToObject<Guid>();
public Guid? Id => this.GetValueOrDefault(nameof(IResource.Id))?.Deserialize<Guid>();
public string? Slug => this.GetValueOrDefault(nameof(IResource.Slug))?.ToObject<string>();
public string? Slug => this.GetValueOrDefault(nameof(IResource.Slug))?.Deserialize<string>();
public T Apply(T current)
{
foreach ((string property, JToken value) in this)
foreach ((string property, JsonDocument value) in this)
{
PropertyInfo prop = typeof(T).GetProperty(
property,
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance
)!;
prop.SetValue(current, value.ToObject(prop.PropertyType));
prop.SetValue(current, value.Deserialize(prop.PropertyType));
}
return current;
}

View File

@ -20,8 +20,8 @@ using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text.Json.Serialization;
using Kyoo.Abstractions.Models.Attributes;
using Newtonsoft.Json;
namespace Kyoo.Abstractions.Models
{

View File

@ -18,7 +18,6 @@
using System;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace Kyoo.Abstractions.Models;
@ -36,21 +35,18 @@ public class JwtToken(string accessToken, string refreshToken, TimeSpan expireIn
/// <summary>
/// The type of this token (always a Bearer).
/// </summary>
[JsonProperty("token_type")]
[JsonPropertyName("token_type")]
public string TokenType => "Bearer";
/// <summary>
/// The access token used to authorize requests.
/// </summary>
[JsonProperty("access_token")]
[JsonPropertyName("access_token")]
public string AccessToken { get; set; } = accessToken;
/// <summary>
/// The refresh token used to retrieve a new access/refresh token when the access token has expired.
/// </summary>
[JsonProperty("refresh_token")]
[JsonPropertyName("refresh_token")]
public string RefreshToken { get; set; } = refreshToken;
@ -58,14 +54,12 @@ public class JwtToken(string accessToken, string refreshToken, TimeSpan expireIn
/// When the access token will expire. After this time, the refresh token should be used to retrieve.
/// a new token.cs
/// </summary>
[JsonProperty("expire_in")]
[JsonPropertyName("expire_in")]
public TimeSpan ExpireIn => ExpireAt.Subtract(DateTime.UtcNow);
/// <summary>
/// The exact date at which the access token will expire.
/// </summary>
[JsonProperty("expire_at")]
[JsonPropertyName("expire_at")]
public DateTime ExpireAt { get; set; } = DateTime.UtcNow + expireIn;
}

View File

@ -7,7 +7,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.12" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<ProjectReference Include="../Kyoo.Abstractions/Kyoo.Abstractions.csproj" />
</ItemGroup>

View File

@ -12,8 +12,6 @@
<PackageReference Include="InterpolatedSql.Dapper" Version="2.1.0" />
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.12" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SkiaSharp" Version="2.88.6" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" />
</ItemGroup>

View File

@ -17,7 +17,7 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Data;
using Newtonsoft.Json;
using System.Text.Json;
using Npgsql;
using NpgsqlTypes;
using static Dapper.SqlMapper;
@ -30,13 +30,13 @@ public class JsonTypeHandler<T> : TypeHandler<T>
public override T? Parse(object value)
{
if (value is string str)
return JsonConvert.DeserializeObject<T>(str);
return JsonSerializer.Deserialize<T>(str);
return default;
}
public override void SetValue(IDbDataParameter parameter, T? value)
{
parameter.Value = JsonConvert.SerializeObject(value);
parameter.Value = JsonSerializer.Serialize(value);
((NpgsqlParameter)parameter).NpgsqlDbType = NpgsqlDbType.Jsonb;
}
}