Serialization and deserialization of c data (recommended)

  • 2020-05-07 20:16:58
  • OfStack

When the code is finished, debugging, I X (forgive my dirty words, because it really makes me angry), the entity because of [DataContractAttribute(IsReference=true)] such a property, the prompt can not be serialized, of course, the hand to change 1, after the change, the prompt base class EntityObject this property can not be
MY God!!!!!
It was also because DataContractJsonSerializer did not work well when deserialized into collections that it was decided to scrap. Adopted the third thing: the legendary Json.net
Today I'm also going to talk about the component serialization and deserialization capabilities:
Without further ado, model is online, as a way to provide data
 
public class wf_Task_Inbox 
{ 
public wf_Task_Inbox(string id,string name) 
{ 
this.ID = id; 
this.Name = name; 
} 
[DataMember ] 
public string ID { get; set; } 
[DataMember] 
public string Name{get ;set ;} 
} 

This is mainly used as a test data source
Next is the serialized code. For comparison, here is the method that comes with net:
 
/// <summary> 
///  the 1 Object to json Format data  
/// </summary> 
/// <param name="entity"></param> 
/// <returns></returns> 
public string EntityToJson(wf_Task_Inbox entity) 
{ 
DataContractJsonSerializer serializer = newDataContractJsonSerializer(entity.GetType()); 
using (MemoryStream stream = new MemoryStream()) 
{ 
serializer.WriteObject(stream, entity); 
return Encoding.UTF8.GetString(stream .ToArray ()); 
} 
} 

It returns a string, and the collection serializes similarly;
The following is the code for json.net:
JsonConvert.SerializeObject (list, Formatting.None); There is only 1 row!! Of course, you first need to add a reference to json.net
Next is the deserialization code.net comes with:
 
/// <summary> 
///  will json  Data is converted into objects  
/// </summary> 
/// <param name="entity"></param> 
/// <param name="jsonstring"></param> 
/// <returns></returns> 
public wf_Task_Inbox JsonToEntity(string jsonstring) 
{ 
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(wf_Task_Inbox)); 
using (MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonstring))) 
{ 
return serializer.ReadObject(mStream) as wf_Task_Inbox; 
} 
} 

json, net, json, net, json, net
JsonConvert.DeserializeObject < List < wf_Task_Inbox > > (jsonstring); This is a collection, of course, how to do a single object, you know, remove T on the line...
The generated serialized data is in the standard json format:
[{" ID ":" 0 ", "Name" : "name: 0}, {" ID" : "1", "Name" : name: "1"}, {" ID ":" 2 ", "Name" : "name: 2"}, {" ID ":" 3 ", "Name" : "name: 3"}, {" ID ":" 4 ", "Name" : name: "4"}, {" ID ":" 5 ", "Name" : "name: 5 "}, {" ID ":" 6 ", "Name" : "name: 6"}, {" ID ":" 7 ", "Name" : "name: 7"}, {" ID ":" 8 "and" Name ":" name: 8 "}, {" ID ":" 9 ", "Name" : "name: 9"}]
Finally, the download address of json.net is attached. The download package contains help and various versions of json.net
http://json.codeplex.com/Release/ProjectReleases.aspx

Related articles: