-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFluentJsonTypedDeserialization.cs
More file actions
171 lines (158 loc) · 8.9 KB
/
Copy pathFluentJsonTypedDeserialization.cs
File metadata and controls
171 lines (158 loc) · 8.9 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
namespace FluentHttpClient;
/// <summary>
/// Fluent extension methods for deserializing JSON from an <see cref="HttpRequestMessage"/> instance.
/// </summary>
#if NET7_0_OR_GREATER
[RequiresDynamicCode("Uses reflection-based JSON deserialization. For Native AOT, use the overloads that accept JsonTypeInfo<T>.")]
#endif
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Uses reflection-based JSON deserialization which is not trimming-safe. For trimmed/AOT apps, use the overloads that accept JsonTypeInfo<T>.")]
#endif
public static partial class FluentJsonTypedDeserialization
{
/// <summary>
/// Reads the JSON content of the response and deserializes it to <typeparamref name="T"/>.
/// </summary>
/// <remarks>
/// <para>
/// Returns <c>default(T?)</c> when the response content is null or empty. For reference types, this is <c>null</c>.
/// For nullable value types (e.g., <c>int?</c>), this is also <c>null</c>. This behavior differs from
/// <see cref="FluentJsonDeserialization.ReadJsonDocumentAsync(HttpResponseMessage)"/> which explicitly
/// returns <c>null</c> for empty content.
/// </para>
/// </remarks>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="response">The HTTP response whose content will be read.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static Task<T?> ReadJsonAsync<T>(this HttpResponseMessage response)
{
return response.ReadJsonAsync<T>(
FluentJsonSerializer.DefaultJsonSerializerOptions,
CancellationToken.None);
}
/// <summary>
/// Reads the JSON content of the response and deserializes it to <typeparamref name="T"/>,
/// using the specified serializer options.
/// </summary>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="response">The HTTP response whose content will be read.</param>
/// <param name="options">Options to control the deserialization behavior.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static Task<T?> ReadJsonAsync<T>(
this HttpResponseMessage response,
JsonSerializerOptions? options)
{
return response.ReadJsonAsync<T>(options, CancellationToken.None);
}
/// <summary>
/// Reads the JSON content of the response and deserializes it to <typeparamref name="T"/>,
/// observing the provided cancellation token.
/// </summary>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="response">The HTTP response whose content will be read.</param>
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static Task<T?> ReadJsonAsync<T>(
this HttpResponseMessage response,
CancellationToken cancellationToken)
{
return response.ReadJsonAsync<T>(
FluentJsonSerializer.DefaultJsonSerializerOptions,
cancellationToken);
}
/// <summary>
/// Reads the JSON content of the response and deserializes it to <typeparamref name="T"/>,
/// using the specified serializer options and cancellation token.
/// </summary>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="response">The HTTP response whose content will be read.</param>
/// <param name="options">Options to control the deserialization behavior.</param>
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static async Task<T?> ReadJsonAsync<T>(
this HttpResponseMessage response,
JsonSerializerOptions? options,
CancellationToken cancellationToken)
{
// NOTE: HttpResponseMessage.Content is never null on modern TFMs, but can be on older platforms.
// These checks exist for cross-target safety and are not hit in current test runs.
if (response.Content is null)
return default;
options ??= FluentJsonSerializer.DefaultJsonSerializerOptions;
#if NET5_0_OR_GREATER
var json = await response.Content
.ReadAsStringAsync(cancellationToken)
.ConfigureAwait(false);
#else
var json = await response.Content
.ReadAsStringAsync()
.ConfigureAwait(false);
#endif
return cancellationToken.IsCancellationRequested
? throw new OperationCanceledException(cancellationToken)
: JsonSerializer.Deserialize<T>(json, options);
}
/// <summary>
/// Awaits the HTTP response task, then reads and deserializes the JSON content to <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="responseTask">A task that produces the HTTP response whose content will be read.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static Task<T?> ReadJsonAsync<T>(this Task<HttpResponseMessage> responseTask)
{
return responseTask.ReadJsonAsync<T>(
FluentJsonSerializer.DefaultJsonSerializerOptions,
CancellationToken.None);
}
/// <summary>
/// Awaits the HTTP response task, then reads and deserializes the JSON content to <typeparamref name="T"/>,
/// using the specified serializer options.
/// </summary>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="responseTask">A task that produces the HTTP response whose content will be read.</param>
/// <param name="options">Options to control the deserialization behavior.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static Task<T?> ReadJsonAsync<T>(
this Task<HttpResponseMessage> responseTask,
JsonSerializerOptions? options)
{
return responseTask.ReadJsonAsync<T>(options, CancellationToken.None);
}
/// <summary>
/// Awaits the HTTP response task, then reads and deserializes the JSON content to <typeparamref name="T"/>,
/// observing the provided cancellation token.
/// </summary>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="responseTask">A task that produces the HTTP response whose content will be read.</param>
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static Task<T?> ReadJsonAsync<T>(
this Task<HttpResponseMessage> responseTask,
CancellationToken cancellationToken)
{
return responseTask.ReadJsonAsync<T>(
FluentJsonSerializer.DefaultJsonSerializerOptions,
cancellationToken);
}
/// <summary>
/// Awaits the HTTP response task, then reads and deserializes the JSON content to <typeparamref name="T"/>,
/// using the specified serializer options and cancellation token.
/// </summary>
/// <typeparam name="T">The type to deserialize the JSON content into.</typeparam>
/// <param name="responseTask">A task that produces the HTTP response whose content will be read.</param>
/// <param name="options">Options to control the deserialization behavior.</param>
/// <param name="cancellationToken">A cancellation token to observe while waiting for the task to complete.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the deserialized object, or default(T?) if the content is empty.</returns>
public static async Task<T?> ReadJsonAsync<T>(
this Task<HttpResponseMessage> responseTask,
JsonSerializerOptions? options,
CancellationToken cancellationToken)
{
var response = await responseTask.ConfigureAwait(false);
return await response
.ReadJsonAsync<T>(options, cancellationToken)
.ConfigureAwait(false);
}
}