This is a migrated thread and some comments may be shown as answers.

Objects getting Circular Refernce under Ajax/JSON

4 Answers 167 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jeremy Mann
Top achievements
Rank 1
Jeremy Mann asked on 20 Jan 2010, 06:02 PM

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?


4 Answers, 1 is accepted

Sort by
0
Accepted
Jan Blessenohl
Telerik team
answered on 20 Jan 2010, 07:10 PM
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.
0
Jeremy Mann
Top achievements
Rank 1
answered on 25 Jan 2010, 04:37 PM
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!
0
Anil
Top achievements
Rank 1
answered on 14 Sep 2011, 12:34 PM
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?
0
Jan Blessenohl
Telerik team
answered on 15 Sep 2011, 09:42 AM
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 >>

Tags
General Discussions
Asked by
Jeremy Mann
Top achievements
Rank 1
Answers by
Jan Blessenohl
Telerik team
Jeremy Mann
Top achievements
Rank 1
Anil
Top achievements
Rank 1
Share this question
or