Home > OS >  How to serialize object in ASP.NET Core/6 using a camelCase?
How to serialize object in ASP.NET Core/6 using a camelCase?

Time:05-29

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?

CodePudding user response:

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);
  • Related