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<T> ReadFromJsonAsync<T>(this HttpContent content, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
Debug.Assert(content.Headers.ContentType != null);
Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType.CharSet);
- return ReadFromJsonAsyncCore<T>(content, sourceEncoding, options ?? JsonContent.DefaultSerializerOptions, cancellationToken);
+ return ReadFromJsonAsyncCore<T>(content, sourceEncoding, options, cancellationToken);
}
private static async Task<object?> ReadFromJsonAsyncCore(HttpContent content, Type type, Encoding? sourceEncoding, JsonSerializerOptions? options, CancellationToken cancellationToken)
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);
}
}
using (contentStream)
{
- return await JsonSerializer.DeserializeAsync<T>(contentStream, options, cancellationToken).ConfigureAwait(false);
+ return await JsonSerializer.DeserializeAsync<T>(contentStream, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false);
}
}
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; }
Value = inputValue;
ObjectType = inputType;
Headers.ContentType = mediaType ?? DefaultMediaType;
- _jsonSerializerOptions = options ?? DefaultSerializerOptions;
+ _jsonSerializerOptions = options ?? s_defaultSerializerOptions;
}
public static JsonContent Create<T>(T inputValue, MediaTypeHeaderValue? mediaType = null, JsonSerializerOptions? options = null)