I cannot serialize then immediately deserialize a large object without issues. I followed advice from: JSON.NET and nHibernate Lazy Loading of Collections and JSON.Net Serialization of NHibernate Proxies (NH 3.3.2.4000) to get Json.Net working with my legacy system. Despite trying the suggestions and in various combinations i have had no success. Here are the settings that make most sense to me as i understand them all.
Converter:
protected override List<MemberInfo> GetSerializableMembers(Type objectType)
{
if (typeof(INHibernateProxy).IsAssignableFrom(objectType))
{
return base.GetSerializableMembers(objectType.BaseType);
}
else
{
return base.GetSerializableMembers(objectType);
}
}
Main Code:
var jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new NHibernateContractResolver(),
PreserveReferencesHandling = PreserveReferencesHandling.All,
TypeNameHandling = TypeNameHandling.Auto,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ObjectCreationHandling = ObjectCreationHandling.Replace
};
string serialisedEnquiry = JsonConvert.SerializeObject(enquiry, Formatting.Indented, jsonSerializerSettings);
Enquiry enq = JsonConvert.DeserializeObject<Enquiry>(serialisedEnquiry, jsonSerializerSettings);
I have another SO question open which may be of note: JSON.net null property Although as you will see in the last comment, i believe i have solved it. I am just waiting to solve this problem to confirm the other is fixed and not simply buried/replaced by this new error.
Fluent NHibernate 1.4.0.0
NHibernate 3.3.1.4000
Netwonsoft.Json 6.0.0.0
EDIT:
My exception is:
An unhandled exception of type 'NHibernate.LazyInitializationException' occurred in Newtonsoft.Json.dll
Additional information: Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session was closed
I have actually found this buried within my JSON:
$type : "NHibernate.Collection.Generic.PersistentGenericBag`1[[ComponentModel.Role, ComponentModel]], NHibernate"
I am not sure why when i have the NHibernate converter but it gives me a lead i can look into. I will post back if i find anything.