Skip to content

Commit

Permalink
Add custom naming convention for JsonNamingPolicy for snake_case
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkilcommins committed Mar 20, 2024
1 parent ef1ac6e commit 3a3ec79
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion provider_azure_function/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
var jsonSerializerOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNamingPolicy = SnakeCaseNamingPolicy.Instance,
};
jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));

Expand Down
12 changes: 12 additions & 0 deletions provider_azure_function/Utilities/SnakeCaseNamingPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json;

public class SnakeCaseNamingPolicy : JsonNamingPolicy
{
public static SnakeCaseNamingPolicy Instance { get; } = new SnakeCaseNamingPolicy();

public override string ConvertName(string name)
{
// Conversion to other naming convention goes here. Like SnakeCase, KebabCase etc.
return name.ToSnakeCase();
}
}
7 changes: 7 additions & 0 deletions provider_azure_function/Utilities/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public static class StringUtils
{
public static string ToSnakeCase(this string str)
{
return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower();
}
}

0 comments on commit 3a3ec79

Please sign in to comment.