[摘要] Revit二次开发:Newtonsoft Json自定义序列化字段class Program{static void Main(string[] args){var m = new JsonModel()
Revit二次开发:Newtonsoft.Json自定义序列化字段
class Program
{
static void Main(string[] args)
{
var m = new JsonModel()
{
Id = 0,
Name = "nnnn"
};
var json = JsonConvert.SerializeObject(m, new JsonSerializerSettings()
{
ContractResolver = new MyContractResolver(new List(){ "Id" })
});
Console.WriteLine(json);
}
}
public class JsonModel
{
[JsonProperty("myId")]
public int Id { get; set; }
public string Name { get; set; }
}
public class MyContractResolver : DefaultContractResolver
{
private List _names = null;
public MyContractResolver(List names)
{
_names = names;
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
string name = member.Name;
if (_names.Contains(name))
return base.CreateProperty(member, memberSerialization);
return null;
}
}