Objects getting Circular Refernce under Ajax/JSON

Thread is closed for posting
5 posts, 1 answers
  1. Jeremy Mann
    Jeremy Mann avatar
    23 posts
    Member since:
    Jul 2009

    Posted 20 Jan 2010 Link to this post


    The json error via Firebug -
    A circular reference was detected while serializing an object of type [MY PERSISTANT TYPE]

    I have two objects creating the offense have a One-to-Many relationship which I'm guessing is leading to the above error, but I'm unsure of how to get around it it short of killing the relationship which I need for other areas in the app.

    Example: Customers has a one-to-many relationship with Orders.
    So if I'm in Orders, I can say Order.Customer.CustomerName and likewise in Customer I could do Customer.Order.OrderDate The problem comes in when I'm referencing the customer object via JSON webservice. JSON does not like that the Customer object can now do a circular refrence:    Customer.Order.Customer.Order....etc 

    How do I prevent this to a certain depth or prevent the circular refrence under this situation?


  2. Answer
    Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 20 Jan 2010 Link to this post

    Hello Jeremy Mann,
    One way is to remove the public getter, another way is to use serialization attributes at the properties to prevent the serialization of one of the sides.

    Greetings,
    Jan Blessenohl
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Jeremy Mann
    Jeremy Mann avatar
    23 posts
    Member since:
    Jul 2009

    Posted 25 Jan 2010 Link to this post

    I wanted to post a followup on the steps to take in order to save others time. This is an elaboration to Jan's answer.

    1. Add System.Web.Extensions reference to the project.
    2. Add using System.Web.Script.Serialization; to the offending object
    3. Add [ScriptIgnore] above the cyclical property you wish to block under JSON.

    Under my example, the "Orders" object would now look something like.....      
            [ScriptIgnore]  
            [Telerik.OpenAccess.FieldAlias("customer")]  
            public Customer Customer 
            {  
                get { return customer; }  
                set { this.customer = value; }  
            }  

    Hope it saves someone time!
  4. Anil
    Anil avatar
    2 posts
    Member since:
    Sep 2011

    Posted 14 Sep 2011 Link to this post

    Hi All,

    I m facing the same Circular Reference problem.
    In  my case i need to show associated Order in Customer Grid in a drop-down so order can be change when editing customer using Inline Edit mode.

    I can't use [ScriptIgnore] as this will remove column Order from Grid, which i need to show as well in the grid in drop-down list.

    So how can i prevent the circular refrence under this situation?
  5. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 15 Sep 2011 Link to this post

    Hi Anil,
    If you need the order collection on the customer instance, you have to set the reference from order to customer to be ignored.
    To be able to play the collection changes back to the database, the collection has to be set to 'Managed' so that the references are updated on the server side automatically.

    Greetings,
    Jan Blessenohl
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

Back to Top