2

When I call any of my api functions that return JSON, it always alters the casing of the object's properties. This must be the default behavior as I haven't configured anything.

However, I'd prefer if it serialized my objects using the same casing that's present in the class, ie. no modification, just copy what's there.

So if I have:

public class Exercise
{
    public string ExerciseId { get; set; }
    public string ExerciseName { get; set; }
}

I'd like the properties to be serialized as ExerciseId and ExerciseName, not exerciseId and exerciseName.

The target framework is .NET 6.0. I haven't registered any middleware, I'm just adorning my classes with the [Serializable] attribute.

Here's an example of the JSON that is output:

{
   "exerciseId":"BBBC",
   "exerciseName":"Barbell Bicep Curl"
}

How do I configure that and is it possible to configure it in a single location and have it apply everywhere?

Legion
  • 3,922
  • 8
  • 51
  • 95
  • There isn't enough information to help you. You haven't specified .NET Framework (which uses JSON.NET as the default) or .NET (which uses System.Text.Json). In addition, if you've registered any middleware for controlling serialization, you would need to include that in your question. – David L Jan 18 '23 at 20:06
  • @DavidL Understood. I've added the requested information to the question. – Legion Jan 18 '23 at 20:11
  • what does the JSON look like? c# is inherently case sensitive. – KeithL Jan 18 '23 at 20:11
  • @KeithL I've added an example of the JSON that is output. – Legion Jan 18 '23 at 20:13

2 Answers2

3

To configure the default JSON serialization handler across all controllers / actions, set the naming policy via AddJsonOptions in your Program.cs.

builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
});

This will maintain the casing utilized in your C# classes.

https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.propertynamingpolicy?view=net-6.0

Tristan
  • 1,608
  • 1
  • 20
  • 34
  • Works great, thanks! Really appreciate not having to add a dependency. – Legion Jan 18 '23 at 20:31
  • The Newtonsoft.Json section seems uncalled for here and could lead people into bad paths if they stop reading there (changing the serialization library because of this feature is completely unnecessary). I understand people following just that without reading to the end are making a mistake themselves, but I'd still suggest removing the unnecessary part from your answer to avoid the confusion and avoid being potentially misleading. – julealgon Jan 18 '23 at 20:41
  • Yeah, I agree. I always use Newtonsoft JSON so was habit to start there first. I've edited the answer, thanks. – Tristan Jan 18 '23 at 20:59
3

From How to customize property names and values with System.Text.Json doc:

Note
The web default is camel case.

If you want to switch from camel case to using property names then you need to change json options (note that for Minimal APIs you will need to configure JsonOptions from Microsoft.AspNetCore.Http.Json, see this answer):

builder.Services.AddControllers()
    .AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);
Guru Stron
  • 102,774
  • 10
  • 95
  • 132