.NET 6-骆驼无效

发布于 2025-01-24 13:05:20 字数 1719 浏览 0 评论 0原文

我曾尝试使用.NET 6上的Camelcase Insentive用于

我在startup.cs中像这样配置的API的内容,但是

            .AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                options.JsonSerializerOptions.IgnoreNullValues = true;
            });

我无法使用此分辨率来解决: https://github.com/andre-ss6 https:///github.com/github.com/dotnet/dotnet/runtime/runtime/31094/suecomment/31094#issuecomment/ 543342051

他建议使用以下代码:

            ((JsonSerializerOptions)typeof(JsonSerializerOptions)
    .GetField("s_defaultOptions",
        System.Reflection.BindingFlags.Static |
        System.Reflection.BindingFlags.NonPublic).GetValue(null))
    .PropertyNameCaseInsensitive = true;

我尝试和工作,但我认为很复杂,因为它使用了反射,我不知道该怎么想,有人有其他解决方案或解释?

我可以这样对其进行审理:

        var content = await response.Content.ReadAsStringAsync(cancellationToken);

        var result = JsonSerializer.Deserialize<InvestimentFundsResponseData>(content);

我的班级是,你怎么看,我不使用属性[jsonpropertyname]

    public class InvestimentFundsResponseData
    {
      public IEnumerable<InvestmentFundsResponse> Data { get; set;}
    }

    public class InvestmentFundsResponse
    {
      public Guid Id { get; set; }
    }

I have tried use camelCase insentive on .NET 6 for deseralize content from API

I configured like this in Startup.cs, but it is not working

            .AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                options.JsonSerializerOptions.IgnoreNullValues = true;
            });

I get to solve with this resolution: https://github.com/andre-ss6
https://github.com/dotnet/runtime/issues/31094#issuecomment-543342051

He recommended using the following code:

            ((JsonSerializerOptions)typeof(JsonSerializerOptions)
    .GetField("s_defaultOptions",
        System.Reflection.BindingFlags.Static |
        System.Reflection.BindingFlags.NonPublic).GetValue(null))
    .PropertyNameCaseInsensitive = true;

I tried and worked, but I thought is complex, because it is used reflection, I don't know what to thought, Someone have other solution or a explanation?

I deserialize it like this:

        var content = await response.Content.ReadAsStringAsync(cancellationToken);

        var result = JsonSerializer.Deserialize<InvestimentFundsResponseData>(content);

My class is, how can you saw, I don't use the attribute [JsonPropertyName]

    public class InvestimentFundsResponseData
    {
      public IEnumerable<InvestmentFundsResponse> Data { get; set;}
    }

    public class InvestmentFundsResponse
    {
      public Guid Id { get; set; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2025-01-31 13:05:20

jsonserializer.Deserialize不使用jSonserialializaizeOptionsaddjsonoptions配置> jsonoptions ):

var result = JsonSerializer.Deserialize<InvestimentFundsResponseData>(content, new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    Converters = {new JsonStringEnumConverter()},
    IgnoreNullValues = true
});

JsonSerializer.Deserialize does not use JsonSerializerOptions which are configured by AddJsonOptions, create and pass required options manually (possibly resolve ones from the DI via JsonOptions):

var result = JsonSerializer.Deserialize<InvestimentFundsResponseData>(content, new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    Converters = {new JsonStringEnumConverter()},
    IgnoreNullValues = true
});
蝶舞 2025-01-31 13:05:20

我遇到了同样的问题,所以我删除了system.net.json 5.0.2,并重新安装了6.0.2版,这为我解决了。

I had the same issue, so I removed System.Net.Json 5.0.2 and reinstalled the version 6.0.2 this solved it for me.

无悔心 2025-01-31 13:05:20

您也可以制作一个jsonserialialialoptions singleton,然后将其添加到di:

// Add this to the ConfigureServices routine in Startup.cs:
JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
{
    PropertyNameCaseInsensitive = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
    DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
    MaxDepth = 10,
    ReferenceHandler = ReferenceHandler.IgnoreCycles,
    WriteIndented = true
};
serializerOptions.Converters.Add(new JsonStringEnumConverter());
services.AddSingleton(s => serializerOptions);

然后使用,只需将其注入构造函数即可。这样,对选项的一个更改就可以通过您的所有代码进行。

注意:我更新了选项,以符合最新版本的system.text.json(6.0.9),并添加了一些我通常指定的。默认的MaxDepth是3,因此,如果您的父/子关系更深,则会错过一些数据。

You could also make a JsonSerializerOptions singleton and add that to DI:

// Add this to the ConfigureServices routine in Startup.cs:
JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
{
    PropertyNameCaseInsensitive = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
    DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
    MaxDepth = 10,
    ReferenceHandler = ReferenceHandler.IgnoreCycles,
    WriteIndented = true
};
serializerOptions.Converters.Add(new JsonStringEnumConverter());
services.AddSingleton(s => serializerOptions);

Then to use, simply inject it into the constructor. That way, one change to the options is carried through all your code.

Note: I updated the options to be compliant with the latest version of System.Text.Json (6.0.9) and added a few that I usually specify. The default MaxDepth is 3, so if you have deeper parent/child relationships than that it'll miss some data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文