From d8234f388973e6effc870f732020a15da15cd1ec Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Fri, 10 Jan 2025 11:17:38 +0000 Subject: [PATCH] Fix System.Text.Json MissingMethodException when targeting net6.0 --- src/lib-csharp/Sources/GiteaSource.cs | 22 ++++++---- src/lib-csharp/Sources/GithubSource.cs | 23 ++++++---- src/lib-csharp/Sources/GitlabSource.cs | 27 +++++++----- src/lib-csharp/Util/CompiledJson.cs | 59 ++------------------------ src/lib-csharp/Velopack.csproj | 3 +- 5 files changed, 50 insertions(+), 84 deletions(-) diff --git a/src/lib-csharp/Sources/GiteaSource.cs b/src/lib-csharp/Sources/GiteaSource.cs index 1cf14abc..720ea1f9 100644 --- a/src/lib-csharp/Sources/GiteaSource.cs +++ b/src/lib-csharp/Sources/GiteaSource.cs @@ -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 { /// Describes a Gitea release, including attached assets. public class GiteaRelease { /// The name of this release. - [JsonPropertyName("name")] + [JsonName("name")] public string? Name { get; set; } /// True if this release is a prerelease. - [JsonPropertyName("prerelease")] + [JsonName("prerelease")] public bool Prerelease { get; set; } /// The date which this release was published publicly. - [JsonPropertyName("published_at")] + [JsonName("published_at")] public DateTime? PublishedAt { get; set; } /// A list of assets (files) uploaded to this release. - [JsonPropertyName("assets")] + [JsonName("assets")] public GiteaReleaseAsset[] Assets { get; set; } = new GiteaReleaseAsset[0]; } /// Describes a asset (file) uploaded to a Gitea release. @@ -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". /// - [JsonPropertyName("url")] + [JsonName("url")] public string? Url { get; set; } /// @@ -41,11 +47,11 @@ namespace Velopack.Sources /// assets from a private repository, the property must /// be used with an appropriate access token. /// - [JsonPropertyName("browser_download_url")] + [JsonName("browser_download_url")] public string? BrowserDownloadUrl { get; set; } /// The name of this release asset. - [JsonPropertyName("name")] + [JsonName("name")] public string? Name { get; set; } } diff --git a/src/lib-csharp/Sources/GithubSource.cs b/src/lib-csharp/Sources/GithubSource.cs index dac9ad56..611d9063 100644 --- a/src/lib-csharp/Sources/GithubSource.cs +++ b/src/lib-csharp/Sources/GithubSource.cs @@ -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 { /// Describes a GitHub release, including attached assets. public class GithubRelease { /// The name of this release. - [JsonPropertyName("name")] + [JsonName("name")] public string? Name { get; set; } /// True if this release is a prerelease. - [JsonPropertyName("prerelease")] + [JsonName("prerelease")] public bool Prerelease { get; set; } /// The date which this release was published publicly. - [JsonPropertyName("published_at")] + [JsonName("published_at")] public DateTime? PublishedAt { get; set; } /// A list of assets (files) uploaded to this release. - [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". /// - [JsonPropertyName("url")] + [JsonName("url")] public string? Url { get; set; } /// @@ -43,15 +48,15 @@ namespace Velopack.Sources /// assets from a private repository, the property must /// be used with an appropriate access token. /// - [JsonPropertyName("browser_download_url")] + [JsonName("browser_download_url")] public string? BrowserDownloadUrl { get; set; } /// The name of this release asset. - [JsonPropertyName("name")] + [JsonName("name")] public string? Name { get; set; } /// The mime type of this release asset (as detected by GitHub). - [JsonPropertyName("content_type")] + [JsonName("content_type")] public string? ContentType { get; set; } } diff --git a/src/lib-csharp/Sources/GitlabSource.cs b/src/lib-csharp/Sources/GitlabSource.cs index d7312725..a51b469a 100644 --- a/src/lib-csharp/Sources/GitlabSource.cs +++ b/src/lib-csharp/Sources/GitlabSource.cs @@ -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 { /// @@ -14,25 +19,25 @@ namespace Velopack.Sources /// /// The name of the release. /// - [JsonPropertyName("name")] + [JsonName("name")] public string? Name { get; set; } /// /// True if this is intended for an upcoming release. /// - [JsonPropertyName("upcoming_release")] + [JsonName("upcoming_release")] public bool UpcomingRelease { get; set; } /// /// The date which this release was published publicly. /// - [JsonPropertyName("released_at")] + [JsonName("released_at")] public DateTime? ReleasedAt { get; set; } /// /// A container for the assets (files) uploaded to this release. /// - [JsonPropertyName("assets")] + [JsonName("assets")] public GitlabReleaseAsset? Assets { get; set; } } @@ -44,13 +49,13 @@ namespace Velopack.Sources /// /// The amount of assets linked to the release. /// - [JsonPropertyName("count")] + [JsonName("count")] public int Count { get; set; } /// /// A list of asset (file) links. /// - [JsonPropertyName("links")] + [JsonName("links")] public GitlabReleaseLink[] Links { get; set; } = new GitlabReleaseLink[0]; } @@ -62,13 +67,13 @@ namespace Velopack.Sources /// /// Name of the asset (file) linked. /// - [JsonPropertyName("name")] + [JsonName("name")] public string? Name { get; set; } /// /// The url for the asset. This make use of the Gitlab API. /// - [JsonPropertyName("url")] + [JsonName("url")] public string? Url { get; set; } /// @@ -76,14 +81,14 @@ namespace Velopack.Sources /// As a posed to using the API. /// This links directly to the raw asset (file). /// - [JsonPropertyName("direct_asset_url")] + [JsonName("direct_asset_url")] public string? DirectAssetUrl { get; set; } /// /// The category type that the asset is listed under. /// Options: 'Package', 'Image', 'Runbook', 'Other' /// - [JsonPropertyName("link_type")] + [JsonName("link_type")] public string? Type { get; set; } } diff --git a/src/lib-csharp/Util/CompiledJson.cs b/src/lib-csharp/Util/CompiledJson.cs index bafa87d5..046fe178 100644 --- a/src/lib-csharp/Util/CompiledJson.cs +++ b/src/lib-csharp/Util/CompiledJson.cs @@ -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))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] [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>(json, Options); } - public static VelopackAsset[]? DeserializeVelopackAssetArray(string json) - { - return JsonConvert.DeserializeObject(json, Options); - } - public static VelopackFlowReleaseAsset[]? DeserializeVelopackFlowAssetArray(string json) { return JsonConvert.DeserializeObject(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() is { } stj) { - property.PropertyName = stj.Name; - return property; - } - return property; - } - } #endif } \ No newline at end of file diff --git a/src/lib-csharp/Velopack.csproj b/src/lib-csharp/Velopack.csproj index 639683f4..54f82bb9 100644 --- a/src/lib-csharp/Velopack.csproj +++ b/src/lib-csharp/Velopack.csproj @@ -11,7 +11,7 @@ false - true + true @@ -34,6 +34,7 @@ +