ActiveX
ActiveX

Reputation: 1166

Newtonsoft Json - OutOfMemoryException

All,

Environment: ASP.net 2.0, Nhibernate 3.3, Json.net (latest, 6.x)

I am using latest version of Newtonsoft.Json library. When I load an entity using nhibernate (my entities reference other entities and are loaded lazily) I receive either an out of memory exception or stackoverflow exception.

Code for outofmemory exception:

JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
    string json = JsonConvert.SerializeObject(container.DataItem, settings);

Code for stackoverflow exception:

JsonSerializerSettings settings = new JsonSerializerSettings();
    settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
    settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
    string json = JsonConvert.SerializeObject(container.DataItem, settings);

People have these issues but there seems to be no solution. I see responses such a your graph is to large or too deep but my object graph is small,- I just call the code above lots of times (once per each object). I need a fix for this.

Upvotes: 3

Views: 3193

Answers (1)

Firo
Firo

Reputation: 30813

you are using lazy loading so NHibernate hands back proxies here and there and these proxies have references to a System.Type object which will have endless loops and also a reference to the session and sessionfactory which will be heavy on its own, check NHibernate.Proxy.INHibernateProxy.

So either:

  • eager load the things to serialize or
  • specify directly which properties to serialize or
  • don't serialize entities alltogether

Upvotes: 3

Related Questions