Expose JsonSerializerOptions directly in SignalR (#9881)

This commit is contained in:
BrennanConroy 2019-05-02 18:59:18 -07:00 committed by GitHub
parent 8863ebfda1
commit 0748d18a0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 37 deletions

View File

@ -45,7 +45,7 @@ namespace FunctionalTests
{
// we are running the same tests with JSON and MsgPack protocols and having
// consistent casing makes it cleaner to verify results
options.UseCamelCase = false;
options.PayloadSerializerOptions.PropertyNamingPolicy = null;
})
.AddMessagePackProtocol();

View File

@ -6,10 +6,7 @@ namespace Microsoft.AspNetCore.SignalR
public partial class JsonHubProtocolOptions
{
public JsonHubProtocolOptions() { }
public bool AllowTrailingCommas { get { throw null; } set { } }
public bool IgnoreNullValues { get { throw null; } set { } }
public bool UseCamelCase { get { throw null; } set { } }
public bool WriteIndented { get { throw null; } set { } }
public System.Text.Json.Serialization.JsonSerializerOptions PayloadSerializerOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
}
}
namespace Microsoft.AspNetCore.SignalR.Protocol

View File

@ -6,10 +6,7 @@ namespace Microsoft.AspNetCore.SignalR
public partial class JsonHubProtocolOptions
{
public JsonHubProtocolOptions() { }
public bool AllowTrailingCommas { get { throw null; } set { } }
public bool IgnoreNullValues { get { throw null; } set { } }
public bool UseCamelCase { get { throw null; } set { } }
public bool WriteIndented { get { throw null; } set { } }
public System.Text.Json.Serialization.JsonSerializerOptions PayloadSerializerOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
}
}
namespace Microsoft.AspNetCore.SignalR.Protocol

View File

@ -11,30 +11,9 @@ namespace Microsoft.AspNetCore.SignalR
/// </summary>
public class JsonHubProtocolOptions
{
internal readonly JsonSerializerOptions _serializerOptions;
public JsonHubProtocolOptions()
{
_serializerOptions = JsonHubProtocol.CreateDefaultSerializerSettings();
}
public bool IgnoreNullValues { get => _serializerOptions.IgnoreNullValues; set => _serializerOptions.IgnoreNullValues = value; }
public bool WriteIndented { get => _serializerOptions.WriteIndented; set => _serializerOptions.WriteIndented = value; }
public bool AllowTrailingCommas { get => _serializerOptions.AllowTrailingCommas; set => _serializerOptions.AllowTrailingCommas = value; }
public bool UseCamelCase
{
get => _serializerOptions.PropertyNamingPolicy == JsonNamingPolicy.CamelCase;
set
{
if (value)
{
_serializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
}
else
{
_serializerOptions.PropertyNamingPolicy = null;
}
}
}
/// <summary>
/// Gets or sets the settings used to serialize invocation arguments and return values.
/// </summary>
public JsonSerializerOptions PayloadSerializerOptions { get; set; } = JsonHubProtocol.CreateDefaultSerializerSettings();
}
}

View File

@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
/// <param name="options">The options used to initialize the protocol.</param>
public JsonHubProtocol(IOptions<JsonHubProtocolOptions> options)
{
_payloadSerializerOptions = options.Value._serializerOptions;
_payloadSerializerOptions = options.Value.PayloadSerializerOptions;
}
/// <inheritdoc />

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Extensions.Options;
@ -24,8 +25,11 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol
{
var protocolOptions = new JsonHubProtocolOptions()
{
IgnoreNullValues = ignoreNullValues,
UseCamelCase = useCamelCase,
PayloadSerializerOptions = new JsonSerializerOptions()
{
IgnoreNullValues = ignoreNullValues,
PropertyNamingPolicy = useCamelCase ? JsonNamingPolicy.CamelCase : null
}
};
return new JsonHubProtocol(Options.Create(protocolOptions));