ASP.NET Core 3.0 System.Text.Json 驼峰序列化

ASP.NET Core 3.0 System.Text.Json Camel Case Serialization

在 ASP.NET Core 3.0 Web API 项目中,如何将 System.Text.Json 序列化选项自动指定为 serialize/deserialize Pascal Case 属性为 Camel Case,反之亦然?

给定一个具有 Pascal Case 属性的模型,例如:

public class Person
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

以及使用 System.Text.Json 将 JSON 字符串反序列化为 Person class:

类型的代码
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Deserialize<Person>(json);

不成功反序列化,除非 JsonPropertyName 与每个 属性 一起使用,例如:

public class Person
{
    [JsonPropertyName("firstname")]
    public string Firstname { get; set; }
    [JsonPropertyName("lastname")]
    public string Lastname { get; set; }
}

我在 startup.cs 中尝试了以下方法,但就仍然需要 JsonPropertyName 而言没有帮助:

services.AddMvc().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});

// also the following given it's a Web API project

services.AddControllers().AddJsonOptions(options => {
    options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        });

如何使用新的 System.Text.Json 命名空间在 ASP.NET Core 3.0 中设置 Camel Case serialize/deserialize?

谢谢!

AddJsonOptions() 将为 MVC 配置 System.Text.Json。如果您想在自己的代码中使用 JsonSerializer,您应该将配置传递给它。

var options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Parse<Person>(json, options);

您可以使用 PropertyNameCaseInsensitive。您需要将其作为参数传递给解串器。

var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var person = JsonSerializer.Deserialize<Person>(json, options);

哪个(来自docs):

Gets or sets a value that determines whether a property's name uses a case-insensitive comparison during deserialization. The default value is false

因此,它没有指定驼峰式或 PascalCase,但它将使用不区分大小写的比较。


下面将为通过控制器端点传递的 Json 配置 System.Text.Json:

services.AddControllers()
        .AddJsonOptions(options => {
            options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
         }); 

您仍然可以通过安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson Nuget 包在应用程序范围内设置它,它允许您使用以前的 Json 序列化程序实现:

services.AddControllers()
        .AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        });

感谢 Poke,答案在这里找到:

startup.cs中:

// keeps the casing to that of the model when serializing to json
// (default is converting to camelCase)
services.AddMvc()
    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); 

这意味着您不需要导入 newtonsoft.json。

options.JsonSerializerOptions.PropertyNamingPolicy 的唯一其他选项是 JsonNamingPolicy.CamelCase。似乎没有任何其他 JsonNamingPolicy 命名策略选项,例如 snake_case 或 PascalCase。

如果你想要 camelCase 序列化,请在 Startup.cs 中使用此代码:(例如 firstName)

services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        });

如果你想要 PascalCase 序列化,请在 Startup.cs 中使用此代码:(例如 FirstName)

services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy= null;
        );

试试这个!

StartUp.cs里面的ConfigureServices方法中写:

 services.AddMvc()
                    .AddJsonOptions(options =>
                    options.JsonSerializerOptions.PropertyNamingPolicy
                     = JsonNamingPolicy.CamelCase);

您需要 Newtonsoft.Json.Serialization & System.Text.Json

等名称空间