Home
Paste
Show Key
Listed
Keep Editing
using System.Text.Json; public class SnakeCaseNamingPolicy : JsonNamingPolicy { public static SnakeCaseNamingPolicy Instance { get; } = new(); public override string ConvertName(string name) => ConvertToSnakeCase(name); public static string ConvertToSnakeCase(string name) { return string.Concat(name.Select((c, i) => { if (i < 1) return c.ToString(); var pc = name[i - 1]; var nc = i + 1 >= name.Length ? '?' : name[i + 1]; if (pc == '_') //Property name already contains underscore return c.ToString(); if (char.IsUpper(c) && !(char.IsNumber(pc) && char.IsNumber(nc))) return $"_{c}"; if (char.IsNumber(c) && !char.IsNumber(pc) && !char.IsUpper(pc)) return $"_{c}"; return c.ToString(); })).ToLower(); } }
using System.Text.Json; public class SnakeCaseNamingPolicy : JsonNamingPolicy { public static SnakeCaseNamingPolicy Instance { get; } = new(); public override string ConvertName(string name) => ConvertToSnakeCase(name); public static string ConvertToSnakeCase(string name) { return string.Concat(name.Select((c, i) => { if (i < 1) return c.ToString(); var pc = name[i - 1]; var nc = i + 1 >= name.Length ? '?' : name[i + 1]; if (pc == '_') //Property name already contains underscore return c.ToString(); if (char.IsUpper(c) && !(char.IsNumber(pc) && char.IsNumber(nc))) return $"_{c}"; if (char.IsNumber(c) && !char.IsNumber(pc) && !char.IsUpper(pc)) return $"_{c}"; return c.ToString(); })).ToLower(); } }