question

Andrus-0543 avatar image
0 Votes"
Andrus-0543 asked AgaveJoe answered

How to serialize properties to lower case using System.Text.Json.JsonSerializer

ASP.NET 5 MVC Core application controller consumes json API using

   using System.Text.Json;

     public async Task<IActionResult> EstoAPICall()
     {
   ...
   EstoOst estoOst;
   ....
   var json = JsonSerializer.Serialize(estoOst);
   StringContent content = new(json, Encoding.UTF8, "application/json");
   using var response = await httpClient.PostAsync("https://example.com", content);
   }
    
   public class EstoOst
   {
     public decimal Amount { get; set; }
     }

This returns error response because caller requires lower case amount but Serialize method returns upper case Amount.

How to fix this ? Switching to Newtosoft or changing class property name to lowercase seems to be not good solutions.

dotnet-aspnet-core-mvcdotnet-aspnet-core-webapidotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

The official documentation illustrates how to control casing. For example, camel casing.

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.