This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddControllers() | |
.AddJsonOptions( | |
options => | |
{ | |
options.JsonSerializerOptions.PropertyNamingPolicy = | |
SnakeCaseNamingPolicy.Instance; | |
}); ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SnakeCaseNamingPolicy : JsonNamingPolicy | |
{ | |
public static SnakeCaseNamingPolicy Instance { get; } = new SnakeCaseNamingPolicy(); | |
public override string ConvertName(string name) | |
{ | |
// Conversion to other naming conventaion goes here. Like SnakeCase, KebabCase etc. | |
return name.ToSnakeCase(); | |
} | |
} |