我已经梳理了MS docs,但是找不到与NewtonSoft JsonPropertyRequired等效的属性。
我在寻找什么:
public class Videogame
{
[JsonProperty(Required = Required.Always)]
public string Name { get; set; }
}
我只是缺少什么吗?Microsoft库中是否不存在这种级别的验证?
参考方案
从.NET core 3.0开始。仅支持以下几种:
JsonConverterAttribute
JsonExtensionDataAttribute
JsonIgnoreAttribute
JsonPropertyNameAttribute
不幸的是,即使自定义转换器也不起作用,因为空值会跳过调用Read和Write方法的操作。
public class Radiokiller
{
[JsonConverter(typeof(MyCustomNotNullConverter<string>))]
public string Name { get; set; }
}
public class MyCustomNotNullConverter<T> : JsonConverter<T>
{
public override bool CanConvert(Type typeToConvert) => true;
public override T Read(...)
{
//Not called for nulls
}
public override void Write(...)
{
// Not called for nulls
}
}
System.Text.Json合并两个对象 - c#是否可以用System.Text.Json?合并两个这样的json对象对象1{ id: 1 william: "shakespeare" } 对象2{ william: "dafoe" foo: "bar" } 结果对象{ id: 1 william: "dafoe" foo: …
.NET核心3:JsonPropertyName的序列化顺序(System.Text.Json.Serialization) - c#迁移到.NET Core 3时,我已经从Newtonsoft.Json序列化切换到System.Text.Json.Serialization。在所有功能中,我想继续使用JsonPropertyName属性。Newtonsoft版本允许ordering of serialized attributes:[JsonProperty(Order = 1)] pu…
JSON.NET如何删除节点 - c#我有一个像下面这样的json:{ "d": { "results": [ { "__metadata": { }, "prop1": "value1", "prop2": "value2", "__some…
json_encode网址失败 - php有人在this bug附近吗?echo json_encode(array('url'=>'/foo/bar')); {"url":"\/foo\/bar"} 我使用Zend_Json and Zend_Json_Expr以便我甚至可以在js对象中获取回调函数-但我无法获得…
将python json.dump()转换为javascript JSON.parse() - javascript问题摘要:无法解析从.json文件格式化为JSON对象的字符串长版:我有一些正在使用Python处理的推文,其中创建了一个我想传递到d3.js并解析的json文件。我将获得的推文写入文件中,因此在将它们写入文件之前,必须使用Python中的json.dump()命令对其进行序列化。蟒蛇def on_data(self, data): f = open(…