Home > Enterprise >  How do I serialize object properties to lower case using System.Text.Json?
How do I serialize object properties to lower case using System.Text.Json?

Time:10-10

I have an ASP.NET 5 MVC Core application controller with the below code inside:

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 causes an error as the API requires lower case amount in the JSON but .Serialize(...) is returning upper case Amount.

How can I fix this?

Switching to Json.NET, or changing the class property name to lowercase don't seem to be 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