0

When using ASP.NET Core/6 to return object as JSON we use return Ok(data) which takes the data object and serialize it using camelCase.

But, what I want to serialize an object manually, it does not serialize using camelCase.

Here is how I am attempting to decentralize the object

System.Text.Json.JsonSerializer.Serialize(data)

Is there a service to use in ASP.NET Core that would serialize the object using the default formatter? IS not, how can I serialize using camelCase?

dbc
  • 104,963
  • 20
  • 228
  • 340
Jay
  • 1,168
  • 13
  • 41
  • 1
    https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties?pivots=dotnet-6-0#use-camel-case-for-all-json-property-names – Riwen May 28 '22 at 21:13

1 Answers1

3

You can use the object: JsonSerializerOptions. You have to pass it as a parameter after your object like this:

JsonSerializerOptions options = new JsonSerializerOptions()
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

System.Text.Json.JsonSerializer.Serialize(data, options);
Emilien Mathieu
  • 301
  • 3
  • 16