From 8ebd40e058573792f22c9d5daeb2b5236eb4a483 Mon Sep 17 00:00:00 2001 From: David Cantu Date: Tue, 7 Apr 2020 12:32:06 -0700 Subject: [PATCH] Fix Http.Json serialization performance --- .../src/System/Net/Http/Json/HttpContentJsonExtensions.cs | 8 ++++---- .../src/System/Net/Http/Json/JsonContent.cs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs b/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs index 6d1d309760f3a9..c58b64f572b5f0 100644 --- a/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs +++ b/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.cs @@ -19,7 +19,7 @@ public static class HttpContentJsonExtensions Debug.Assert(content.Headers.ContentType != null); Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType.CharSet); - return ReadFromJsonAsyncCore(content, type, sourceEncoding, options ?? JsonContent.DefaultSerializerOptions, cancellationToken); + return ReadFromJsonAsyncCore(content, type, sourceEncoding, options, cancellationToken); } public static Task ReadFromJsonAsync(this HttpContent content, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) @@ -28,7 +28,7 @@ public static Task ReadFromJsonAsync(this HttpContent content, JsonSeriali Debug.Assert(content.Headers.ContentType != null); Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType.CharSet); - return ReadFromJsonAsyncCore(content, sourceEncoding, options ?? JsonContent.DefaultSerializerOptions, cancellationToken); + return ReadFromJsonAsyncCore(content, sourceEncoding, options, cancellationToken); } private static async Task ReadFromJsonAsyncCore(HttpContent content, Type type, Encoding? sourceEncoding, JsonSerializerOptions? options, CancellationToken cancellationToken) @@ -43,7 +43,7 @@ public static Task ReadFromJsonAsync(this HttpContent content, JsonSeriali using (contentStream) { - return await JsonSerializer.DeserializeAsync(contentStream, type, options, cancellationToken).ConfigureAwait(false); + return await JsonSerializer.DeserializeAsync(contentStream, type, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false); } } @@ -59,7 +59,7 @@ private static async Task ReadFromJsonAsyncCore(HttpContent content, Encod using (contentStream) { - return await JsonSerializer.DeserializeAsync(contentStream, options, cancellationToken).ConfigureAwait(false); + return await JsonSerializer.DeserializeAsync(contentStream, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false); } } diff --git a/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/JsonContent.cs b/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/JsonContent.cs index a84d375b137c1f..72bbe069fb669f 100644 --- a/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/JsonContent.cs +++ b/src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/JsonContent.cs @@ -20,8 +20,8 @@ public sealed partial class JsonContent : HttpContent private static MediaTypeHeaderValue DefaultMediaType => new MediaTypeHeaderValue(JsonMediaType) { CharSet = "utf-8" }; - internal static JsonSerializerOptions DefaultSerializerOptions - => new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + internal static readonly JsonSerializerOptions s_defaultSerializerOptions + = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; private readonly JsonSerializerOptions? _jsonSerializerOptions; public Type ObjectType { get; } @@ -42,7 +42,7 @@ private JsonContent(object? inputValue, Type inputType, MediaTypeHeaderValue? me Value = inputValue; ObjectType = inputType; Headers.ContentType = mediaType ?? DefaultMediaType; - _jsonSerializerOptions = options ?? DefaultSerializerOptions; + _jsonSerializerOptions = options ?? s_defaultSerializerOptions; } public static JsonContent Create(T inputValue, MediaTypeHeaderValue? mediaType = null, JsonSerializerOptions? options = null)