Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Fix Circular Reference Error When Dealing With Json

Tags:

This question is a part of my original post here Get Data Into Extjs GridPanel

Below is my Controller that reads data from sql db and then I am trying to encode the result as JSON and send the data back to my gridview.js

public JsonResult writeRecord() //public string writeRecord()     {          Response.Write("Survey Completed!");         SqlConnection conn = DBTools.GetDBConnection("ApplicationServices2");           string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable";         SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn);           DataSet myData = new DataSet();         cmd.Fill(myData, "myTable");          conn.Open();         conn.Close();          return Json(myData, JsonRequestBehavior.AllowGet);         //return myData.GetXml();      }  

Here lies the problem, with the above code, I get my gridview table with no data when executing gridview.js, but if I directly access my controller's method like this

http://localhost:55099/GridView/writeRecord 

I get this error,

A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'.

Can someone please help..

like image 245
EagleFox Avatar asked Sep 25 '12 14:09

EagleFox


People also ask

How do I fix Typeerror converting circular structure to JSON?

The "Converting circular structure to JSON" error occurs when we pass an object that contains circular references to the JSON. stringify() method. To solve the error, make sure to remove any circular references before converting the object to JSON.

What is circular structure in JSON?

A circular structure is an object that references itself. To be able to stringify such objects, developers can utilize the replacer parameter in the stringify() method, making sure the function that is being passed in, filters out repeated or circular data.

What is wrong with circular references?

The circular reference error message "There are one or more circular references where a formula refers to its own cell either directly or indirectly. This might cause them to calculate incorrectly. Try removing or changing these references, or moving the formulas to different cells."

Are circular references bad Javascript?

It will not be a problem for garbage collection: any new Garbage Collector (>IE6) will handle circular references just fine! It might be a problem though if you are doing recursive functions, or printing the object. So the answer is: it is no problem unless you screw up yourselves :-) Show activity on this post.


2 Answers

I use the following tool for serializing and deserializing JSON:

http://james.newtonking.com/pages/json-net.aspx

It's very easy to use and very lightweight.

While serializing we use this option:

JsonConvert.SerializeObject(myObject, Formatting.Indented,                              new JsonSerializerSettings {                                     ReferenceLoopHandling = ReferenceLoopHandling.Ignore                              }) 

It ignores circular references.

Also json.net from newtonking is extremely fast.

The other options is to use DTO's and map them via Automapper as mentioned by Diver.

Edit: I suspect your store is wrong:

var store = Ext.create('Ext.data.JsonStore', {               storeId: 'myData',         reader: new Ext.data.JsonReader({             root: 'myTable',             fields: [{ name: 'Q1', type: 'int' },                      { name: 'Q2', type: 'int' },                      { name: 'Q3', type: 'int' },                      { name: 'Q4', type: 'int' },                      { name: 'Q5', type: 'int' },                      { name: 'Improvements', type: 'string' },                      { name: 'Comments', type: 'string'}]         }),          proxy: {              type: 'json',             url: 'GridView/writeRecord'         }     });   
like image 152
Johan Haest Avatar answered Sep 20 '22 22:09

Johan Haest


It's because something inside CultureInfo has a reference to itself (this type) and in process of converting to JSON it fails. To avoid this situation, you should use ViewModels (return to client only information that is needed). You can read more here http://blogs.msdn.com/b/dphill/archive/2009/01/31/the-viewmodel-pattern.aspx

In your situation you shoud create ViewModels for your data, convert your data to these data types and return them converted to JSON. For the purpose of converting from Model to ViewModel consider using AutoMapper http://automapper.codeplex.com/ or some similar tool.

like image 22
berliner Avatar answered Sep 18 '22 22:09

berliner