Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public static class CliHostSearchCacheData
}
try
{
if (cacheObject.Count == 0)
{
return HostSpecificTemplateData.Default;
}

var keys = new HashSet<string>(cacheObject.Select(p => p.Key), StringComparer.OrdinalIgnoreCase);
if (_hostDataPropertyNames.Any(keys.Contains))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Constraints;
using Microsoft.TemplateEngine.Cli.Commands;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using MSBuildProject = Microsoft.Build.Evaluation.Project;

namespace Microsoft.DotNet.Cli.Commands.New.MSBuildEvaluation;
Expand Down Expand Up @@ -69,10 +70,10 @@ public TemplateConstraintResult Evaluate(string? args)
}

_logger.LogDebug("Configuration: '{0}'", args);
JToken? token;
JsonNode? token;
try
{
token = JToken.Parse(args!);
token = JsonNode.Parse(args!);
}
catch (Exception e)
{
Expand All @@ -82,9 +83,9 @@ public TemplateConstraintResult Evaluate(string? args)

string configuredCapabiltiesExpression;

if (token.Type == JTokenType.String)
if (token is JsonValue v && v.GetValueKind() == JsonValueKind.String)
{
string? configuredCapability = token.Value<string>();
string? configuredCapability = token.GetValue<string>();
if (string.IsNullOrWhiteSpace(configuredCapability))
{
_logger.LogDebug("Invalid configuration: '{0}', reason: arguments should not contain empty values.", args);
Expand Down
17 changes: 2 additions & 15 deletions src/Cli/dotnet/Installer/Windows/InstallMessageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable disable

using Newtonsoft.Json;
using System.Text.Json;

namespace Microsoft.DotNet.Cli.Installer.Windows;

Expand All @@ -12,18 +12,13 @@ namespace Microsoft.DotNet.Cli.Installer.Windows;
/// </summary>
internal abstract class InstallMessageBase
{
/// <summary>
/// Default serialization settings for a message.
/// </summary>
protected static JsonSerializerSettings DefaultSerializerSettings;

/// <summary>
/// Serializes the message to a JSON string.
/// </summary>
/// <returns>The serialized message.</returns>
public override string ToString()
{
return JsonConvert.SerializeObject(this, DefaultSerializerSettings);
return JsonSerializer.Serialize(this, GetType(), InstallerJsonSerializerContext.Default);
}

/// <summary>
Expand All @@ -34,12 +29,4 @@ public byte[] ToByteArray()
{
return Encoding.UTF8.GetBytes(ToString());
}

static InstallMessageBase()
{
DefaultSerializerSettings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
}
}
5 changes: 3 additions & 2 deletions src/Cli/dotnet/Installer/Windows/InstallRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable disable

using Newtonsoft.Json;
using System.Text.Json;

namespace Microsoft.DotNet.Cli.Installer.Windows;

Expand Down Expand Up @@ -154,6 +154,7 @@ public string WorkloadSetVersion
public static InstallRequestMessage Create(byte[] bytes)
{
string json = Encoding.UTF8.GetString(bytes);
return JsonConvert.DeserializeObject<InstallRequestMessage>(json, DefaultSerializerSettings);
return JsonSerializer.Deserialize(json, InstallerJsonSerializerContext.Default.InstallRequestMessage)
?? throw new JsonException("The install request message payload deserialized to null.");
}
}
5 changes: 3 additions & 2 deletions src/Cli/dotnet/Installer/Windows/InstallResponseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable disable

using Newtonsoft.Json;
using System.Text.Json;
using static Microsoft.Win32.Msi.Error;

namespace Microsoft.DotNet.Cli.Installer.Windows;
Expand Down Expand Up @@ -60,7 +60,8 @@ public static InstallResponseMessage Create(byte[] bytes)
{
string json = Encoding.UTF8.GetString(bytes);

return JsonConvert.DeserializeObject<InstallResponseMessage>(json, DefaultSerializerSettings);
return JsonSerializer.Deserialize(json, InstallerJsonSerializerContext.Default.InstallResponseMessage)
?? throw new JsonException("The install response message payload deserialized to null.");
}

public static InstallResponseMessage Create(Exception e)
Expand Down
12 changes: 12 additions & 0 deletions src/Cli/dotnet/Installer/Windows/InstallerJsonSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Serialization;

namespace Microsoft.DotNet.Cli.Installer.Windows;

[JsonSerializable(typeof(InstallRequestMessage))]
[JsonSerializable(typeof(InstallResponseMessage))]
[JsonSerializable(typeof(MsiManifest))]
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
internal partial class InstallerJsonSerializerContext : JsonSerializerContext;
6 changes: 3 additions & 3 deletions src/Cli/dotnet/Installer/Windows/MsiPackageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.DotNet.Cli.Commands.Workload;
using Microsoft.DotNet.Cli.Installer.Windows.Security;

using Newtonsoft.Json;
using System.Text.Json;

namespace Microsoft.DotNet.Cli.Installer.Windows;

Expand Down Expand Up @@ -73,7 +73,7 @@ public void CachePayload(string packageId, string packageVersion, string manifes

// We cannot assume that the MSI adjacent to the manifest is the one to cache. We'll trust
// the manifest to provide the MSI filename.
MsiManifest? msiManifest = JsonConvert.DeserializeObject<MsiManifest>(File.ReadAllText(manifestPath));
MsiManifest? msiManifest = JsonSerializer.Deserialize(File.ReadAllText(manifestPath), InstallerJsonSerializerContext.Default.MsiManifest);
// Only use the filename+extension of the payload property in case the manifest has been altered.
string msiPath = Path.Combine(Path.GetDirectoryName(manifestPath)!, Path.GetFileName(msiManifest?.Payload ?? string.Empty));

Expand Down Expand Up @@ -146,7 +146,7 @@ public bool TryGetMsiPathFromPackageData(string packageDataPath, [NotNullWhen(tr

// The msi.json manifest contains the name of the actual MSI. The filename does not necessarily match the package
// ID as it may have been shortened to support VS caching.
MsiManifest? msiManifest = JsonConvert.DeserializeObject<MsiManifest>(File.ReadAllText(manifestPath));
MsiManifest? msiManifest = JsonSerializer.Deserialize(File.ReadAllText(manifestPath), InstallerJsonSerializerContext.Default.MsiManifest);
string possibleMsiPath = Path.Combine(Path.GetDirectoryName(manifestPath)!, msiManifest?.Payload ?? string.Empty);

if (!File.Exists(possibleMsiPath))
Expand Down
4 changes: 2 additions & 2 deletions src/Cli/dotnet/Installer/Windows/MsiPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable disable

using Newtonsoft.Json;
using System.Text.Json;

namespace Microsoft.DotNet.Cli.Installer.Windows;

Expand Down Expand Up @@ -68,7 +68,7 @@ public MsiManifest Manifest
{
get
{
_manifest ??= JsonConvert.DeserializeObject<MsiManifest>(File.ReadAllText(ManifestPath));
_manifest ??= JsonSerializer.Deserialize(File.ReadAllText(ManifestPath), InstallerJsonSerializerContext.Default.MsiManifest);

return _manifest;
}
Expand Down
11 changes: 4 additions & 7 deletions src/Cli/dotnet/Installer/Windows/RelatedProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

#nullable disable

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json.Serialization;

namespace Microsoft.DotNet.Cli.Installer.Windows;

/// <summary>
/// Describes a single row from the MSI <see href="https://docs.microsoft.com/en-us/windows/win32/msi/upgrade-table">Upgrade</see>
/// Describes a single row from the MSI <see href="https://docs.microsoft.com/en-us/windows/win32/msi/upgrade-table">Upgrade</see>
/// table.
/// </summary>
internal class RelatedProduct
Expand All @@ -26,7 +25,7 @@ public UpgradeAttributes Attributes
}

/// <summary>
/// A comma separated list of languages identifiers (LANGID) that can be detected. When empty, all languages can be
/// A comma separated list of languages identifiers (LANGID) that can be detected. When empty, all languages can be
/// detected. If msidbUpgradeAttributesLanguagesExclusive is set, the list becomes exclusive.
/// </summary>
public string Language
Expand All @@ -47,7 +46,7 @@ public IEnumerable<int> Languages
}

/// <summary>
/// The upgrade code of the related product.
/// The upgrade code of the related product.
/// </summary>
public string UpgradeCode
{
Expand All @@ -58,7 +57,6 @@ public string UpgradeCode
/// <summary>
/// Upper boundary of the range of product versions detected by the FindRelatedProducts action.
/// </summary>
[JsonConverter(typeof(VersionConverter))]
public Version VersionMax
{
get;
Expand All @@ -68,7 +66,6 @@ public Version VersionMax
/// <summary>
/// The lower boundary of the range of product versions detected by the FindRelatedProducts action.
/// </summary>
[JsonConverter(typeof(VersionConverter))]
public Version VersionMin
{
get;
Expand Down
11 changes: 7 additions & 4 deletions src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.EnvironmentAbstractions;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using NuGet.Configuration;
using NuGet.Packaging;
using NuGet.Packaging.Core;
Expand All @@ -18,6 +19,8 @@ namespace Microsoft.DotNet.Cli.ToolPackage;

internal abstract class ToolPackageDownloaderBase : IToolPackageDownloader
{
private static readonly JsonSerializerOptions s_writeIndentedOptions = new() { WriteIndented = true };

private readonly IToolPackageStore _toolPackageStore;

protected readonly IFileSystem _fileSystem;
Expand Down Expand Up @@ -384,11 +387,11 @@ ToolPackageInstance toolPackageInstance
{
string existingJson = _fileSystem.File.ReadAllText(runtimeConfigFilePath);

var jsonObject = JObject.Parse(existingJson);
if (jsonObject["runtimeOptions"] is JObject runtimeOptions)
var jsonObject = JsonNode.Parse(existingJson)!.AsObject();
if (jsonObject["runtimeOptions"] is JsonObject runtimeOptions)
{
runtimeOptions["rollForward"] = "Major";
string updateJson = jsonObject.ToString();
string updateJson = jsonObject.ToJsonString(s_writeIndentedOptions);
_fileSystem.File.WriteAllText(runtimeConfigFilePath, updateJson);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Cli/dotnet/dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" />
<PackageReference Include="Microsoft.VisualStudio.SolutionPersistence" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="NuGet.CommandLine.XPlat" />
<PackageReference Include="Microsoft.Build" />
<PackageReference Include="Microsoft.NET.HostModel" />
Expand Down
6 changes: 3 additions & 3 deletions src/Tasks/sdk-tasks/GetRuntimePackRids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable disable

using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;

namespace Microsoft.DotNet.Build.Tasks
{
Expand All @@ -19,8 +19,8 @@ public override bool Execute()
{
string runtimeJsonPath = Path.Combine(MetapackagePath, "runtime.json");
string runtimeJsonContents = File.ReadAllText(runtimeJsonPath);
var runtimeJsonRoot = JObject.Parse(runtimeJsonContents);
string [] runtimeIdentifiers = ((JObject)runtimeJsonRoot["runtimes"]).Properties().Select(p => p.Name).ToArray();
var runtimeJsonRoot = JsonNode.Parse(runtimeJsonContents)!.AsObject();
string [] runtimeIdentifiers = runtimeJsonRoot["runtimes"]!.AsObject().Select(p => p.Key).ToArray();
AvailableRuntimePackRuntimeIdentifiers = runtimeIdentifiers.Select(rid => new TaskItem(rid)).ToArray();

return true;
Expand Down
8 changes: 6 additions & 2 deletions src/Tasks/sdk-tasks/ProcessRuntimeAnalyzerVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.DotNet.Build.Tasks;

Expand Down Expand Up @@ -58,15 +59,18 @@ public override bool Execute()
}

Directory.CreateDirectory(Path.GetDirectoryName(MetadataFilePath)!);
File.WriteAllText(path: MetadataFilePath!, JsonSerializer.Serialize(metadata));
File.WriteAllText(path: MetadataFilePath!, JsonSerializer.Serialize(metadata, SdkTasksJsonSerializerContext.Default.DictionaryStringMetadataEntry));

Outputs = Inputs;
return true;
}

private sealed class MetadataEntry(string version)
internal sealed class MetadataEntry(string version)
{
public string Version { get; } = version;
public List<string> Files { get; } = [];
}
}

[JsonSerializable(typeof(Dictionary<string, ProcessRuntimeAnalyzerVersions.MetadataEntry>))]
internal partial class SdkTasksJsonSerializerContext : JsonSerializerContext;
Loading
Loading