Fix System.Text.Json MissingMethodException when targeting net6.0

This commit is contained in:
Caelan Sayler
2025-01-10 11:17:38 +00:00
committed by Caelan
parent 379e533c78
commit d8234f3889
5 changed files with 50 additions and 84 deletions

View File

@@ -1,28 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Velopack.Util;
#if NET8_0_OR_GREATER
using JsonName = System.Text.Json.Serialization.JsonPropertyNameAttribute;
#else
using JsonName = Newtonsoft.Json.JsonPropertyAttribute;
#endif
namespace Velopack.Sources
{
/// <summary> Describes a Gitea release, including attached assets. </summary>
public class GiteaRelease
{
/// <summary> The name of this release. </summary>
[JsonPropertyName("name")]
[JsonName("name")]
public string? Name { get; set; }
/// <summary> True if this release is a prerelease. </summary>
[JsonPropertyName("prerelease")]
[JsonName("prerelease")]
public bool Prerelease { get; set; }
/// <summary> The date which this release was published publicly. </summary>
[JsonPropertyName("published_at")]
[JsonName("published_at")]
public DateTime? PublishedAt { get; set; }
/// <summary> A list of assets (files) uploaded to this release. </summary>
[JsonPropertyName("assets")]
[JsonName("assets")]
public GiteaReleaseAsset[] Assets { get; set; } = new GiteaReleaseAsset[0];
}
/// <summary> Describes a asset (file) uploaded to a Gitea release. </summary>
@@ -32,7 +38,7 @@ namespace Velopack.Sources
/// The asset URL for this release asset. Requests to this URL will use API
/// quota and return JSON unless the 'Accept' header is "application/octet-stream".
/// </summary>
[JsonPropertyName("url")]
[JsonName("url")]
public string? Url { get; set; }
/// <summary>
@@ -41,11 +47,11 @@ namespace Velopack.Sources
/// assets from a private repository, the <see cref="Url"/> property must
/// be used with an appropriate access token.
/// </summary>
[JsonPropertyName("browser_download_url")]
[JsonName("browser_download_url")]
public string? BrowserDownloadUrl { get; set; }
/// <summary> The name of this release asset. </summary>
[JsonPropertyName("name")]
[JsonName("name")]
public string? Name { get; set; }
}

View File

@@ -1,29 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Velopack.Util;
#if NET8_0_OR_GREATER
using JsonName = System.Text.Json.Serialization.JsonPropertyNameAttribute;
#else
using JsonName = Newtonsoft.Json.JsonPropertyAttribute;
#endif
namespace Velopack.Sources
{
/// <summary> Describes a GitHub release, including attached assets. </summary>
public class GithubRelease
{
/// <summary> The name of this release. </summary>
[JsonPropertyName("name")]
[JsonName("name")]
public string? Name { get; set; }
/// <summary> True if this release is a prerelease. </summary>
[JsonPropertyName("prerelease")]
[JsonName("prerelease")]
public bool Prerelease { get; set; }
/// <summary> The date which this release was published publicly. </summary>
[JsonPropertyName("published_at")]
[JsonName("published_at")]
public DateTime? PublishedAt { get; set; }
/// <summary> A list of assets (files) uploaded to this release. </summary>
[JsonPropertyName("assets")]
[JsonName("assets")]
public GithubReleaseAsset[] Assets { get; set; } = new GithubReleaseAsset[0];
}
@@ -34,7 +39,7 @@ namespace Velopack.Sources
/// The asset URL for this release asset. Requests to this URL will use API
/// quota and return JSON unless the 'Accept' header is "application/octet-stream".
/// </summary>
[JsonPropertyName("url")]
[JsonName("url")]
public string? Url { get; set; }
/// <summary>
@@ -43,15 +48,15 @@ namespace Velopack.Sources
/// assets from a private repository, the <see cref="Url"/> property must
/// be used with an appropriate access token.
/// </summary>
[JsonPropertyName("browser_download_url")]
[JsonName("browser_download_url")]
public string? BrowserDownloadUrl { get; set; }
/// <summary> The name of this release asset. </summary>
[JsonPropertyName("name")]
[JsonName("name")]
public string? Name { get; set; }
/// <summary> The mime type of this release asset (as detected by GitHub). </summary>
[JsonPropertyName("content_type")]
[JsonName("content_type")]
public string? ContentType { get; set; }
}

View File

