Home > Software engineering >  How to serialize properties to lower case using System.Text.Json.JsonSerializer
How to serialize properties to lower case using System.Text.Json.JsonSerializer

Time:10-09

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.

CodePudding user response:

If you really are looking for all lowercase property names and not camel-case property names e.g. FullName becomes fullname and not fullName, there's nothing built-in - you will have to create your own JsonNamingPolicy like so:

LowerCaseNamingPolicy.cs

public class LowerCaseNamingPolicy : JsonNamingPolicy
{
  public override string ConvertName(string name)
  {
      if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0]))
          return name;

      return name.ToLower();
  }
}

Usage:

var options = new JsonSerializerOptions {
    PropertyNamingPolicy = new LowerCaseNamingPolicy(),
};

var json = JsonSerializer.Serialize(estoOst, options);

However, I think you're looking for camel-case naming, considering you mentioned Json.NET which also doesn't have a lowercase naming policy.

If so, set the JsonSerializerOptions.PropertyNamingPolicy property to JsonNamingPolicy.CamelCase to serialise property names into a camel-case format:

var options = new JsonSerializerOptions {
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

var json = JsonSerializer.Serialize(estoOst, options);

Here's a working demo to output & show the difference of output for both methods:

public class Program
{
  public static void Main()
  {
      var person = new Person
      {
          Age = 100,
          FullName = "Lorem Ipsum"
      };

      var camelCaseJsonSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
      var lowercaseJsonSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = new LowerCaseNamingPolicy()};

      var camelCaseJson = JsonSerializer.Serialize(person, camelCaseJsonSerializerOptions);
      var lowercaseJson = JsonSerializer.Serialize(person, lowercaseJsonSerializerOptions);

      Console.WriteLine(camelCaseJson);
      Console.WriteLine(lowercaseJson);
  }
}

public class Person
{
  public string FullName { get; set; }
  public int Age { get; set; }
}

public class LowerCaseNamingPolicy : JsonNamingPolicy
{
  public override string ConvertName(string name)
  {
      if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0]))
          return name;

      return name.ToLower();
  }
}

Output:

{"fullName":"Lorem Ipsum","age":100}
{"fullname":"Lorem Ipsum","age":100}
  • Related