Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> ReadFromJsonAsync<T>(this HttpContent content, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
Expand All @@ -28,7 +28,7 @@ public static Task<T> ReadFromJsonAsync<T>(this HttpContent content, JsonSeriali
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)
Expand All @@ -43,7 +43,7 @@ public static Task<T> ReadFromJsonAsync<T>(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);
}
}

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

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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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>(T inputValue, MediaTypeHeaderValue? mediaType = null, JsonSerializerOptions? options = null)
Expand Down