Replace SimpleJson for Newtonsoft on netfx

This commit is contained in:
Caelan Sayler
2024-01-15 20:31:24 +00:00
parent c9f7073761
commit f0dca85ed6
7 changed files with 247 additions and 2183 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -2,41 +2,40 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Velopack.Json;
namespace Velopack.Sources
{
/// <summary> Describes a GitHub release, including attached assets. </summary>
[DataContract]
public class GithubRelease
{
/// <summary> The name of this release. </summary>
[DataMember(Name = "name")]
[JsonPropertyName("name")]
public string Name { get; set; }
/// <summary> True if this release is a prerelease. </summary>
[DataMember(Name = "prerelease")]
[JsonPropertyName("prerelease")]
public bool Prerelease { get; set; }
/// <summary> The date which this release was published publically. </summary>
[DataMember(Name = "published_at")]
[JsonPropertyName("published_at")]
public DateTime? PublishedAt { get; set; }
/// <summary> A list of assets (files) uploaded to this release. </summary>
[DataMember(Name = "assets")]
[JsonPropertyName("assets")]
public GithubReleaseAsset[] Assets { get; set; }
}
/// <summary> Describes a asset (file) uploaded to a GitHub release. </summary>
[DataContract]
public class GithubReleaseAsset
{
/// <summary>
/// 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>
[DataMember(Name = "url")]
[JsonPropertyName("url")]
public string Url { get; set; }
/// <summary>
@@ -45,15 +44,15 @@ namespace Velopack.Sources
/// assets from a private repository, the <see cref="Url"/> property must
/// be used with an appropriate access token.
/// </summary>
[DataMember(Name = "browser_download_url")]
[JsonPropertyName("browser_download_url")]
public string BrowserDownloadUrl { get; set; }
/// <summary> The name of this release asset. </summary>
[DataMember(Name = "name")]
[JsonPropertyName("name")]
public string Name { get; set; }
/// <summary> The mime type of this release asset (as detected by GitHub). </summary>
[DataMember(Name = "content_type")]
[JsonPropertyName("content_type")]
public string ContentType { get; set; }
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Velopack.Json;
@@ -11,69 +12,66 @@ namespace Velopack.Sources
/// <summary>
/// Describes a Gitlab release, plus any assets that are attached.
/// </summary>
[DataContract]
public class GitlabRelease
{
/// <summary>
/// The name of the release.
/// </summary>
[DataMember(Name = "name")]
[JsonPropertyName("name")]
public string Name { get; set; }
/// <summary>
/// True if this is intended for an upcoming release.
/// </summary>
[DataMember(Name = "upcoming_release")]
[JsonPropertyName("upcoming_release")]
public bool UpcomingRelease { get; set; }
/// <summary>
/// The date which this release was published publically.
/// </summary>
[DataMember(Name = "released_at")]
[JsonPropertyName("released_at")]
public DateTime ReleasedAt { get; set; }
/// <summary>
/// A container for the assets (files) uploaded to this release.
/// </summary>
[DataMember(Name = "assets")]
[JsonPropertyName("assets")]
public GitlabReleaseAsset Assets { get; set; }
}
/// <summary>
/// Describes a container for the assets attached to a release.
/// </summary>
[DataContract]
public class GitlabReleaseAsset
{
/// <summary>
/// The amount of assets linked to the release.
/// </summary>
[DataMember(Name = "count")]
[JsonPropertyName("count")]
public int Count { get; set; }
/// <summary>
/// A list of asset (file) links.
/// </summary>
[DataMember(Name = "links")]
[JsonPropertyName("links")]
public GitlabReleaseLink[] Links { get; set; }
}
/// <summary>
/// Describes a container for the links of assets attached to a release.
/// </summary>
[DataContract]
public class GitlabReleaseLink
{
/// <summary>
/// Name of the asset (file) linked.
/// </summary>
[DataMember(Name = "name")]
[JsonPropertyName("name")]
public string Name { get; set; }
/// <summary>
/// The url for the asset. This make use of the Gitlab API.
/// </summary>
[DataMember(Name = "url")]
[JsonPropertyName("url")]
public string Url { get; set; }
/// <summary>
@@ -81,14 +79,14 @@ namespace Velopack.Sources
/// As a posed to using the API.
/// This links directly to the raw asset (file).
/// </summary>
[DataMember(Name = "direct_asset_url")]
[JsonPropertyName("direct_asset_url")]
public string DirectAssetUrl { get; set; }
/// <summary>
/// The category type that the asset is listed under.
/// Options: 'Package', 'Image', 'Runbook', 'Other'
/// </summary>
[DataMember(Name = "link_type")]
[JsonPropertyName("link_type")]
public string Type { get; set; }
}

View File

@@ -34,6 +34,10 @@
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework) == 'netstandard2.0' OR $(TargetFramework.StartsWith('net4')) ">
<PackageReference Include="Newtonsoft.Json" Version="[13.0.1,)" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) ">
<Reference Include="System.Web" />
<Reference Include="System.Net.Http" />

