2

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

1 Answer 1

3

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It took me a little bit to figure out how to implement it in my design but it works great.
I really glad I could help. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.