如何让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>));