Web API中的System.Text.Json JsonRequired替换

我的Google不错,但似乎无法在System.Text.Json(特别是JsonRequired)中找到Json.Net注释的替代品。

新的microsoft框架真的不是牛顿软件的替代品吗?

liuzhaobing 回答:Web API中的System.Text.Json JsonRequired替换

请尝试使用我写的作为System.Text.Json扩展的库来提供缺少的功能:https://github.com/dahomey-technologies/Dahomey.Json

您将找到对JsonRequiredAttribute的支持。

public class A
{
    [JsonRequired]
    public int Id { get;set; }
}

通过调用JsonSerializerOptions来设置json扩展,该扩展方法是在命名空间Dahomey.Json中定义的扩展方法SetupExtensions。然后使用常规的Sytem.Text.Json API反序列化您的类。

JsonSerializerOptions options = new JsonSerializerOptions();
options.SetupExtensions();

const string json = @"{""Id"":12}";
A obj = JsonSerializer.Deserialize<A>(json,options);
本文链接:https://www.f2er.com/3073818.html

大家都在问