Skip to content

Commit

Permalink
SignalR now uses System.Text.Json (#14902)
Browse files Browse the repository at this point in the history
* SignalR now uses System.Text.Json

Starting in 3.0, SignalR no longer defaults to using Newtonsoft.Json

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#systemtextjson-is-the-default-protocol

* Address PR feedback

* Documentation -> documentation

* Apply suggestions from code review

Co-Authored-By: Scott Addie <10702007+scottaddie@users.noreply.github.com>

* Update configuration.md (#14958)

* Update configuration.md

* Update configuration.md
  • Loading branch information
halter73 authored and Rick-Anderson committed Oct 10, 2019
1 parent 438b739 commit a06a747
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions aspnetcore/migration/22-to-30.md
Expand Up @@ -1062,7 +1062,7 @@ In `Startup.ConfigureServices`, call `AddJsonProtocol` to set serializer options
services.AddSignalR(...)
.AddJsonProtocol(options =>
{
options.WriteIndented = false;
options.PayloadSerializerOptions.WriteIndented = false;
})
```

Expand All @@ -1073,7 +1073,7 @@ new HubConnectionBuilder()
.WithUrl("/chatHub")
.AddJsonProtocol(options =>
{
options.WriteIndented = false;
options.PayloadSerializerOptions.WriteIndented = false;
})
.Build();
```
Expand Down
45 changes: 41 additions & 4 deletions aspnetcore/signalr/configuration.md
Expand Up @@ -14,15 +14,16 @@ uid: signalr/configuration

ASP.NET Core SignalR supports two protocols for encoding messages: [JSON](https://www.json.org/) and [MessagePack](https://msgpack.org/index.html). Each protocol has serialization configuration options.

JSON serialization can be configured on the server using the [AddJsonProtocol](/dotnet/api/microsoft.extensions.dependencyinjection.jsonprotocoldependencyinjectionextensions.addjsonprotocol) extension method, which can be added after [AddSignalR](/dotnet/api/microsoft.extensions.dependencyinjection.signalrdependencyinjectionextensions.addsignalr) in your `Startup.ConfigureServices` method. The `AddJsonProtocol` method takes a delegate that receives an `options` object. The [PayloadSerializerSettings](/dotnet/api/microsoft.aspnetcore.signalr.jsonhubprotocoloptions.payloadserializersettings) property on that object is a JSON.NET `JsonSerializerSettings` object that can be used to configure serialization of arguments and return values. See the [JSON.NET Documentation](https://www.newtonsoft.com/json/help/html/Introduction.htm) for more details.
::: moniker range=">= aspnetcore-3.0"

JSON serialization can be configured on the server using the [AddJsonProtocol](/dotnet/api/microsoft.extensions.dependencyinjection.jsonprotocoldependencyinjectionextensions.addjsonprotocol) extension method. `AddJsonProtocol` can be added after [AddSignalR](/dotnet/api/microsoft.extensions.dependencyinjection.signalrdependencyinjectionextensions.addsignalr) in `Startup.ConfigureServices`. The `AddJsonProtocol` method takes a delegate that receives an `options` object. The [PayloadSerializerOptions](/dotnet/api/microsoft.aspnetcore.signalr.jsonhubprotocoloptions.payloadserializeroptions) property on that object is a `System.Text.Json` <xref:System.Text.Json.JsonSerializerOptions> object that can be used to configure serialization of arguments and return values. For more information, see the [System.Text.Json documentation](/dotnet/api/system.text.json).

As an example, to configure the serializer to use "PascalCase" property names, instead of the default "camelCase" names, use the following code:
As an example, to configure the serializer to not change the casing of property names, instead of the default "camelCase" names, use the following code in `Startup.ConfigureServices`:

```csharp
services.AddSignalR()
.AddJsonProtocol(options => {
options.PayloadSerializerSettings.ContractResolver =
new DefaultContractResolver();
options.PayloadSerializerOptions.PropertyNamingPolicy = null
});
```

Expand All @@ -32,6 +33,40 @@ In the .NET client, the same `AddJsonProtocol` extension method exists on [HubCo
// At the top of the file:
using Microsoft.Extensions.DependencyInjection;

// When constructing your connection:
var connection = new HubConnectionBuilder()
.AddJsonProtocol(options => {
options.PayloadSerializerOptions.PropertyNamingPolicy = null;
})
.Build();
```

### Switch to Newtonsoft.Json

If you need features of `Newtonsoft.Json` that aren't supported in `System.Text.Json`, See [Switch to Newtonsoft.Json](xref:migration/22-to-30#switch-to-newtonsoftjson).

::: moniker-end

::: moniker range="<= aspnetcore-2.2"

JSON serialization can be configured on the server using the [AddJsonProtocol](/dotnet/api/microsoft.extensions.dependencyinjection.jsonprotocoldependencyinjectionextensions.addjsonprotocol) extension method, which can be added after [AddSignalR](/dotnet/api/microsoft.extensions.dependencyinjection.signalrdependencyinjectionextensions.addsignalr) in your `Startup.ConfigureServices` method. The `AddJsonProtocol` method takes a delegate that receives an `options` object. The [PayloadSerializerSettings](/dotnet/api/microsoft.aspnetcore.signalr.jsonhubprotocoloptions.payloadserializersettings) property on that object is a JSON.NET `JsonSerializerSettings` object that can be used to configure serialization of arguments and return values. For more information, see the [JSON.NET documentation](https://www.newtonsoft.com/json/help/html/Introduction.htm).

As an example, to configure the serializer to use "PascalCase" property names, instead of the default "camelCase" names, use the following code in `Startup.ConfigureServices`:

```csharp
services.AddSignalR()
.AddJsonProtocol(options => {
options.PayloadSerializerSettings.ContractResolver =
new DefaultContractResolver();
});
```

In the .NET client, the same `AddJsonProtocol` extension method exists on [HubConnectionBuilder](/dotnet/api/microsoft.aspnetcore.signalr.client.hubconnectionbuilder). The `Microsoft.Extensions.DependencyInjection` namespace must be imported to resolve the extension method:

```csharp
// At the top of the file:
using Microsoft.Extensions.DependencyInjection;

// When constructing your connection:
var connection = new HubConnectionBuilder()
.AddJsonProtocol(options => {
Expand All @@ -41,6 +76,8 @@ var connection = new HubConnectionBuilder()
.Build();
```

::: moniker-end

> [!NOTE]
> It's not possible to configure JSON serialization in the JavaScript client at this time.
Expand Down

0 comments on commit a06a747

Please sign in to comment.