views:

181

answers:

3

I'm using return Json(whatever); inside an ASP.NET MVC Action to return data to my page via JSON. I'm using jQuery to perform the JSON request and subsequent interaction with DOM.

when whatever contains NHibernate objects with collection members (i.e. contains ISet objects), the callback function is never invoked by jQuery.

We're using jQuery & NHibernate are the latest versions, ASP.NET MVC version 1.0.

Did anyone experience this issue? How did you work around it? (except not passing ISet's)

A: 

Yeah, the state bags that NHibernate put in there are notoriously difficult to serialize to JSON correctly. I would make a make a new object ans assign the values manually to it, including child collections, then pass the new object to the Json serializer return method, since it won't have all the state bag stuff.

Josh Pearce
A: 

Found the problem. My Hibernate objects had circular references. That in itself is not a design problem in itself, but since JSON (which is a notation, mind you) is tree-based, it cannot handle circular references, and hence it had thrown an exception.

JSON.net, for instance, can handle reference loops better, though not as finely-grained as I would have liked.

Shachar
json.net (http://json.codeplex.com/) supports circular references. Also see http://www.west-wind.com/weblog/posts/442969.aspx
Mauricio Scheffer
A: 

The Quick way is to project the Data into a new Object, a custom class with the properties you want to Serialize.

something like: Assuming you use LINQ to loop the short way, you can use a foreach if not;

        var whatever = Myclass.Select(t => new MyJSonClass{
                       p1 = t.property1,
                       p2= t.t.property2
                       });
       return Json(whatever);
Omar