Skip to content

Commit 9c16b00

Browse files
authored
Move to System.Text.Json (loic-sharma#559)
Replaces Newtonsoft.Json with System.Text.Json. This will simplifies the route-to-code migration (see loic-sharma#558).
1 parent 58e00e8 commit 9c16b00

File tree

69 files changed

+1012
-660
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1012
-660
lines changed

.azure/pipelines/ci-official.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
command: custom
5757
workingDir: src/BaGet.UI
5858
customCommand: run build
59-
59+
6060
- task: Npm@1
6161
displayName: Test frontend
6262
inputs:
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
23
using BaGet.Protocol.Models;
3-
using Newtonsoft.Json;
44

55
namespace BaGet.Core
66
{
@@ -10,26 +10,25 @@ namespace BaGet.Core
1010
/// </summary>
1111
public class BaGetPackageMetadata : PackageMetadata
1212
{
13-
[JsonProperty("downloads")]
13+
[JsonPropertyName("downloads")]
1414
public long Downloads { get; set; }
1515

16-
[JsonProperty("hasReadme")]
16+
[JsonPropertyName("hasReadme")]
1717
public bool HasReadme { get; set; }
1818

19-
[JsonProperty("packageTypes")]
19+
[JsonPropertyName("packageTypes")]
2020
public IReadOnlyList<string> PackageTypes { get; set; }
2121

2222
/// <summary>
2323
/// The package's release notes.
2424
/// </summary>
25-
[JsonProperty("releaseNotes")]
25+
[JsonPropertyName("releaseNotes")]
2626
public string ReleaseNotes { get; set; }
2727

28-
[JsonProperty("repositoryUrl")]
28+
[JsonPropertyName("repositoryUrl")]
2929
public string RepositoryUrl { get; set; }
3030

31-
[JsonProperty("repositoryType")]
31+
[JsonPropertyName("repositoryType")]
3232
public string RepositoryType { get; set; }
33-
3433
}
3534
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
3+
using BaGet.Protocol.Models;
4+
5+
namespace BaGet.Core
6+
{
7+
/// <summary>
8+
/// BaGet's extensions to a registration index page.
9+
/// Extends <see cref="RegistrationIndexPage"/>.
10+
/// </summary>
11+
/// <remarks>
12+
/// TODO: After this project is updated to .NET 5, make <see cref="BaGetRegistrationIndexPage"/>
13+
/// extend <see cref="RegistrationIndexPage"/> and remove identical properties.
14+
/// Properties that are modified should be marked with the "new" modified.
15+
/// See: https://github.com/dotnet/runtime/pull/32107
16+
/// </remarks>
17+
public class BaGetRegistrationIndexPage
18+
{
19+
#region Original properties from RegistrationIndexPage.
20+
[JsonPropertyName("@id")]
21+
public string RegistrationPageUrl { get; set; }
22+
23+
[JsonPropertyName("count")]
24+
public int Count { get; set; }
25+
26+
[JsonPropertyName("lower")]
27+
public string Lower { get; set; }
28+
29+
[JsonPropertyName("upper")]
30+
public string Upper { get; set; }
31+
#endregion
32+
33+
/// <summary>
34+
/// This was modified to use BaGet's extended registration index page item model.
35+
/// </summary>
36+
[JsonPropertyName("items")]
37+
public IReadOnlyList<BaGetRegistrationIndexPageItem> ItemsOrNull { get; set; }
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Text.Json.Serialization;
2+
using BaGet.Protocol.Models;
3+
4+
namespace BaGet.Core
5+
{
6+
/// <summary>
7+
/// BaGet's extensions to a registration index page.
8+
/// Extends <see cref="RegistrationIndexPageItem"/>.
9+
/// </summary>
10+
/// <remarks>
11+
/// TODO: After this project is updated to .NET 5, make <see cref="BaGetRegistrationIndexPageItem"/>
12+
/// extend <see cref="RegistrationIndexPageItem"/> and remove identical properties.
13+
/// Properties that are modified should be marked with the "new" modified.
14+
/// See: https://github.com/dotnet/runtime/pull/32107
15+
/// </remarks>
16+
public class BaGetRegistrationIndexPageItem
17+
{
18+
#region Original properties from RegistrationIndexPageItem.
19+
[JsonPropertyName("@id")]
20+
public string RegistrationLeafUrl { get; set; }
21+
22+
[JsonPropertyName("packageContent")]
23+
public string PackageContentUrl { get; set; }
24+
#endregion
25+
26+
/// <summary>
27+
/// The catalog entry containing the package metadata.
28+
/// This was modified to use BaGet's extended package metadata model.
29+
/// </summary>
30+
[JsonPropertyName("catalogEntry")]
31+
public BaGetPackageMetadata PackageMetadata { get; set; }
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Serialization;
13
using BaGet.Protocol.Models;
2-
using Newtonsoft.Json;
34

45
namespace BaGet.Core
56
{
67
/// <summary>
7-
/// BaGet's extensions to a registration index response. These additions
8-
/// are not part of the official protocol.
8+
/// BaGet's extensions to a registration index response.
9+
/// Extends <see cref="RegistrationIndexResponse"/>.
910
/// </summary>
10-
public class BaGetRegistrationIndexResponse : RegistrationIndexResponse
11+
/// <remarks>
12+
/// TODO: After this project is updated to .NET 5, make <see cref="BaGetRegistrationIndexResponse"/>
13+
/// extend <see cref="RegistrationIndexResponse"/> and remove identical properties.
14+
/// Properties that are modified should be marked with the "new" modified.
15+
/// See: https://github.com/dotnet/runtime/pull/32107
16+
/// </remarks>
17+
public class BaGetRegistrationIndexResponse
1118
{
19+
#region Original properties from RegistrationIndexResponse.
20+
[JsonPropertyName("@id")]
21+
public string RegistrationIndexUrl { get; set; }
22+
23+
[JsonPropertyName("@type")]
24+
public IReadOnlyList<string> Type { get; set; }
25+
26+
[JsonPropertyName("count")]
27+
public int Count { get; set; }
28+
#endregion
29+
30+
/// <summary>
31+
/// The pages that contain all of the versions of the package, ordered
32+
/// by the package's version. This was modified to use BaGet's extended
33+
/// registration index page model.
34+
/// </summary>
35+
[JsonPropertyName("items")]
36+
public IReadOnlyList<BaGetRegistrationIndexPage> Pages { get; set; }
37+
1238
/// <summary>
13-
/// How many times all versions of this package have been downloaded.
39+
/// The package's total downloads across all versions.
40+
/// This is not part of the official NuGet protocol.
1441
/// </summary>
15-
[JsonProperty("totalDownloads")]
42+
[JsonPropertyName("totalDownloads")]
1643
public long TotalDownloads { get; set; }
1744
}
1845
}

src/BaGet.Core/Metadata/BaGetRegistrationLeafResponse.cs

-15
This file was deleted.

src/BaGet.Core/Metadata/DefaultPackageMetadataService.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using BaGet.Protocol.Models;
67
using NuGet.Packaging.Core;
78
using NuGet.Versioning;
89

@@ -41,7 +42,7 @@ public async Task<BaGetRegistrationIndexResponse> GetRegistrationIndexOrNullAsyn
4142
packages));
4243
}
4344

44-
public async Task<BaGetRegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
45+
public async Task<RegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
4546
string id,
4647
NuGetVersion version,
4748
CancellationToken cancellationToken = default)

src/BaGet.Core/Metadata/IPackageMetadataService.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading;
22
using System.Threading.Tasks;
3+
using BaGet.Protocol.Models;
34
using NuGet.Versioning;
45

56
namespace BaGet.Core
@@ -27,7 +28,7 @@ public interface IPackageMetadataService
2728
/// <param name="packageVersion">The package's version.</param>
2829
/// <param name="cancellationToken">A token to cancel the task.</param>
2930
/// <returns>The registration leaf, or null if the package does not exist.</returns>
30-
Task<BaGetRegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
31+
Task<RegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
3132
string packageId,
3233
NuGetVersion packageVersion,
3334
CancellationToken cancellationToken = default);

src/BaGet.Core/Metadata/RegistrationBuilder.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public virtual BaGetRegistrationIndexResponse BuildIndex(PackageRegistration reg
2929
TotalDownloads = registration.Packages.Sum(p => p.Downloads),
3030
Pages = new[]
3131
{
32-
new RegistrationIndexPage
32+
new BaGetRegistrationIndexPage
3333
{
3434
RegistrationPageUrl = _url.GetRegistrationIndexUrl(registration.PackageId),
3535
Count = registration.Packages.Count(),
@@ -41,25 +41,24 @@ public virtual BaGetRegistrationIndexResponse BuildIndex(PackageRegistration reg
4141
};
4242
}
4343

44-
public virtual BaGetRegistrationLeafResponse BuildLeaf(Package package)
44+
public virtual RegistrationLeafResponse BuildLeaf(Package package)
4545
{
4646
var id = package.Id;
4747
var version = package.Version;
4848

49-
return new BaGetRegistrationLeafResponse
49+
return new RegistrationLeafResponse
5050
{
5151
Type = RegistrationLeafResponse.DefaultType,
5252
Listed = package.Listed,
53-
Downloads = package.Downloads,
5453
Published = package.Published,
5554
RegistrationLeafUrl = _url.GetRegistrationLeafUrl(id, version),
5655
PackageContentUrl = _url.GetPackageDownloadUrl(id, version),
5756
RegistrationIndexUrl = _url.GetRegistrationIndexUrl(id)
5857
};
5958
}
6059

61-
private RegistrationIndexPageItem ToRegistrationIndexPageItem(Package package) =>
62-
new RegistrationIndexPageItem
60+
private BaGetRegistrationIndexPageItem ToRegistrationIndexPageItem(Package package) =>
61+
new BaGetRegistrationIndexPageItem
6362
{
6463
RegistrationLeafUrl = _url.GetRegistrationLeafUrl(package.Id, package.Version),
6564
PackageContentUrl = _url.GetPackageDownloadUrl(package.Id, package.Version),

src/BaGet.Core/Search/DependentsResponse.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using Newtonsoft.Json;
2+
using System.Text.Json.Serialization;
33

44
namespace BaGet.Core
55
{
@@ -12,13 +12,13 @@ public class DependentsResponse
1212
/// <summary>
1313
/// The total number of matches, disregarding skip and take.
1414
/// </summary>
15-
[JsonProperty("totalHits")]
15+
[JsonPropertyName("totalHits")]
1616
public long TotalHits { get; set; }
1717

1818
/// <summary>
1919
/// The package IDs matched by the dependent query.
2020
/// </summary>
21-
[JsonProperty("data")]
21+
[JsonPropertyName("data")]
2222
public IReadOnlyList<string> Data { get; set; }
2323
}
2424
}

src/BaGet.Hosting/BaGet.Hosting.csproj

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1212
</ItemGroup>
1313

14-
<ItemGroup>
15-
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="$(MicrosoftAspNetCorePackageVersion)" />
16-
</ItemGroup>
17-
1814
<ItemGroup>
1915
<ProjectReference Include="..\BaGet.Core\BaGet.Core.csproj" />
2016
</ItemGroup>

src/BaGet.Hosting/Controllers/PackageMetadataController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public PackageMetadataController(IPackageMetadataService metadata)
2323

2424
// GET v3/registration/{id}.json
2525
[HttpGet]
26-
public async Task<ActionResult<RegistrationIndexResponse>> RegistrationIndexAsync(string id, CancellationToken cancellationToken)
26+
public async Task<ActionResult<BaGetRegistrationIndexResponse>> RegistrationIndexAsync(string id, CancellationToken cancellationToken)
2727
{
2828
var index = await _metadata.GetRegistrationIndexOrNullAsync(id, cancellationToken);
2929
if (index == null)

src/BaGet.Hosting/Extensions/IEndpointRouteBuilderExtensions.cs

-17
This file was deleted.

src/BaGet.Hosting/Extensions/IServiceCollectionExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static IServiceCollection AddBaGetWebApplication(
1717
.AddControllers()
1818
.AddApplicationPart(typeof(PackageContentController).Assembly)
1919
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
20-
.AddNewtonsoftJson(options =>
20+
.AddJsonOptions(options =>
2121
{
22-
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
22+
options.JsonSerializerOptions.IgnoreNullValues = true;
2323
});
2424

2525
services.AddHttpContextAccessor();

src/BaGet.Protocol/BaGet.Protocol.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
12-
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
1312
<PackageReference Include="NuGet.Frameworks" Version="$(NuGetPackageVersion)" />
1413
<PackageReference Include="NuGet.Versioning" Version="$(NuGetPackageVersion)" />
14+
<PackageReference Include="System.Text.Json" Version="4.7.2" />
1515
</ItemGroup>
1616

1717
</Project>

src/BaGet.Protocol/Catalog/CatalogProcessor.cs

+12-11
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,19 @@ private async Task<bool> ProcessLeafAsync(CatalogLeafItem leafItem, Cancellation
144144
bool success;
145145
try
146146
{
147-
switch (leafItem.Type)
147+
if (leafItem.IsPackageDelete())
148148
{
149-
case CatalogLeafType.PackageDelete:
150-
var packageDelete = await _client.GetPackageDeleteLeafAsync(leafItem.CatalogLeafUrl);
151-
success = await _leafProcessor.ProcessPackageDeleteAsync(packageDelete, cancellationToken);
152-
break;
153-
case CatalogLeafType.PackageDetails:
154-
var packageDetails = await _client.GetPackageDetailsLeafAsync(leafItem.CatalogLeafUrl);
155-
success = await _leafProcessor.ProcessPackageDetailsAsync(packageDetails, cancellationToken);
156-
break;
157-
default:
158-
throw new NotSupportedException($"The catalog leaf type '{leafItem.Type}' is not supported.");
149+
var packageDelete = await _client.GetPackageDeleteLeafAsync(leafItem.CatalogLeafUrl);
150+
success = await _leafProcessor.ProcessPackageDeleteAsync(packageDelete, cancellationToken);
151+
}
152+
else if (leafItem.IsPackageDetails())
153+
{
154+
var packageDetails = await _client.GetPackageDetailsLeafAsync(leafItem.CatalogLeafUrl);
155+
success = await _leafProcessor.ProcessPackageDetailsAsync(packageDetails, cancellationToken);
156+
}
157+
else
158+
{
159+
throw new NotSupportedException($"The catalog leaf type '{leafItem.Type}' is not supported.");
159160
}
160161
}
161162
catch (Exception exception)

0 commit comments

Comments
 (0)