4

I have several BusinessObject classes that refer to each other and I need to serialize one in a JsonResponse and return it to my view. I keep getting a circular reference exception and I cannot get rid of it. I have placed the [ScriptIgnore()] decorator on every property that is not a simple data type property and I am still getting the exception. I cant figure out where the problem is, as I am blocking the serializer from just about everything and it is still blowing up on me.

Is there any way to see what they current state of the serialized object is?

    [HttpPost]
    public JsonResult GetAnalysisInfo(int id)
    {
        ProjectContext myDB = db;
        SearchAnalysis searchanalysis = SearchAnalysis.Find(myDB, id);
        //searchanalysis.Results = searchanalysis.SearchResultsPrototype(myDB);
        return this.Json(searchanalysis);
    }

Update

I have also tried implementing ISerializable to no avail. My GetObjectData is very simple:

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("SearchAnalysisId", this.SearchAnalysisId);
        info.AddValue("Created", this.Created);
    }

Still getting a CircularRefernce error. DateTime data types don't cause problems with Serialization right?

CodeWarrior
  • 7,388
  • 7
  • 51
  • 78
  • My classes are Entity Framework entities. It has only a couple of simple persisted properties, a bunch of unmapped properties, and quite a few static and instance methods. The class definition is a bit too long to post here. The Json object is actually in the return line of the controller action and I have posted it above. – CodeWarrior Feb 13 '12 at 17:05
  • Interestingly, after attempting to implement ISerializable (and failing) I added the [Serializable] attribute decorator. Everything is working fine now (although the properties that I am specifically serializing are not the ones that end up in the serialized object... If I remove ISerializable, it fails. If I remove [Serializable] it fails. – CodeWarrior Feb 14 '12 at 14:37
  • Did you ever figure out why `ScriptIgnore` wasnt working? I'm having the same problem with my Entities split out into a separate class library and trying to serialize the entity from my controller in my web project. – bflemi3 Jan 27 '13 at 21:59
  • Afraid I didn't. I ended up, in the case above, just returning an anonymous type, and in a different instance, creating a couple of DTOs to house the data I wanted to return. – CodeWarrior Jan 28 '13 at 18:00

1 Answers1

1

What I'm doing to prevent that error is to return an anonymouse type that reflects my object's properties like this :

    public JsonResult CompanyListJson()
    {
        var list = (from companies in appContext.Companies
                    where companies.Active.Equals(true)
                    select new
                    {
                        Name = companies.DbName,
                        Id = companies.Id,
                        Active = companies.Id.Equals(CurrentCompany.Id)

                    });
        return Json(list, JsonRequestBehavior.AllowGet);
    }

Maybe it's not the best way, but it allows me to keep my JSON thin and push only the data I need ( and avoid that circular reference exception of course )

Looking at your sample, I would select new anonymouse type from SearchAnalysis, taking the properties I need. That should work

Paweł Staniec
  • 3,151
  • 3
  • 29
  • 41
  • Initially I thought of doing that, although I need to call a couple of other methods on SearchAnalysis to populate some properties after retrieving from database. – CodeWarrior Feb 13 '12 at 17:25
  • and why is that in conflict with using LINQ to create new anynoumous collection as a final step ? :) – Paweł Staniec Feb 13 '12 at 17:28
  • I guess it was an indirect way of asking if returning anonymous typed objects would allow me to call instance methods to populate them with unmapped property values. – CodeWarrior Feb 13 '12 at 19:46