JSONIgnore类上的数据注释

时间:2016-01-15 14:52:03

标签: asp.net-mvc json.net data-annotations

我有一个公共属性,它是一个包含许多属性的对象。使用ASP.net MVC,当我序列化JSON数据时,我只需在我使用对象的地方添加[JsonIgnore]属性,这样它就不会显示内容。

有没有办法将[JsonIgnore]属性添加到类中,以便永远不会被序列化?

//[JsonIgnore]  ??
public class DataObj
{
    public string ConnectionName { get; set; }
    public string Query { get; set; }
    ...
}

public class Customer
{
    public string First { get; set; }
    public string Last { get; set; }
    [JsonIgnore]
    public DataObj Foo { get; set; }
}

public class ShipAddress
{
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    [JsonIgnore]
    public DataObj Foo { get; set; }
}
收到jvanrhyn提供的代码后,

我的解决方案 另外,这里有一个link解释更多。

public class DataObjFilterContractResolver : DefaultContractResolver
{
    public static readonly DataObjFilterContractResolver Instance = new DataObjFilterContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member,MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        if (property.DeclaringType.Name.StartsWith("DataObj") || property.PropertyName == "DataObj")
        {
            property.ShouldSerialize = instance => false;
        }
        return property;
    }
}


public class UtcJsonResult : JsonResult
{
    public UtcJsonResult(object data)
    {
        Data = data;
        JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    }

    private const string DateFormat = @"yyyy-MM-dd HH:mm:ssZ";

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null) throw new ArgumentNullException("context");
        if (Data == null) return;

        var response = context.HttpContext.Response;
        response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
        if (ContentEncoding != null) response.ContentEncoding = ContentEncoding;

        var isoConvert = new IsoDateTimeConverter {DateTimeFormat = DateFormat};
        JsonConvert.DefaultSettings = 
            () => new JsonSerializerSettings 
                { ContractResolver = new DataObjFilterContractResolver()};  //<--- Used here
        var json = JsonConvert.SerializeObject(Data, isoConvert);
        response.Write(json);
    }
}

1 个答案:

答案 0 :(得分:3)

您可以在项目中添加合同解析程序。

public class ShouldSerializeContractResolver : DefaultContractResolver
{
    public new static readonly ShouldSerializeContractResolver Instance =
    new ShouldSerializeContractResolver();

    protected override JsonProperty CreateProperty(MemberInfo member,
    MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);

        if (property.DeclaringType == typeof(DataObj))
        {
            property.ShouldSerialize =
                instance =>
                {
                    return false;
                };
        }

        return property;
    }
}