Serialize NHibernate object to JSON

0

How can I serialize an NHibernate object to JSON. When I try to serialize the error saying that it is not possible to serialize an object in context.

    
asked by anonymous 16.06.2016 / 01:41

2 answers

0

I was able to serialize, I believe it should be because in the mapping I was with Lazy enabled, I put the Lazy loads to false and it worked.

    
16.06.2016 / 16:56
1

You should not serialize a domain object. You should always load your domain object with NHibernate for example, and transform it into a ViewModel, that is, a representation of that object for a given call.

If you serialize a domain object, the serializer will go through all the properties of your object, including lists, you run a huge risk of encountering a cyclic reference error when serializing. Here is an example:

public class Cliente
{
   public List<Pedido> Pedidos {get;set;}
}

public class Pedido
{
   public Cliente Cliente {get;set;
}

If you load a Client and serialize the client object, it will also serialize the Request list and each request object will have the Client reference, which will be serialized as well. This goes into a loop, is called a cyclic reference.

    
19.10.2016 / 22:54