如何让NewtonSoft.Json序列化时忽略掉所有的Property

实现一个ContractResolver

public class IgnorePropertiesResolver : DefaultContractResolver
if (member is PropertyInfo)
{
property.ShouldSerialize = x => false;
}
return property;
}

逻辑代码里

var colors = new List<Color>
{
Color.red,
Color.green
};

var settings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new IgnorePropertiesResolver()
};

var json = JsonConvert.SerializeObject(colors, settings);
var newColorList = JsonConvert.DeserializeObject(json, typeof(List<Color>));

 

posted @ 2021-12-29 11:20  lilei9110  阅读(272)  评论(0编辑  收藏  举报