asp.net-mvc - JSONIgnore 类上的数据注释

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

我有一个公共(public)属性,它是一个本身包含许多属性的对象。使用 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);
    }
}

最佳答案

您可以在项目中添加契约(Contract)解析器。

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;
    }
}

关于asp.net-mvc - JSONIgnore 类上的数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34813747/

相关文章:

ajax - Asp Mvc3 webgrid 通过ajax进行分页和过滤

asp.net-mvc - 哪些CMS支持ASP.NET MVC

c# - 使用 C# 仅获取 JSON 的一部分

asp.net-core - ASP.NET MVC Core 仅日期格式无法使用数据注释

c# - 在某些情况下禁用 Required 验证属性

javascript - "MVC"和 "Single Page Application"模板有什么区别?

C# 使用 HttpWebResponse 获取 JSON 编码的字典以及错误的状态代码

c# - 使用已知和未知字段反序列化 json

c# - 是否可以将 DataAnnotations 与接口(interface)继承一起使用?

c# - 在模型中声明列表