Is anybody using JSON.NET with nHibernate? I notice that I'm getting errors when i try to load a class with child collections.
Thanks
Graham
Is anybody using JSON.NET with nHibernate? I notice that I'm getting errors when i try to load a class with child collections. Thanks Graham |
|||||||
|
We had this exact problem, which was solved with inspiration from Handcraftsman's response here. The problem arises from JSON.NET being confused about how to serialize NHibernate's proxy classes. Solution: serialize the proxy instances like their base class. A simplified version of Handcraftsman's code goes like this:
IMHO, this code has the advantage of still relying on JSON.NET's default behaviour regarding custom attributes, etc. (and the code is a lot shorter!). It is used like this
Note: This code was written for and used with NHibernate 2.1. As some commenters have pointed out, it doesn't work out of the box with later versions of NHibernate, you will have to make some adjustments. I will try to update the code if I ever have to do it with later versions of NHibernate. |
|||||||||||
|
I use NHibernate with Json.NET and noticed that I was getting inexplicable "__interceptors" properties in my serialized objects. A google search turned up this excellent solution by Lee Henson which I adapted to work with Json.NET 3.5 Release 5 as follows.
To use it just put an instance in the ContractResolver property of your JsonSerializer. The circular dependency problem noted by jishi can be resolved by setting the ReferenceLoopHandling property to ReferenceLoopHandling.Ignore . Here's an extension method that can be used to serialize objects using Json.Net
|
|||||||||
|
I was facing the same problem so I tried to use @Liedman's code but the
|
|||||
|
Are you getting a circular dependancy-error? How do you ignore objects from serialization? Since lazy loading generates a proxy-objects, any attributes your class-members have will be lost. I ran into the same issue with Newtonsoft JSON-serializer, since the proxy-object didn't have the [JsonIgnore] attributes anymore. |
|||||||||
|
You will probably want to eager load most of the object so that it can be serialized:
A nice way to do this is to add a bool to the constructor (bool isFetchEager) of your data provider method. |
|||
|