Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR .NET Core camelCase JSON Contract Resolver

Using .NET Core RC2. Got SignalR working, but trying to get it returning camelCase properties in JSON.

For APIs I'm using...

services.AddMvc().AddJsonOptions(o => {
    o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

Maybe there's just nothing in place yet for SignalR (after all, it's not even supposed to work yet...), but wondering if anyone's figured it out yet? I've tried a few things like...

services.AddTransient<IContractResolver, CamelCasePropertyNamesContractResolver>();

... but no go.

Anyone got this working yet?

like image 747
PersonThing Avatar asked Jun 15 '16 10:06

PersonThing


2 Answers

In SignalR 3.0 you can do this with the following statement, as described in the Microsoft Docs

services.AddSignalR()
  .AddNewtonsoftJsonProtocol(
    options =>
      options.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()
  );

like image 82
PeterB Avatar answered Sep 28 '22 09:09

PeterB


In .NET Core 3.0, using System.Text.Json

 services.AddSignalR().AddJsonProtocol(options =>
        {
            options.PayloadSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        });
like image 32
pka246 Avatar answered Sep 28 '22 10:09

pka246