Entity structure serializes POCO to JSON

I am using Ef 4.1 and I have a POCO object that I would like to serialize to JSON, I read that there is a problem with this when using lazy loading, but I'm not sure I can, because a Message can have a Message set.

Is there any way to do this? sirialize this kind of object in json?

My Message object looks like this:

 public class Message { [Key] public int Id { get; set; } public int? ParentId { get; set; } public string Title { get; set; } public string Content { get; set; } public DateTime CreatedAt { get; set; } public DateTime? LastModified { get; set; } public virtual User User { get; set; } public virtual Message Parent { get; set; } public virtual ICollection<Message> Children { get; set; } } 
+1
source share
4 answers

The problem is circular links. An easy way to avoid this is to use Json.Net http://james.newtonking.com/projects/json-net.aspx instead of the default default MVC serializer. The latest version of Json.Net will serialize objects with circular links out of the box. http://james.newtonking.com/projects/json/help/PreserveObjectReferences.html for more information about the problem

+2
source

It is advisable to load it using the Include () function. Linq example:

 var serializeMe = (from m in MyContext.Message.Include("User") where m.Id == someValue select m).ToList(); 

This will say that EF will immediately load the user's navigation property, instead of lazily loading it, and the serializer should not have problems with it.

+1
source

How about this:

  • Mark your class as [Serializable]
  • Use JsonSerializer to serialize your object to JSON.
  • Perhaps you use property loading in your EF request?
 Message msg = new Message(); var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(msg.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, msg); string json = Encoding.Default.GetString(ms.ToArray()); [Serializable] public class Message { } 
0
source

Well, let's do it in parts.

Why is this happening?

Because you have virtual properties. If you use EF, you really need them if you use Lazy download. You can configure your EF to not do this in this example:

  context.Configuration.ProxyCreationEnabled = false; 

where context is ObjectContext or DbContext ... this assumes you are using EF. But for most scenarios, this is not very good.

Possible Solution

As I always say: "There are no good or bad solutions, just different ways, and it depends on the context," saying that you can create dynamic objects.

If you only need to serialize a unique object, you can do something like this

  Json(new {@property1=yourObject.property1, @property2=yourObject.property2}) 

If you have a list, you can do this:

  var list = new List<dynamic>(); foreach(var item in myRepository.GetAll()) { list.Add(new { @property1= item.property1, @property2= item.property2, @property3= item.property3 }); } return Json(list, JsonRequestBehavior.DenyGet); 

I tried to make this as general as possible. Hope this helps someone!

Best wishes and a very nice day! :)

0
source

Source: https://habr.com/ru/post/910550/


All Articles