I want to serialize some objects into Json. Sometimes (not always, and I don’t know what triggers it) one of these objects is a proxy, which causes the serializer to throw a circular reference error even though there is no circular reference: “A circular reference was detected while serializing an object of type ‘System.Reflection.RuntimeModule’.”
My code uses NHibernate and JavaScriptSerializer:
public static string Convert(object xiObject)
{
var lSerialiser = new JavaScriptSerializer();
return lSerialiser.Serialize(xiObject);
}
Lazy Loading is supposedly off, as in:
HasManyToMany(x => x.Managers)
.Not.LazyLoad();
It sounds really similar to this guy’s problem:
http://markmail.org/message/x5a2k7j7qtjmj73g#query:+page:1+mid:4r5lcggmfrcq5tby+state:results
So my plan is to use Json.net instead of JavaScriptSerializer, and implement the solution on that page, but it looks really complicated. Any other suggestions would be great.
Probably you can “unproxy” object before serializing it. This article shows how to do it – Identifying NHibernate proxy classes