atom feed2 messages in com.googlegroups.castle-project-usersRe: proxyobjects looses it's attributes?
FromSent OnAttachments
Jimmy ShimizuAug 12, 2008 8:57 am 
Lee HensonAug 12, 2008 1:58 pm 
Subject:Re: proxyobjects looses it's attributes?
From:Lee Henson (
Date:Aug 12, 2008 1:58:11 pm


I'm assuming you're using Newtonsoft Json.Net? Here's what I've been using for a while:

1) Create your own custom json serializer:

public class NHibernateProxyJsonSerializer : JsonSerializer { private static readonly MemberInfo[] NHibernateProxyInterfaceMembers = typeof(INHibernateProxy).GetMembers();

protected override List<MemberInfo> GetSerializableMembers(Type objectType) { var members = base.GetSerializableMembers(objectType);

members.RemoveAll(memberInfo => (IsMemberPartOfNHibernateProxyInterface(memberInfo)) ||

(IsMemberDynamicProxyMixin(memberInfo)) ||

(IsMemberMarkedWithIgnoreAttribute(memberInfo, objectType)) ||

(IsMemberInheritedFromProxySuperclass(memberInfo, objectType)));

var actualMemberInfos = new List<MemberInfo>();

foreach (var memberInfo in members) { var infos = memberInfo.DeclaringType.BaseType.GetMember(memberInfo.Name); actualMemberInfos.Add(infos.Length == 0 ? memberInfo : infos[0]); }

return actualMemberInfos; }

private static bool IsMemberPartOfNHibernateProxyInterface(MemberInfo memberInfo) { return Array.Exists(NHibernateProxyInterfaceMembers, mi => memberInfo.Name == mi.Name); }

private static bool IsMemberDynamicProxyMixin(MemberInfo memberInfo) { return memberInfo.Name == "__interceptors"; }

private static bool IsMemberMarkedWithIgnoreAttribute(MemberInfo memberInfo, Type objectType) { var infos = typeof(INHibernateProxy).IsAssignableFrom(objectType) ? objectType.BaseType.GetMember(memberInfo.Name) : objectType.GetMember(memberInfo.Name);

return infos[0].GetCustomAttributes(typeof (JsonIgnoreAttribute), true).Length > 0; }

private static bool IsMemberInheritedFromProxySuperclass(MemberInfo memberInfo, Type objectType) { return memberInfo.DeclaringType != objectType; } }

2) Create your own converter:

public class NHibernateProxyJsonConverter : JsonConverter { public override bool CanConvert(Type objectType) { return typeof(INHibernateProxy).IsAssignableFrom(objectType); }

public override void WriteJson(JsonWriter writer, object value) { new NHibernateProxyJsonSerializer().Serialize(writer, value); }

public override object ReadJson(JsonReader reader, Type objectType) { throw new NotImplementedException(); } }

3) Use the converter when you attempt to convert an object to Json (notepad code below):

JavaScriptConvert.SerializeObject( object, new JsonConverterCollection { new NHibernateProxyJsonConverter() } )

2008/8/12 Jimmy Shimizu <>

Hi, I have problems with Entities which is specified with Lazy = true, which I have [JsonIgnore] attributes on. Seems like they drop attributes therefor making them unserializable (self referencing loop).

debugging the app shows me that it's an array of proxy-objects that I'm trying to serialize, instead of an array of my entity. In some cases I don't get proxyobjects, and I'm not sure what triggers it.

Is this wanted behaviour? Can I somehow prevent lazy loading the class from some points, or convert them back to the original entity?