View File

@@ -45,7 +45,7 @@ namespace Velopack
public string PackageId { get; init; }
/// <summary> The version of this release. </summary>
public string Version { get; init; }
public SemanticVersion Version { get; init; }
/// <summary> The type of asset (eg. full or delta). </summary>
public VelopackAssetType Type { get; init; }
@@ -73,7 +73,7 @@ namespace Velopack
var filePath = zip.LoadedFromPath;
return new VelopackAsset {
PackageId = zip.Id,
Version = zip.Version.ToNormalizedString(),
Version = zip.Version,
NotesMarkdown = zip.ReleaseNotes,
NotesHTML = zip.ReleaseNotesHtml,
Size = new FileInfo(filePath).Length,

View File

@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
using NuGet.Versioning;
using Velopack.Json;
using Velopack.Sources;
using JsonPropertyNameAttribute = System.Text.Json.Serialization.JsonPropertyNameAttribute;
#if NET5_0_OR_GREATER
using SimpleJsonNameAttribute = System.Text.Json.Serialization.JsonPropertyNameAttribute;
#else
using SimpleJsonNameAttribute = Velopack.Json.JsonPropertyNameAttribute;
#endif
namespace Velopack.Tests
{
public class SimpleJsonTests
{
public static readonly JsonSerializerOptions Options = new JsonSerializerOptions {
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter(), new SemanticVersionConverter() },
};
internal class SemanticVersionConverter : JsonConverter<SemanticVersion>
{
public override SemanticVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return SemanticVersion.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, SemanticVersion value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToFullString());
}
}
[Fact]
public void JsonPropertyNameAttribueWrks()
{
var obj = new TestGithubReleaseAsset {
UrlSomething = "https://ho",
BrowserDownloadUrl = "https://browser",
ContentType = "via",
};
var json = JsonSerializer.Serialize(obj, Options);
var dez = SimpleJson.DeserializeObject<GithubReleaseAsset>(json);
Assert.Equal(obj.UrlSomething, dez.Url);
Assert.Equal(obj.BrowserDownloadUrl, dez.BrowserDownloadUrl);
Assert.Equal(obj.ContentType, dez.ContentType);
}
[Fact]
public void JsonCanRoundTripComplexTypes()
{
var obj = new TestClass1 {
NameAsd = "hello",
UpcomingRelease = true,
ReleasedAt = DateTime.UtcNow,
Version = SemanticVersion.Parse("1.2.3-hello.23+metadata"),
AssetType = VelopackAssetType.Delta,
Greetings = new List<string> { "hi", "there" },
};
var json = JsonSerializer.Serialize(obj, Options);
Assert.Contains("\"Delta\"", json);
var dez = SimpleJson.DeserializeObject<TestClass2>(json);
Assert.Equal(obj.NameAsd, dez.nameAsd);
Assert.Equal(obj.UpcomingRelease, dez.upcomingRelease);
Assert.Equal(obj.ReleasedAt, dez.releasedAt);
Assert.Equal(obj.Version, dez.version);
Assert.Equal(obj.AssetType, dez.assetType);
Assert.Equal(obj.Greetings, dez.greetings);
}
public class TestGithubReleaseAsset
{
/// <summary>
/// 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")]
public string UrlSomething { get; set; }
/// <summary>
/// The browser URL for this release asset. This does not use API quota,
/// however this URL only works for public repositories. If downloading
/// assets from a private repository, the <see cref="Url"/> property must
/// be used with an appropriate access token.
/// </summary>
[JsonPropertyName("browser_download_url")]
public string BrowserDownloadUrl { get; set; }
/// <summary> The mime type of this release asset (as detected by GitHub). </summary>
[JsonPropertyName("content_type")]
public string ContentType { get; set; }
}
internal class TestClass1
{
public string NameAsd { get; set; }
[JsonPropertyName("upcoming_release888")]
public bool UpcomingRelease { get; set; }
[JsonPropertyName("released_at")]
public DateTime ReleasedAt { get; set; }
public SemanticVersion Version { get; set; }
[JsonPropertyName("t")]
public VelopackAssetType AssetType { get; set; }
public List<string> Greetings { get; set; }
}
internal class TestClass2
{
public string nameAsd { get; set; }
[SimpleJsonName("upcoming_release888")]
public bool upcomingRelease { get; set; }
[SimpleJsonName("released_at")]
public DateTime releasedAt { get; set; }
public SemanticVersion version { get; set; }
[SimpleJsonName("t")]
public VelopackAssetType assetType { get; set; }
public List<string> greetings { get; set; }
}
}
}

View File

@@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="System.IO.Packaging" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) ">