Write default values by default in Json (#23542)
This commit is contained in:
parent
a57a80fcfb
commit
f6bc908a32
|
|
@ -773,7 +773,7 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
|
||||||
WriteIndented = false,
|
WriteIndented = false,
|
||||||
ReadCommentHandling = JsonCommentHandling.Disallow,
|
ReadCommentHandling = JsonCommentHandling.Disallow,
|
||||||
AllowTrailingCommas = false,
|
AllowTrailingCommas = false,
|
||||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
|
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
|
||||||
IgnoreReadOnlyProperties = false,
|
IgnoreReadOnlyProperties = false,
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
PropertyNameCaseInsensitive = true,
|
PropertyNameCaseInsensitive = true,
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol
|
||||||
{
|
{
|
||||||
PayloadSerializerOptions = new JsonSerializerOptions()
|
PayloadSerializerOptions = new JsonSerializerOptions()
|
||||||
{
|
{
|
||||||
IgnoreNullValues = ignoreNullValues,
|
DefaultIgnoreCondition = ignoreNullValues ? System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault : System.Text.Json.Serialization.JsonIgnoreCondition.Never,
|
||||||
PropertyNamingPolicy = useCamelCase ? JsonNamingPolicy.CamelCase : null,
|
PropertyNamingPolicy = useCamelCase ? JsonNamingPolicy.CamelCase : null,
|
||||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -314,6 +314,33 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol
|
||||||
}, streamItemMessage.Item);
|
}, streamItemMessage.Item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DefaultValuesAreWrittenByDefault()
|
||||||
|
{
|
||||||
|
var obj = new CustomObject()
|
||||||
|
{
|
||||||
|
ByteArrProp = new byte[] { 2, 4, 6 },
|
||||||
|
IntProp = default,
|
||||||
|
DoubleProp = 1.1,
|
||||||
|
StringProp = "test",
|
||||||
|
DateTimeProp = default
|
||||||
|
};
|
||||||
|
var expectedOutput = Frame("{\"type\":1,\"invocationId\":\"123\",\"target\":\"Target\",\"arguments\":[{\"stringProp\":\"test\",\"doubleProp\":1.1,\"intProp\":0,\"dateTimeProp\":\"0001-01-01T00:00:00\",\"nullProp\":null,\"byteArrProp\":\"AgQG\"}]}");
|
||||||
|
|
||||||
|
var writer = MemoryBufferWriter.Get();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JsonHubProtocol.WriteMessage(new InvocationMessage("123", "Target", new object[] { obj }), writer);
|
||||||
|
var json = Encoding.UTF8.GetString(writer.ToArray());
|
||||||
|
|
||||||
|
Assert.Equal(expectedOutput, json);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
MemoryBufferWriter.Return(writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static IDictionary<string, MessageSizeTestData> MessageSizeData => new[]
|
public static IDictionary<string, MessageSizeTestData> MessageSizeData => new[]
|
||||||
{
|
{
|
||||||
new MessageSizeTestData("InvocationMessage_WithoutInvocationId", new InvocationMessage("Target", new object[] { 1 }), 45),
|
new MessageSizeTestData("InvocationMessage_WithoutInvocationId", new InvocationMessage("Target", new object[] { 1 }), 45),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue