在我的项目中一个类有太多的属性,有的时候我只想将其中一两个系列化成Json字符串传递到客户端来,Newtonsoft.Json为我们提供了JsonIgnoreAttribute这个Attribute,但是由于属性太多,大部分属性几乎都贴上了标签实在太麻烦。如下的User类,我只想要Id和RealName,但对其他属性不得不都贴上标签
public class User
{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _userName;
[JsonIgnore]
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _pho;
[JsonIgnore]
public string Pho
{
get { return _pho; }
set { _pho = value; }
}
private string _realName;
public string RealName
{
get { return _realName; }
set { _realName = value; }
}
private string _email;
[JsonIgnore]
public string Email
{
get { return _email; }
set { _email = value; }
}
private string _addr;
[JsonIgnore]
public string Addr
{
get { return _addr; }
set { _addr = value; }
}
}
这样实在不怎么雅观,而且很麻烦。{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _userName;
[JsonIgnore]
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _pho;
[JsonIgnore]
public string Pho
{
get { return _pho; }
set { _pho = value; }
}
private string _realName;
public string RealName
{
get { return _realName; }
set { _realName = value; }
}
private string _email;
[JsonIgnore]
public string Email
{
get { return _email; }
set { _email = value; }
}
private string _addr;
[JsonIgnore]
public string Addr
{
get { return _addr; }
set { _addr = value; }
}
}
幸亏Newtonsoft.Json给我们提供了源代码,这样我们就可以扩展她了。我在Newtonsoft.Json里添加了两个Attribute:JsonCustomSerializationAttribute是用于贴在类上的,JsonDisplayAttribute是用于贴在字段或者属性上的,当该类贴上了JsonCustomSerializationAttribute标签后,系列化的时候就只会将贴上了JsonDisplayAttribute标签的属性或字段给系列化出来,如下:
[JsonCustomSerialization]
public class User
{
private int _id;
[JsonDisplay]
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _userName;
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _pho;
public string Pho
{
get { return _pho; }
set { _pho = value; }
}
private string _realName;
[JsonDisplay]
public string RealName
{
get { return _realName; }
set { _realName = value; }
}
private string _email;
public string Email
{
get { return _email; }
set { _email = value; }
}
private string _addr;
public string Addr
{
get { return _addr; }
set { _addr = value; }
}
}
public class User
{
private int _id;
[JsonDisplay]
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _userName;
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _pho;
public string Pho
{
get { return _pho; }
set { _pho = value; }
}
private string _realName;
[JsonDisplay]
public string RealName
{
get { return _realName; }
set { _realName = value; }
}
private string _email;
public string Email
{
get { return _email; }
set { _email = value; }
}
private string _addr;
public string Addr
{
get { return _addr; }
set { _addr = value; }
}
}
这个时候只系列化Id和RealName这两个属性了
除了添加上面两个attribute外还修改了JsonSerializer.cs代码中的307行的位置加入了如下代码
if (objectType.IsDefined(typeof(JsonCustomSerializationAttribute), true))
{
ignored = !member.IsDefined(typeof(JsonDisplayAttribute), true);
}
你也可以根据上面的代码编译自己的版本,如果你像我这样比较懒的话也可以从这里下载我编译好了的版本{
ignored = !member.IsDefined(typeof(JsonDisplayAttribute), true);
}
/Files/yuyijq/Release.rar