Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonNumberHandling.AllowReadingFromString as a JsonSerializer web default #41539

Merged
merged 3 commits into from Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,8 +19,7 @@ public sealed partial class JsonContent : HttpContent
private static MediaTypeHeaderValue DefaultMediaType
=> new MediaTypeHeaderValue(JsonMediaType) { CharSet = "utf-8" };

internal static readonly JsonSerializerOptions s_defaultSerializerOptions
= new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
internal static readonly JsonSerializerOptions s_defaultSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
layomia marked this conversation as resolved.
Show resolved Hide resolved

private readonly JsonSerializerOptions? _jsonSerializerOptions;
public Type ObjectType { get; }
Expand Down
Expand Up @@ -36,12 +36,7 @@ public string Serialize(JsonSerializerOptions options = null)

internal static class JsonOptions
{
public static readonly JsonSerializerOptions DefaultSerializerOptions
= new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public static readonly JsonSerializerOptions DefaultSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
layomia marked this conversation as resolved.
Show resolved Hide resolved
}

internal class EnsureDefaultOptionsConverter : JsonConverter<EnsureDefaultOptions>
Expand All @@ -68,7 +63,8 @@ public override void Write(Utf8JsonWriter writer, EnsureDefaultOptions value, Js
private static void AssertDefaultOptions(JsonSerializerOptions options)
{
Assert.True(options.PropertyNameCaseInsensitive);
Assert.Equal(JsonNamingPolicy.CamelCase, options.PropertyNamingPolicy);
Assert.Same(JsonNamingPolicy.CamelCase, options.PropertyNamingPolicy);
Assert.Equal(JsonNumberHandling.AllowReadingFromString, options.NumberHandling);
}
}

Expand Down
Expand Up @@ -106,6 +106,7 @@ public JsonSerializerOptions(JsonSerializerDefaults defaults) : this()
{
_propertyNameCaseInsensitive = true;
_jsonPropertyNamingPolicy = JsonNamingPolicy.CamelCase;
_numberHandling = JsonNumberHandling.AllowReadingFromString;
}
else if (defaults != JsonSerializerDefaults.General)
{
Expand Down
Expand Up @@ -533,9 +533,9 @@ public static void DefaultSerializerOptions_General()
public static void PredefinedSerializerOptions_Web()
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
JsonNamingPolicy policy = options.PropertyNamingPolicy;
Assert.True(options.PropertyNameCaseInsensitive);
Assert.Same(JsonNamingPolicy.CamelCase, policy);
Assert.Same(JsonNamingPolicy.CamelCase, options.PropertyNamingPolicy);
Assert.Equal(JsonNumberHandling.AllowReadingFromString, options.NumberHandling);
}

[Theory]
Expand Down