I haven't written anything for a while. It's not because I'm lazy or busy, it's because I cannot get online ....
Data serialization and deserialization are used in today's project. The data uses the code automatically generated by EF and starts to be used. net comes with DataContractJsonSerializer for serialization and deserialization. After the code is written, debug, I X (Forgive me for swearing, because it makes me angry ), because the object has an attribute such as [DataContractAttribute (IsReference = true)], the system prompts that serialization is not allowed. Of course, you can modify the attribute manually. After the attribute is changed, the system prompts that the attribute of the base class EntityObject is not allowed.
MY God !!
Later, it was difficult to deserialize DataContractJsonSerializer into a set, so I decided to discard it. Using third-party things: the legendary Json.net
Today I want to talk about the serialization and deserialization functions of this component:
To put it bluntly, an online model is used 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 ;}
}
It is mainly used as the data source during testing.
The following is the serialized code. In order to make the comparison clear, we will first provide the method that comes with. net:
/// <Summary>
/// Convert an object to json format data
/// </Summary>
/// <Param name = "entity"> </param>
/// <Returns> </returns>
Public string EntityToJson (wf_Task_Inbox entity)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer (entity. GetType ());
Using (MemoryStream stream = new MemoryStream ())
{
Serializer. WriteObject (stream, entity );
Return Encoding. UTF8.GetString (stream. ToArray ());
}
}
Returns a string. the serialization method of the set is similar;
Below is the json.net code:
JsonConvert. SerializeObject (list, Formatting. None); only one row exists !!!! Of course, you must first Add a reference to json.net.
Next is the deserialization code, which comes with. net:
/// <Summary> /// convert json data to an object // </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 ;}}
At the same time, because the built-in wood has a generic extension, so I don't like it very much. The json.net method:
JsonConvert. DeserializeObject <List <wf_Task_Inbox> (jsonstring); this is a set. Of course, how to get a single object? You know, just remove T ....
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: "Name: 3 "},{" ID ":" 4 "," Name ":" Name: 4 "},{" ID ":" 5 "," Name ":" Name: 5 "},{" ID ":" 6 "," Name ":" Name: 6 "},{" ID ":" 7 "," Name ":" Name: 7 "},{" ID ":" 8 "," Name ":" Name: 8 "},{" ID ":" 9 "," Name ":" Name: "Name: 9 "}]
Attached to json.net, the download package is helpful and different versions of json.net
Aspx "> http://json.codeplex.com/Release/ProjectReleases.aspx