Skip to content

Commit 0748d18

Browse files
Expose JsonSerializerOptions directly in SignalR (#9881)
1 parent 8863ebf commit 0748d18

File tree

6 files changed

+14
-37
lines changed

6 files changed

+14
-37
lines changed

src/SignalR/clients/ts/FunctionalTests/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void ConfigureServices(IServiceCollection services)
4545
{
4646
// we are running the same tests with JSON and MsgPack protocols and having
4747
// consistent casing makes it cleaner to verify results
48-
options.UseCamelCase = false;
48+
options.PayloadSerializerOptions.PropertyNamingPolicy = null;
4949
})
5050
.AddMessagePackProtocol();
5151

src/SignalR/common/Protocols.Json/ref/Microsoft.AspNetCore.SignalR.Protocols.Json.netcoreapp3.0.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ namespace Microsoft.AspNetCore.SignalR
66
public partial class JsonHubProtocolOptions
77
{
88
public JsonHubProtocolOptions() { }
9-
public bool AllowTrailingCommas { get { throw null; } set { } }
10-
public bool IgnoreNullValues { get { throw null; } set { } }
11-
public bool UseCamelCase { get { throw null; } set { } }
12-
public bool WriteIndented { get { throw null; } set { } }
9+
public System.Text.Json.Serialization.JsonSerializerOptions PayloadSerializerOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
1310
}
1411
}
1512
namespace Microsoft.AspNetCore.SignalR.Protocol

src/SignalR/common/Protocols.Json/ref/Microsoft.AspNetCore.SignalR.Protocols.Json.netstandard2.0.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ namespace Microsoft.AspNetCore.SignalR
66
public partial class JsonHubProtocolOptions
77
{
88
public JsonHubProtocolOptions() { }
9-
public bool AllowTrailingCommas { get { throw null; } set { } }
10-
public bool IgnoreNullValues { get { throw null; } set { } }
11-
public bool UseCamelCase { get { throw null; } set { } }
12-
public bool WriteIndented { get { throw null; } set { } }
9+
public System.Text.Json.Serialization.JsonSerializerOptions PayloadSerializerOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
1310
}
1411
}
1512
namespace Microsoft.AspNetCore.SignalR.Protocol

src/SignalR/common/Protocols.Json/src/JsonHubProtocolOptions.cs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,9 @@ namespace Microsoft.AspNetCore.SignalR
1111
/// </summary>
1212
public class JsonHubProtocolOptions
1313
{
14-
internal readonly JsonSerializerOptions _serializerOptions;
15-
16-
public JsonHubProtocolOptions()
17-
{
18-
_serializerOptions = JsonHubProtocol.CreateDefaultSerializerSettings();
19-
}
20-
21-
public bool IgnoreNullValues { get => _serializerOptions.IgnoreNullValues; set => _serializerOptions.IgnoreNullValues = value; }
22-
public bool WriteIndented { get => _serializerOptions.WriteIndented; set => _serializerOptions.WriteIndented = value; }
23-
public bool AllowTrailingCommas { get => _serializerOptions.AllowTrailingCommas; set => _serializerOptions.AllowTrailingCommas = value; }
24-
public bool UseCamelCase
25-
{
26-
get => _serializerOptions.PropertyNamingPolicy == JsonNamingPolicy.CamelCase;
27-
set
28-
{
29-
if (value)
30-
{
31-
_serializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
32-
}
33-
else
34-
{
35-
_serializerOptions.PropertyNamingPolicy = null;
36-
}
37-
}
38-
}
14+
/// <summary>
15+
/// Gets or sets the settings used to serialize invocation arguments and return values.
16+
/// </summary>
17+
public JsonSerializerOptions PayloadSerializerOptions { get; set; } = JsonHubProtocol.CreateDefaultSerializerSettings();
3918
}
4019
}

src/SignalR/common/Protocols.Json/src/Protocol/JsonHubProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public JsonHubProtocol() : this(Options.Create(new JsonHubProtocolOptions()))
6161
/// <param name="options">The options used to initialize the protocol.</param>
6262
public JsonHubProtocol(IOptions<JsonHubProtocolOptions> options)
6363
{
64-
_payloadSerializerOptions = options.Value._serializerOptions;
64+
_payloadSerializerOptions = options.Value.PayloadSerializerOptions;
6565
}
6666

6767
/// <inheritdoc />

src/SignalR/common/SignalR.Common/test/Internal/Protocol/JsonHubProtocolTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Linq;
99
using System.Text;
10+
using System.Text.Json.Serialization;
1011
using Microsoft.AspNetCore.Internal;
1112
using Microsoft.AspNetCore.SignalR.Protocol;
1213
using Microsoft.Extensions.Options;
@@ -24,8 +25,11 @@ protected override IHubProtocol GetProtocolWithOptions(bool useCamelCase, bool i
2425
{
2526
var protocolOptions = new JsonHubProtocolOptions()
2627
{
27-
IgnoreNullValues = ignoreNullValues,
28-
UseCamelCase = useCamelCase,
28+
PayloadSerializerOptions = new JsonSerializerOptions()
29+
{
30+
IgnoreNullValues = ignoreNullValues,
31+
PropertyNamingPolicy = useCamelCase ? JsonNamingPolicy.CamelCase : null
32+
}
2933
};
3034

3135
return new JsonHubProtocol(Options.Create(protocolOptions));

0 commit comments

Comments
 (0)