Skip to content

Commit 045f9d2

Browse files
Move JsonSerializerOptions initialization logic to a shared helper. (#89729)
1 parent 0203dcb commit 045f9d2

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.AsyncEnumerable.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ public static partial class HttpContentJsonExtensions
6767
JsonSerializerOptions? options,
6868
CancellationToken cancellationToken)
6969
{
70-
options ??= JsonSerializerOptions.Default;
71-
options.MakeReadOnly();
72-
73-
var jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfo(typeof(TValue));
74-
70+
var jsonTypeInfo = (JsonTypeInfo<TValue>)JsonHelpers.GetJsonTypeInfo(typeof(TValue), options);
7571
return ReadFromJsonAsAsyncEnumerableCore(content, jsonTypeInfo, cancellationToken);
7672
}
7773

src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/JsonContent.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private JsonContent(
3535
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
3636
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
3737
public static JsonContent Create<T>(T inputValue, MediaTypeHeaderValue? mediaType = null, JsonSerializerOptions? options = null)
38-
=> Create(inputValue, GetJsonTypeInfo(typeof(T), options), mediaType);
38+
=> Create(inputValue, JsonHelpers.GetJsonTypeInfo(typeof(T), options), mediaType);
3939

4040
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
4141
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
@@ -44,7 +44,7 @@ public static JsonContent Create(object? inputValue, Type inputType, MediaTypeHe
4444
ThrowHelper.ThrowIfNull(inputType);
4545
EnsureTypeCompatibility(inputValue, inputType);
4646

47-
return new JsonContent(inputValue, GetJsonTypeInfo(inputType, options), mediaType);
47+
return new JsonContent(inputValue, JsonHelpers.GetJsonTypeInfo(inputType, options), mediaType);
4848
}
4949

5050
public static JsonContent Create<T>(T? inputValue, JsonTypeInfo<T> jsonTypeInfo,
@@ -136,20 +136,6 @@ private async Task SerializeToStreamAsyncCore(Stream targetStream, bool async, C
136136
}
137137
}
138138

139-
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
140-
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
141-
private static JsonTypeInfo GetJsonTypeInfo(Type inputType, JsonSerializerOptions? options)
142-
{
143-
Debug.Assert(inputType is not null);
144-
145-
// Ensure the options supports the call to GetTypeInfo
146-
options ??= JsonHelpers.s_defaultSerializerOptions;
147-
options.TypeInfoResolver ??= JsonSerializerOptions.Default.TypeInfoResolver;
148-
options.MakeReadOnly();
149-
150-
return options.GetTypeInfo(inputType);
151-
}
152-
153139
private static void EnsureTypeCompatibility(object? inputValue, Type inputType)
154140
{
155141
if (inputValue is not null && !inputType.IsAssignableFrom(inputValue.GetType()))

src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/JsonHelpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,33 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Net.Http.Headers;
67
using System.Text;
78
using System.Text.Json;
9+
using System.Text.Json.Serialization.Metadata;
810

911
namespace System.Net.Http.Json
1012
{
1113
internal static class JsonHelpers
1214
{
1315
internal static readonly JsonSerializerOptions s_defaultSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
1416

17+
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
18+
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
19+
internal static JsonTypeInfo GetJsonTypeInfo(Type type, JsonSerializerOptions? options)
20+
{
21+
Debug.Assert(type is not null);
22+
23+
// Resolves JsonTypeInfo metadata using the appropriate JsonSerializerOptions configuration,
24+
// following the semantics of the JsonSerializer reflection methods.
25+
options ??= s_defaultSerializerOptions;
26+
options.TypeInfoResolver ??= JsonSerializerOptions.Default.TypeInfoResolver;
27+
options.MakeReadOnly();
28+
29+
return options.GetTypeInfo(type);
30+
}
31+
1532
internal static MediaTypeHeaderValue GetDefaultMediaType() => new("application/json") { CharSet = "utf-8" };
1633

1734
internal static Encoding? GetEncoding(HttpContent content)

src/libraries/System.Net.Http.Json/tests/FunctionalTests/HttpContentJsonExtensionsTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,33 @@ await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
133133
server => server.HandleRequestAsync(headers: _headers, content: "null"));
134134
}
135135

136+
[Fact]
137+
public async Task HttpContentAsAsyncEnumerableHonorsWebDefaults()
138+
{
139+
await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
140+
async (handler, uri) =>
141+
{
142+
using (HttpClient client = new HttpClient(handler))
143+
{
144+
var request = new HttpRequestMessage(HttpMethod.Get, uri);
145+
HttpResponseMessage response = await client.SendAsync(request);
146+
int count = 0;
147+
await foreach (Person? per in response.Content.ReadFromJsonAsAsyncEnumerable<Person>())
148+
{
149+
Assert.NotNull(per);
150+
Assert.NotNull(per.Name);
151+
count++;
152+
}
153+
Assert.Equal(People.PeopleCount, count);
154+
}
155+
},
156+
async server =>
157+
{
158+
string jsonResponse = JsonSerializer.Serialize(People.WomenOfProgramming, JsonOptions.DefaultSerializerOptions);
159+
await server.HandleRequestAsync(headers: _headers, content: jsonResponse);
160+
});
161+
}
162+
136163
[Fact]
137164
public async Task TestReadFromJsonAsAsyncEnumerableNoMessageBodyAsync()
138165
{

0 commit comments

Comments
 (0)