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 // we are running the same tests with JSON and MsgPack protocols and having
// consistent casing makes it cleaner to verify results // consistent casing makes it cleaner to verify results
options.UseCamelCase = false; options.PayloadSerializerOptions.PropertyNamingPolicy = null;
}) })
.AddMessagePackProtocol(); .AddMessagePackProtocol();

View File

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

View File

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

View File

@ -11,30 +11,9 @@ namespace Microsoft.AspNetCore.SignalR
/// </summary> /// </summary>
public class JsonHubProtocolOptions public class JsonHubProtocolOptions
{ {
internal readonly JsonSerializerOptions _serializerOptions; /// <summary>
/// Gets or sets the settings used to serialize invocation arguments and return values.
public JsonHubProtocolOptions() /// </summary>
{ public JsonSerializerOptions PayloadSerializerOptions { get; set; } = JsonHubProtocol.CreateDefaultSerializerSettings();
_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;
}
}
}
} }
} }

View File

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

View File

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