Skip to content

Commit

Permalink
Move s_defaultSerializerOptions to JsonHelpers.
Browse files Browse the repository at this point in the history
This allows for JsonContent to be trimmed in a default Blazor WASM app.
  • Loading branch information
eerhardt committed May 11, 2021
1 parent d4fc640 commit 956965e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static partial class HttpContentJsonExtensions
{
using (Stream contentStream = await GetContentStream(content, sourceEncoding, cancellationToken).ConfigureAwait(false))
{
return await DeserializeAsyncHelper(contentStream, type, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false);
return await DeserializeAsyncHelper(contentStream, type, options ?? JsonHelpers.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Expand All @@ -63,7 +63,7 @@ public static partial class HttpContentJsonExtensions
{
using (Stream contentStream = await GetContentStream(content, sourceEncoding, cancellationToken).ConfigureAwait(false))
{
return await DeserializeAsyncHelper<T>(contentStream, options ?? JsonContent.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false);
return await DeserializeAsyncHelper<T>(contentStream, options ?? JsonHelpers.s_defaultSerializerOptions, cancellationToken).ConfigureAwait(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace System.Net.Http.Json
{
public sealed partial class JsonContent : HttpContent
{
internal static readonly JsonSerializerOptions s_defaultSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);

private readonly JsonSerializerOptions? _jsonSerializerOptions;
[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)]
public Type ObjectType { get; }
Expand All @@ -43,7 +41,7 @@ public sealed partial class JsonContent : HttpContent
Value = inputValue;
ObjectType = inputType;
Headers.ContentType = mediaType ?? JsonHelpers.GetDefaultMediaType();
_jsonSerializerOptions = options ?? s_defaultSerializerOptions;
_jsonSerializerOptions = options ?? JsonHelpers.s_defaultSerializerOptions;
}

[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

namespace System.Net.Http.Json
{
Expand All @@ -13,6 +14,8 @@ internal static class JsonHelpers
internal const DynamicallyAccessedMemberTypes SerializationMemberTypes = DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties;
internal const DynamicallyAccessedMemberTypes DeserializationMemberTypes = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties;

internal static readonly JsonSerializerOptions s_defaultSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);

internal static MediaTypeHeaderValue GetDefaultMediaType() => new("application/json") { CharSet = "utf-8" };

internal static Encoding? GetEncoding(string? charset)
Expand Down

0 comments on commit 956965e

Please sign in to comment.