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

时间:2019-11-19 11:51:27

标签: asp.net-web-api2 asp.net-core-3.0 system.text.json

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

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

1 个答案:

答案 0 :(得分:0)

请尝试使用我写的作为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);