JSON.NET and nHibernate Lazy Loading of Collections

json.netnhibernate

Is anybody using JSON.NET with nHibernate? I notice that I am getting errors when I try to load a class with child collections.

Best Answer

I was facing the same problem so I tried to use @Liedman's code but the GetSerializableMembers() was never get called for the proxied reference. I found another method to override:

  public class NHibernateContractResolver : DefaultContractResolver
  {
      protected override JsonContract CreateContract(Type objectType)
      {
          if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType))
              return base.CreateContract(objectType.BaseType);
          else
              return base.CreateContract(objectType);
      }
  }