Skip to content

Commit bfbc19c

Browse files
authored
Fix Http.Json serialization performance by using static options (#35040)
1 parent 753fc26 commit bfbc19c

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class HttpContentJsonExtensions
1919
Debug.Assert(content.Headers.ContentType != null);
2020
Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType.CharSet);
2121

22-
return ReadFromJsonAsyncCore(content, type, sourceEncoding, options ?? JsonContent.DefaultSerializerOptions, cancellationToken);
22+
return ReadFromJsonAsyncCore(content, type, sourceEncoding, options, cancellationToken);
2323
}
2424

2525
public static Task<T> ReadFromJsonAsync<T>(this HttpContent content, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
@@ -28,7 +28,7 @@ public static Task<T> ReadFromJsonAsync<T>(this HttpContent content, JsonSeriali
2828
Debug.Assert(content.Headers.ContentType != null);
2929
Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType.CharSet);
3030

31-
return ReadFromJsonAsyncCore<T>(content, sourceEncoding, options ?? JsonContent.DefaultSerializerOptions, cancellationToken);
31+
return ReadFromJsonAsyncCore<T>(content, sourceEncoding, options, cancellationToken);
3232
}
3333

3434
private static async Task<object?> ReadFromJsonAsyncCore(HttpContent content, Type type, Encoding? sourceEncoding, JsonSerializerOptions? options, CancellationToken cancellationToken)
@@ -43,7 +43,7 @@ public static Task<T> ReadFromJsonAsync<T>(this HttpContent content, JsonSeriali
4343

4444
using (contentStream)
4545
{
46-
return await JsonSerializer.DeserializeAsync(contentStream, type, options, cancellationToken).ConfigureAwait(false);
46+
return await JsonSerializer.DeserializeAsync(contentStream, type, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false);
4747
}
4848
}
4949

@@ -59,7 +59,7 @@ private static async Task<T> ReadFromJsonAsyncCore<T>(HttpContent content, Encod
5959

6060
using (contentStream)
6161
{
62-
return await JsonSerializer.DeserializeAsync<T>(contentStream, options, cancellationToken).ConfigureAwait(false);
62+
return await JsonSerializer.DeserializeAsync<T>(contentStream, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false);
6363
}
6464
}
6565

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public sealed partial class JsonContent : HttpContent
2020
private static MediaTypeHeaderValue DefaultMediaType
2121
=> new MediaTypeHeaderValue(JsonMediaType) { CharSet = "utf-8" };
2222

23-
internal static JsonSerializerOptions DefaultSerializerOptions
24-
=> new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
23+
internal static readonly JsonSerializerOptions s_defaultSerializerOptions
24+
= new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
2525

2626
private readonly JsonSerializerOptions? _jsonSerializerOptions;
2727
public Type ObjectType { get; }
@@ -42,7 +42,7 @@ private JsonContent(object? inputValue, Type inputType, MediaTypeHeaderValue? me
4242
Value = inputValue;
4343
ObjectType = inputType;
4444
Headers.ContentType = mediaType ?? DefaultMediaType;
45-
_jsonSerializerOptions = options ?? DefaultSerializerOptions;
45+
_jsonSerializerOptions = options ?? s_defaultSerializerOptions;
4646
}
4747

4848
public static JsonContent Create<T>(T inputValue, MediaTypeHeaderValue? mediaType = null, JsonSerializerOptions? options = null)

0 commit comments

Comments
 (0)