lazy loading while serializing to json

1,573 views
Skip to first unread message

Vladan Strigo

unread,
Feb 2, 2010, 6:16:20 AM2/2/10
to nhusers
I have a small problem while serializing an Entity to Json.

Lets say that we have a model: Customer <-> Product, so Customer has
Product propery, Product has Customers collection property....it comes
down to Customer.Product.Customers.

I eagerly load Customer and his Product, but when sending it to
Serializer (Json.Net), it accesses Customers property of Product, and
thus triggers the lazy load to db.

Is there a way to stop lazy load from occuring...and in this case I
don't want to:

a) not map this side of relationship
b) make another model like a viewmodel which is simpler
c) not eagerly fetch the Customers property because in fact I don't
need it.


Is there a way to stop lazy load while querying?!


Thanks!
Vladan

@Fabio: this is the last part to make serialization work as expected,
when it does Ill post up the code to serialize Entity to Json using
Json.Net :)


allan....@gmail.com

unread,
Feb 2, 2010, 8:55:05 AM2/2/10
to nhusers

Vladan Strigo

unread,
Feb 2, 2010, 9:16:53 AM2/2/10
to nhusers
This works like a charm to serialize the object in the first
place....however, at some points (in methods SerializeList(....) and
comparing one entity to another by calling its Equals method), it goes
to db.

Vladan

On 2 velj, 14:55, "allan.ritc...@gmail.com" <allan.ritc...@gmail.com>
wrote:
> http://stackoverflow.com/questions/286721/json-net-and-nhibernate-laz...

TheNephalim

unread,
Feb 2, 2010, 8:38:39 AM2/2/10
to nhusers
I've used JSON.Net and there are probably two ways you could avoid the
property so you don't trigger the serialization. The first is that
you can label the object and property with an attribute to ignore the
property during serialization. For example, see
http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size.aspx

Alternatively, you can write code that will manually build the JSON
that you need if the previous method fails. See Reading and Writing
JSON on http://james.newtonking.com/projects/json/help/

-Robert Eberhart

On Feb 2, 6:16 am, Vladan Strigo <vladan.str...@gmail.com> wrote:

DaBuddhaMan

unread,
Feb 2, 2010, 9:40:30 AM2/2/10
to nhusers
You can create your own ContractResolver.
In there you create your own IValueProvider by overriding the
CreateMemberValueProvider method.
In your own IValueProvider you override the GetValue method where you
check if the proxy was initialized or not.

Vladan Strigo

unread,
Feb 2, 2010, 10:05:27 AM2/2/10
to nhusers
Robert, thanks, but that's all I tried and it doesn't stop it from
being called :(

DaBudhaMan...this is exactly what I need...thanks sooo much!
Interestly I've digged through the code today and found the value
provider, however my mind just didn't see that as an option...I've
just looked in other places to add that same logic...thanks so much
for the heads up!

Ill post the code when I am done! :)

Vladan

Reply all
Reply to author
Forward
0 new messages