Geeks With Blogs

News profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers

Donate Bitcoins

Check out Elapser from T3rse!
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.

Programming and Learning from SD

We’re using Entity Framework to query a complex object graph using the .Include(“”) method to eager load the results, then sending it to the client with the WebApi. We’ve overridden the JavaScript Serializer in the Application_Start of Global.asax.cs of our MVC application to avoid circular dependencies. These occurred because of the Code First classes that were generated from the EF Power Tools had references to each other. It also created really large JSON structures that we didn’t need all the properties filled out. To avoid this, we added the [JsonIgnore] attribute to properties we don’t need.

// Add this in the Application_Startup of the Global.asax.cs

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

// add the attribute, using partial classes and MetaData
// Entity Framework class
public partial class Entity
{
    public IEnumerable<string> Attributes { get; set; }
}

[MetadataType(typeof(EntityMetaData))]
public partial class Entity
{

}

public class EntityMetaData
{
  // I don’t need these on the client side
  [JsonIgnore]
  public IEnumerable<string> Attributes { get; set; }
}

Now the JSON is much cleaner.

http://james.newtonking.com/projects/json/help/index.html?topic=html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor.htm

Posted on Tuesday, March 5, 2013 1:57 PM JavaScript | Back to top


Comments on this post: JsonIgnore attribute in Json.Net to ignore properties

No comments posted yet.
Your comment:
 (will show your gravatar)
 


Copyright © Aligned | Powered by: GeeksWithBlogs.net | Join free