Pete
Pete

Reputation: 2463

JSONIgnore data annotations on class

I have a public property which is an object that contains numerous properties itself. Using ASP.net MVC, when I serialize the JSON data I simply add the [JsonIgnore] attribute wherever I use the object so it doesn't display the contents.

Is there a way to add the [JsonIgnore] attribute to the class so it never is serialized?

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

My solution after receiving the code provided by jvanrhyn.
Also, here is a link that explains more.

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

Upvotes: 2

Views: 2108

Answers (1)

jvanrhyn
jvanrhyn

Reputation: 2824

You can add a Contract Resolver in your project.

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

Upvotes: 3

Related Questions