@@ -1,9 +1,14 @@
using System;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Velopack.Util;
#if NET8_0_OR_GREATER
using JsonName = System.Text.Json.Serialization.JsonPropertyNameAttribute;
#else
using JsonName = Newtonsoft.Json.JsonPropertyAttribute;
#endif
namespace Velopack.Sources
{
/// <summary>
@@ -14,25 +19,25 @@ namespace Velopack.Sources
/// <summary>
/// The name of the release.
/// </summary>
[JsonPropertyName("name")]
[JsonName("name")]
public string? Name { get; set; }
/// <summary>
/// True if this is intended for an upcoming release.
/// </summary>
[JsonPropertyName("upcoming_release")]
[JsonName("upcoming_release")]
public bool UpcomingRelease { get; set; }
/// <summary>
/// The date which this release was published publicly.
/// </summary>
[JsonPropertyName("released_at")]
[JsonName("released_at")]
public DateTime? ReleasedAt { get; set; }
/// <summary>
/// A container for the assets (files) uploaded to this release.
/// </summary>
[JsonPropertyName("assets")]
[JsonName("assets")]
public GitlabReleaseAsset? Assets { get; set; }
}
@@ -44,13 +49,13 @@ namespace Velopack.Sources
/// <summary>
/// The amount of assets linked to the release.
/// </summary>
[JsonPropertyName("count")]
[JsonName("count")]
public int Count { get; set; }
/// <summary>
/// A list of asset (file) links.
/// </summary>
[JsonPropertyName("links")]
[JsonName("links")]
public GitlabReleaseLink[] Links { get; set; } = new GitlabReleaseLink[0];
}
@@ -62,13 +67,13 @@ namespace Velopack.Sources
/// <summary>
/// Name of the asset (file) linked.
/// </summary>
[JsonPropertyName("name")]
[JsonName("name")]
public string? Name { get; set; }
/// <summary>
/// The url for the asset. This make use of the Gitlab API.
/// </summary>
[JsonPropertyName("url")]
[JsonName("url")]
public string? Url { get; set; }
/// <summary>
@@ -76,14 +81,14 @@ namespace Velopack.Sources
/// As a posed to using the API.
/// This links directly to the raw asset (file).
/// </summary>
[JsonPropertyName("direct_asset_url")]
[JsonName("direct_asset_url")]
public string? DirectAssetUrl { get; set; }
/// <summary>
/// The category type that the asset is listed under.
/// Options: 'Package', 'Image', 'Runbook', 'Other'
/// </summary>
[JsonPropertyName("link_type")]
[JsonName("link_type")]
public string? Type { get; set; }
}

View File

@@ -1,39 +1,25 @@
using System;
using System.Collections.Generic;
using NuGet.Versioning;
using Velopack.Sources;
using System.Collections.Generic;
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
using System.Text.Json;
using System.Text.Json.Serialization;
#else
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
#endif
#if !NET6_0_OR_GREATER
namespace System.Text.Json.Serialization
{
// this is just here so our code can "use" System.Text.Json.Serialization
// without having conditional compilation everywhere
internal class JsonPlaceholderNoopDontUse { }
}
#endif
namespace Velopack.Util
{
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
[JsonSerializable(typeof(List<GithubRelease>))]
[JsonSerializable(typeof(List<GitlabRelease>))]
[JsonSerializable(typeof(List<GiteaRelease>))]
[JsonSerializable(typeof(VelopackAssetFeed))]
[JsonSerializable(typeof(VelopackFlowReleaseAsset[]))]
#if NET8_0_OR_GREATER
[JsonSourceGenerationOptions(UseStringEnumConverter = true)]
#endif
internal partial class CompiledJsonSourceGenerationContext : JsonSerializerContext
{
}
@@ -47,9 +33,6 @@ namespace Velopack.Util
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = {
#if !NET8_0_OR_GREATER
new JsonStringEnumConverter(),
#endif
new SemanticVersionConverter(),
},
};
@@ -71,11 +54,6 @@ namespace Velopack.Util
return JsonSerializer.Deserialize(json, Context.ListGitlabRelease);
}
public static VelopackAsset[]? DeserializeVelopackAssetArray(string json)
{
return JsonSerializer.Deserialize(json, Context.VelopackAssetArray);
}
public static VelopackFlowReleaseAsset[]? DeserializeVelopackFlowAssetArray(string json)
{
return JsonSerializer.Deserialize(json, Context.VelopackFlowReleaseAssetArray);
@@ -102,21 +80,10 @@ namespace Velopack.Util
}
}
#else
internal class JsonPropertyNameAttribute : Attribute
{
public string Name { get; }
public JsonPropertyNameAttribute(string name)
{
Name = name;
}
}
internal static class CompiledJson
{
private static readonly JsonSerializerSettings Options = new JsonSerializerSettings {
private static readonly JsonSerializerSettings Options = new() {
Converters = { new StringEnumConverter(), new SemanticVersionConverter() },
ContractResolver = new JsonNameContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
};
@@ -135,11 +102,6 @@ namespace Velopack.Util
return JsonConvert.DeserializeObject<List<GitlabRelease>>(json, Options);
}
public static VelopackAsset[]? DeserializeVelopackAssetArray(string json)
{
return JsonConvert.DeserializeObject<VelopackAsset[]>(json, Options);
}
public static VelopackFlowReleaseAsset[]? DeserializeVelopackFlowAssetArray(string json)
{
return JsonConvert.DeserializeObject<VelopackFlowReleaseAsset[]>(json, Options);
@@ -169,18 +131,5 @@ namespace Velopack.Util
}
}
}
internal class JsonNameContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if (member.GetCustomAttribute<JsonPropertyNameAttribute>() is { } stj) {
property.PropertyName = stj.Name;
return property;
}
return property;
}
}
#endif
}

View File

@@ -11,7 +11,7 @@
<PropertyGroup>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
</PropertyGroup>
<ItemGroup>
@@ -34,6 +34,7 @@
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework) == 'net6.0' ">
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="[6.0.0,)" />
</ItemGroup>