JSON casing in ASP.NET Core

Some mallethead decided that they wanted to take UpperCamelCase names of properties and change them to lowerCamelCase when sending JSON to the client. Sure, maybe this fits standard naming conventions, but it means that you’ll end up with different property names.

public JsonResult GetFoo()
{
    return Json(new Foo { 
        Name = "Jane Doe", 
        Age = 32 
    });
}

This returns the following content:

{"name":"Jane Doe","age":32}

Personally, I want the properties to match. Here’s the change to make that happen:

// dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson
 
// Startup.cs: in ConfigureServices
services.AddNewtonsoftJson(options =>
    {
        options.UseMemberCasing();
    });

Now the same code produces:

{"Name":"Jane Doe","Age":32}

Comments

Popular posts from this blog

C# Record Serialization

Add timestamp to photo using ImageMagick

Read/write large blob to SQL Server from C#