Analysis of asp. net JSON serialization techniques

  • 2020-05-10 17:56:08
  • OfStack

Starting from ASP.NET 3.5, there are two serialization modes supported under the.NET platform:
 
1.DataContractSerializer 
2.JavascriptSerializer 

The former is serialized primarily according to the data contract (DataContract), through a data contract as defined below:
code
 
<DataMember()> _ 
Public Class Model 
<DataMember()> Public Property PropertyA As String 
<DataMember()> Public Property PropertyB As String 
<IgnoreDataMember()> Public Property PropertyC As String 
End Class 

Which will be based on < DataMember > and < IgnoreDataMember > Determine whether the property is serialized.
Contract according to the data serialization is flawed, its drawback is that the data at the beginning of the class definition will need to determine whether attribute serialize for use over the data model (Data Model) may lead to need all specified for DataMember, and will be useless attributes have a default value properties (such as Nothing or Null) may also be serialized; 2 is that serialization cannot use property aliases, the names defined in the business may be too long, and failure to specify property aliases can waste bandwidth and slow down communication between the server and the client.
PageMethod is the best use of JSON serialization technology. Instead of writing all the logic in the service, the business logic judgment processing of UI can be written in PageMethod. Insert the ScriptManager declaration below and specify EnablePageMethods as True to open PageMethod.
< asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
< /asp:ScriptManager >
Many people have seen MS AJAX calling PageMethod, but since Microsoft has been cooperating with ASP.NET and jQuery in the past two years, we can also call PageMethod via jQuery. However, the calling method of jQuery cannot use GET, as long as the POST empty JSON object can implement the class GET call. The script is as follows:
 
$.ajax({ 
type: "POST", 
url: "Default.aspx/GetJSON", 
data: "{}", 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
success: function (msg) { 
alert(msg); 
} 
}); 

PageMethod's life method is as follows:
 
<WebMethod()> _ 
Public Shared Function GetJSON() As String 
Dim list As New List(Of Model) 
For index As Integer = 0 To 1000 
Dim m_JSON As New Model With {.PropertyA = "Hello", .PropertyB = "World"} 
list.Add(m_JSON) 
Next 
Dim str As String = JSONHelper.Serialize(list) 
Return str 
End Function 
[code] 
 Among them Model The class definition is as follows:  
[code] 
<DataContract()> _ 
Public Class Model 
<DataMember(EmitdefaultValue:=False, IsRequired:=False, Name:="A")> Public Property PropertyA As String 
<DataMember(EmitdefaultValue:=False, IsRequired:=False, Name:="B")> Public Property PropertyB As String 
<DataMember(EmitdefaultValue:=False, IsRequired:=False, Name:="C")> Public Property PropertyC As String 
End Class 

The serialized Model instance results are:
{"A":"Hello","B":"World"}
Here I will provide you with a popular JSON conversion class on the Internet, which USES JavascriptSerializer. The code is as follows:
 
Public Class JSONHelper 
Public Shared Function Serialize(Of T)(ByVal obj As T) As String 
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType()) 
Dim ms As New MemoryStream() 
serializer.WriteObject(ms, obj) 
Dim retVal As String = Encoding.Default.GetString(ms.ToArray()) 
Return retVal 
End Function 
Public Shared Function Deserialize(Of T)(ByVal json As String) As T 
Dim obj As T = Activator.CreateInstance(Of T)() 
Dim ms As New MemoryStream(Encoding.Unicode.GetBytes(json)) 
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType()) 
obj = CType(serializer.ReadObject(ms), T) 
ms.Close() 
Return obj 
End Function 
End Class 

Related articles: