diff --git a/src/NuGet.Core/NuGet.Protocol/Model/AutoCompleteModel.cs b/src/NuGet.Core/NuGet.Protocol/Model/AutoCompleteModel.cs new file mode 100644 index 00000000000..1a02756fd90 --- /dev/null +++ b/src/NuGet.Core/NuGet.Protocol/Model/AutoCompleteModel.cs @@ -0,0 +1,13 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Text.Json.Serialization; + +namespace NuGet.Protocol.Model +{ + internal sealed class AutoCompleteModel + { + [JsonPropertyName("data")] + public string[]? Data { get; set; } + } +} diff --git a/src/NuGet.Core/NuGet.Protocol/Resources/AutoCompleteResourceV3.cs b/src/NuGet.Core/NuGet.Protocol/Resources/AutoCompleteResourceV3.cs index 9a34a284623..4dc3003c30e 100644 --- a/src/NuGet.Core/NuGet.Protocol/Resources/AutoCompleteResourceV3.cs +++ b/src/NuGet.Core/NuGet.Protocol/Resources/AutoCompleteResourceV3.cs @@ -8,10 +8,12 @@ using System.Globalization; using System.Linq; using System.Net; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using Newtonsoft.Json.Linq; using NuGet.Protocol.Core.Types; +using NuGet.Protocol.Model; +using NuGet.Protocol.Utility; using NuGet.Versioning; namespace NuGet.Protocol @@ -55,32 +57,29 @@ public override async Task> IdStartsWith( Common.ILogger logger = log ?? Common.NullLogger.Instance; var queryUri = queryUrl.Uri; - var results = await _client.GetJObjectAsync( + AutoCompleteModel results = await _client.ProcessStreamAsync( new HttpSourceRequest(queryUri, logger), + async stream => + { + if (stream == null) + { + return null; + } + + return await JsonSerializer.DeserializeAsync(stream, JsonContext.Default.AutoCompleteModel, token); + }, logger, token); + token.ThrowIfCancellationRequested(); - if (results == null) - { - return Enumerable.Empty(); - } - var data = results.Value("data"); - if (data == null) - { - return Enumerable.Empty(); - } - // Resolve all the objects - var outputs = new List(); - foreach (var result in data) + if (results?.Data == null) { - if (result != null) - { - outputs.Add(result.ToString()); - } + return Enumerable.Empty(); } - return outputs.Where(item => item.StartsWith(packageIdPrefix, StringComparison.OrdinalIgnoreCase)); + return results.Data + .Where(item => item != null && item.StartsWith(packageIdPrefix, StringComparison.OrdinalIgnoreCase)); } public override async Task> VersionStartsWith( diff --git a/src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs b/src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs index 2a181357a86..a8ad9d7462b 100644 --- a/src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs +++ b/src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs @@ -17,6 +17,7 @@ namespace NuGet.Protocol.Utility [JsonSerializable(typeof(HttpFileSystemBasedFindPackageByIdResource.FlatContainerVersionList))] [JsonSerializable(typeof(IReadOnlyList), TypeInfoPropertyName = "VulnerabilityIndex")] [JsonSerializable(typeof(CaseInsensitiveDictionary>), TypeInfoPropertyName = "VulnerabilityPage")] + [JsonSerializable(typeof(AutoCompleteModel))] internal partial class JsonContext : JsonSerializerContext { }