1

我有一个很好的谷歌,但似乎无法在 System.Text.Json(特别是 JsonRequired)中找到 Json.Net 注释的替代品。

新的 Microsoft 框架真的不是 Newtonsoft 的替代品吗?

4

1 回答 1

0

请尝试我作为 System.Text.Json 的扩展编写的这个库,以提供缺少的功能:https ://github.com/dahomey-technologies/Dahomey.Json 。

您将找到对 JsonRequiredAttribute 的支持。

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

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

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

const string json = @"{""Id"":12}";
A obj = JsonSerializer.Deserialize<A>(json, options);
于 2019-12-11T20:49:27.090 回答