-
Notifications
You must be signed in to change notification settings - Fork 967
Expand file tree
/
Copy pathAtsConvertable.cs
More file actions
125 lines (110 loc) · 4.33 KB
/
Copy pathAtsConvertable.cs
File metadata and controls
125 lines (110 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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;
using System.Text.Json.Nodes;
namespace Aspire.Hosting.Ats;
/// <summary>
/// Represents an object that has custom conversion logic for crossing ATS boundaries.
/// </summary>
public interface IAtsConvertable
{
/// <summary>
/// Deserializes the given JSON object into the implementing class's type.
/// </summary>
/// <param name="jsonObj">The JSON document to convert.</param>
/// <returns>The deserialized object.</returns>
static abstract object? Deserialize(JsonObject jsonObj);
/// <summary>
/// Serializes the given object to JSON.
/// </summary>
/// <param name="value">The value to serialize.</param>
/// <returns>A JSON object matching the given value.</returns>
static abstract JsonNode? Serialize(object value);
}
/// <summary>
/// Represents an object that is convertable across language boundaries through the ATS system.
/// </summary>
/// <remarks>
/// This implementation of <see cref="IAtsConvertable"/> allows for completely generic objects to be sent
/// across language boundaries.
/// <example>
///
/// <code>
/// // User-defined custom TypeScript object
/// {
/// route: "aspire.dev",
/// match: "http",
/// users: ["chris", "dave", "maddy"]
/// }
/// </code>
/// </example>
/// The above object will get serialized into the <see cref="Object"/> property as a <see cref="Dictionary{TKey, TValue}"/>.
/// </remarks>
/// <ats-summary>An object that supports serialization of custom properties.</ats-summary>
[AspireDto]
[AspireExport]
public class CustomAtsObjectDto : IAtsConvertable
{
/// <summary>
/// Contains the result of deserialization.
/// </summary>
[AspireExportIgnore]
internal Dictionary<string, object?>? Object { get; set; }
/// <summary>
/// Deserializes a <see cref="JsonObject"/> into a Dictionary. A new <see cref="CustomAtsObjectDto"/> will be returned
/// with the results of the deserialization set to the object's <see cref="Object"/> property.
/// </summary>
/// <param name="jsonObj">The JSON value to deserialize.</param>
/// <returns>A new <see cref="CustomAtsObjectDto"/> containing the deserialized <paramref name="jsonObj"/>.</returns>
/// <exception cref="NotSupportedException">Thrown if the <paramref name="jsonObj"/> contains an unsupported type.</exception>
public static object? Deserialize(JsonObject jsonObj)
{
if (jsonObj is null)
{
return null;
}
return new CustomAtsObjectDto()
{
Object = jsonObj.ToDictionary(kvp => kvp.Key, kvp => ConvertJsonNode(kvp.Value))
};
static object? ConvertJsonNode(JsonNode? node)
{
return node switch
{
null => null,
JsonValue value => ConvertJsonValue(value),
JsonObject obj => obj.ToDictionary(
kvp => kvp.Key,
kvp => ConvertJsonNode(kvp.Value)),
JsonArray array => array
.Select(ConvertJsonNode)
.ToList(),
_ => throw new NotSupportedException(
$"Unsupported JsonNode type: {node.GetType().FullName}")
};
}
static object? ConvertJsonValue(JsonValue value)
{
var element = value.GetValue<JsonElement>();
return value.GetValueKind() switch
{
JsonValueKind.Null => null,
JsonValueKind.String => element.GetString(),
JsonValueKind.True => true,
JsonValueKind.False => false,
JsonValueKind.Number when element.TryGetInt64(out var longValue) => longValue,
JsonValueKind.Number => element.GetDouble(),
_ => throw new NotSupportedException($"Unsupported JSON value kind '{value.GetValueKind()}'.")
};
}
}
/// <summary>
/// Forwards serialization to the application's default <see cref="JsonSerializer"/>.
/// </summary>
/// <param name="value">The value to serialize.</param>
/// <returns>The serialized JSON.</returns>
public static JsonNode? Serialize(object value)
{
return JsonSerializer.Serialize(value);
}
}