对于那些比我更了解 .NET 6 的人,您是否找到了 Netwonsoft 的 JsonObjectAttribute 的 NamingStrategy 设置的替代品?
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class PortConfig
{
public string Http { get; set; }
public string Https { get; set; }
}
我想要一些可以在 CLASS 上标记的东西来声明其属性的命名策略,而不是必须在每个属性上放置
JsonPropertyName("CamelCaseName")
属性。
您可以将选项传递给序列化器
var options = new System.Text.Json.JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
或者在启动时您可以配置默认值
.AddJsonOptions(opts =>
{
opts.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
})
另一个选项是使用
JsonSourceGenerationOptions
如果您不想全局配置它或使用序列化器选项
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
public class PortConfig
{
public string Http { get; set; }
public string Https { get; set; }
}
如果您需要构建自定义命名类,文档位于此处 自定义 Json 属性命名