File: Commands\Tool\List\ToolListJsonHelper.cs
Web Access
Project: src\src\sdk\src\Cli\dotnet\dotnet.csproj (dotnet)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

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

namespace Microsoft.DotNet.Cli.Commands.Tool.List;

internal sealed class VersionedDataContract<TContract>
{
    /// <summary>
    /// The version of the JSON format for dotnet tool list.
    /// </summary>
    [JsonPropertyName("version")]
    public int Version { get; init; } = 1;

    [JsonPropertyName("data")]
    public required TContract Data { get; init; }
}

internal class ToolListJsonContract
{
    [JsonPropertyName("packageId")]
    public required string PackageId { get; init; }

    [JsonPropertyName("version")]
    public required string Version { get; init; }

    [JsonPropertyName("commands")]
    public required string[] Commands { get; init; }
}

internal sealed class LocalToolListJsonContract : ToolListJsonContract
{
    [JsonPropertyName("manifest")]
    public required string Manifest { get; init; }
}

internal static class JsonHelper
{
    public static readonly ToolListJsonSerializerContext JsonContext = new(new JsonSerializerOptions()
    {
        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
    });
}

[JsonSerializable(typeof(VersionedDataContract<ToolListJsonContract[]>))]
[JsonSerializable(typeof(VersionedDataContract<LocalToolListJsonContract[]>))]
internal partial class ToolListJsonSerializerContext : JsonSerializerContext;