The DataContractJsonSerializer that comes with .net is serialized and deserialized. After the code is finished, debugged. I (forgive my swear words, because it really makes me angry). The entity has such an attribute as [DataContractAttribute(IsReference=true )], which prompts that it cannot be serialized. Of course, I have changed it by hand. After the code is finished, I prompted that this attribute of the base class EntityObject cannot be
MY God!!
Later, it was also because DataContractJsonSerializer was not working well when deserializing it into a collection, so I decided to abandon it. Adopted a third-party thing: the legendary
Today I will also talk about the functions of serialization and deserialization of this component:
Less nonsense, online model, as the use of data provided
public class wf_Task_Inbox
{
public wf_Task_Inbox(string id,string name)
{
= id;
= name;
}
[DataMember ]
public string ID { get; set; }
[DataMember]
public string Name{get ;set ;}
}
This is mainly used as a data source during testing
Next is the serialized code. For clear comparison, let’s first introduce the .net method:
/// <summary>
/// Convert an object into json format data
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public string EntityToJson(wf_Task_Inbox entity)
{
DataContractJsonSerializer serializer = newDataContractJsonSerializer(());
using (MemoryStream stream = new MemoryStream())
{
(stream, entity);
return Encoding.(stream .ToArray ());
}
}
The returned string, and the method of collection serialization is similar;
The following is the code:
JsonConvert .SerializeObject (list, ); There is and only one line! ! ! ! Of course, the first thing to do is add the reference
Next is the deserialization code, which comes with .net:
/// <summary>
/// Convert json data 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.(jsonstring)))
{
return (mStream) as wf_Task_Inbox;
}
}
At the same time, it is also because of the lack of generic extensions that come with, so I don’t like it very much. Method:
<List<wf_Task_Inbox>>(jsonstring); This is a collection, of course, how to do 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:"Name:4"},{"ID":"5","Name":"Name:"Name: 5"},{"ID":"6","Name":"Name:"Name: 6"},{"ID":"7","Name":"Name:"Name: 7"},{"ID":"8","Name":"Name:"Name: 8"},{"ID":"9","Name":"Name:"Name: 9"}]
The download address attached at the end is helpful and various versions in the download package.
/Release/
MY God!!
Later, it was also because DataContractJsonSerializer was not working well when deserializing it into a collection, so I decided to abandon it. Adopted a third-party thing: the legendary
Today I will also talk about the functions of serialization and deserialization of this component:
Less nonsense, online model, as the use of data provided
Copy the codeThe code is as follows:
public class wf_Task_Inbox
{
public wf_Task_Inbox(string id,string name)
{
= id;
= name;
}
[DataMember ]
public string ID { get; set; }
[DataMember]
public string Name{get ;set ;}
}
This is mainly used as a data source during testing
Next is the serialized code. For clear comparison, let’s first introduce the .net method:
Copy the codeThe code is as follows:
/// <summary>
/// Convert an object into json format data
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public string EntityToJson(wf_Task_Inbox entity)
{
DataContractJsonSerializer serializer = newDataContractJsonSerializer(());
using (MemoryStream stream = new MemoryStream())
{
(stream, entity);
return Encoding.(stream .ToArray ());
}
}
The returned string, and the method of collection serialization is similar;
The following is the code:
JsonConvert .SerializeObject (list, ); There is and only one line! ! ! ! Of course, the first thing to do is add the reference
Next is the deserialization code, which comes with .net:
Copy the codeThe code is as follows:
/// <summary>
/// Convert json data 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.(jsonstring)))
{
return (mStream) as wf_Task_Inbox;
}
}
At the same time, it is also because of the lack of generic extensions that come with, so I don’t like it very much. Method:
<List<wf_Task_Inbox>>(jsonstring); This is a collection, of course, how to do 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:"Name:4"},{"ID":"5","Name":"Name:"Name: 5"},{"ID":"6","Name":"Name:"Name: 6"},{"ID":"7","Name":"Name:"Name: 7"},{"ID":"8","Name":"Name:"Name: 8"},{"ID":"9","Name":"Name:"Name: 9"}]
The download address attached at the end is helpful and various versions in the download package.
/Release/