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);
private readonly JsonSerializerOptions? _jsonSerializerOptions;
public Type ObjectType { get; }
{
public class HttpClientJsonExtensionsTests
{
- [Fact]
- public async Task TestGetFromJsonAsync()
+ [Theory]
+ [MemberData(nameof(ReadFromJsonTestData))]
+ public async Task TestGetFromJsonAsync(string json)
{
- string json = Person.Create().Serialize();
HttpHeaderData header = new HttpHeaderData("Content-Type", "application/json");
List<HttpHeaderData> headers = new List<HttpHeaderData> { header };
server => server.HandleRequestAsync(content: json, headers: headers));
}
+ public static IEnumerable<object[]> ReadFromJsonTestData()
+ {
+ Person per = Person.Create();
+ yield return new object[] { per.Serialize() };
+ yield return new object[] { per.SerializeWithNumbersAsStrings() };
+ }
+
[Fact]
public async Task TestGetFromJsonAsyncUnsuccessfulResponseAsync()
{
async server => {
HttpRequestData request = await server.HandleRequestAsync();
ValidateRequest(request);
- Person obj = JsonSerializer.Deserialize<Person>(request.Body, JsonOptions.DefaultSerializerOptions);
+
+ byte[] json = request.Body;
+
+ Person obj = JsonSerializer.Deserialize<Person>(json, JsonOptions.DefaultSerializerOptions);
+ obj.Validate();
+
+ // Assert numbers are not written as strings - JsonException would be thrown here if written as strings.
+ obj = JsonSerializer.Deserialize<Person>(json, JsonOptions.DefaultSerializerOptions_StrictNumberHandling);
obj.Validate();
});
}
AssertExtensions.Throws<ArgumentNullException>("content", () => content.ReadFromJsonAsync(typeof(Person)));
}
- [Fact]
- public async Task HttpContentGetThenReadFromJsonAsync()
+ [Theory]
+ [MemberData(nameof(ReadFromJsonTestData))]
+ public async Task HttpContentGetThenReadFromJsonAsync(string json)
{
await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
async (handler, uri) =>
per.Validate();
}
},
- server => server.HandleRequestAsync(headers: _headers, content: Person.Create().Serialize()));
+ server => server.HandleRequestAsync(headers: _headers, content: json));
+ }
+
+ public static IEnumerable<object[]> ReadFromJsonTestData()
+ {
+ Person per = Person.Create();
+ yield return new object[] { per.Serialize() };
+ yield return new object[] { per.SerializeWithNumbersAsStrings() };
}
[Fact]
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using Xunit;
{
return JsonSerializer.Serialize(this, options);
}
+
+ public string SerializeWithNumbersAsStrings(JsonSerializerOptions options = null)
+ {
+ options ??= new JsonSerializerOptions();
+ options.NumberHandling = options.NumberHandling | JsonNumberHandling.WriteAsString;
+ return JsonSerializer.Serialize(this, options);
+ }
}
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);
+
+ public static readonly JsonSerializerOptions DefaultSerializerOptions_StrictNumberHandling = new JsonSerializerOptions(DefaultSerializerOptions)
+ {
+ NumberHandling = JsonNumberHandling.Strict
+ };
}
internal class EnsureDefaultOptionsConverter : JsonConverter<EnsureDefaultOptions>
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);
}
}
{
_propertyNameCaseInsensitive = true;
_jsonPropertyNamingPolicy = JsonNamingPolicy.CamelCase;
+ _numberHandling = JsonNumberHandling.AllowReadingFromString;
}
else if (defaults != JsonSerializerDefaults.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]