When serializing an object of type 'SubSonic.Schema.DatabaseColumn,' a circular reference was discovered.

.net c# entity-framework json subsonic

Question

I'm attempting to do a straightforward JSON return, but I'm running into problems.

public JsonResult GetEventData()
{
    var data = Event.Find(x => x.ID != 0);
    return Json(data);
}

I get an HTTP 500 error message with the exception mentioned in the question's title. Also, I attempted

var data = Event.All().ToList()

That resulted in the same issue.

Is this a bug or a flaw in my programming?

2
169
2/21/2013 1:02:07 PM

Accepted Answer

Your object hierarchy seems to have circular references, which the JSON serializer can not accept. Do you need every column? You could only choose the view's necessary properties:

return Json(new 
{  
    PropertyINeed1 = data.PropertyINeed1,
    PropertyINeed2 = data.PropertyINeed2
});

Your JSON object will become lighter and simpler as a result. Zzz-11-Zzz might be used to zzz-15-Zzz map between DTO objects and View objects if you have a lot of properties.

175
7/20/2009 12:52:01 PM

Expert Answer

JSON is a tree-based serialization format, much as XML and several other forms. It won't love you if your objects have circular references, like the "tree" does:

root B => child A => parent B => child A => parent B => ...

There are often methods for blocking navigation along a certain route, such as usingXmlSerializer Marking the parent property asXmlIgnore . I'm not sure whether this is doable with the specific json serializer, nor ifDatabaseColumn contains appropriate indicators (very unlikely, as it would need to reference every serialization API)

7
7/20/2009 12:53:23 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow