Technical Analysis of asp.net JSON serialization

Source: Internet
Author: User

Starting from ASP. NET 3.5, the. NET platform supports two serialization methods:
Copy codeThe Code is as follows:
1. DataContractSerializer
2. JavascriptSerializer

The former is mainly serialized Based on the Data contract (DataContract), through the following definition of the Data contract:
Code
Copy codeThe Code is as follows:
<DataMember ()> _
Public Class Model
<DataMember ()> Public Property PropertyA As String
<DataMember ()> Public Property PropertyB As String
<IgnoreDataMember ()> Public Property PropertyC As String
End Class

The attributes are serialized Based on <DataMember> and <IgnoreDataMember>.
Serialization Based on Data contracts has disadvantages. Its disadvantage lies in the need to determine whether to serialize attributes at the beginning of Data class definition, and the Data Model used everywhere) this may cause all attributes to be specified as DataMember, and useless attributes with default values (such as Nothing or Null) are also serialized. Second, attribute aliases cannot be used for serialization, the name defined in the service may be too long. If the attribute alias cannot be specified, the bandwidth will be wasted, resulting in slow communication between the server and the client.
PageMethod is the best use case of JSON serialization technology. It implements asynchronous calls (AJAX calls) on pages without writing all the logic into the service, business logic judgment and processing on the UI can be written in PageMethod. Insert the following ScriptManager Statement on the screen and specify EnablePageMethods as True to enable PageMethod.
<Asp: ScriptManager ID = "ScriptManager1" runat = "server" EnablePageMethods = "true">
</Asp: ScriptManager>
Many people see examples of PageMethod calling through ms ajax, but Microsoft has been working on ASP in the past two years. NET and jQuery, we can also call PageMethod through jQuery. However, GET cannot be used for jQuery call methods. As long as the POST object is empty, the class GET call can be implemented. The script is as follows:
Copy codeThe Code is as follows:
$. Ajax ({
Type: "POST ",
Url: "Default. aspx/GetJSON ",
Data :"{}",
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
Success: function (msg ){
Alert (msg );
}
});

The PageMethod life method is as follows:
Copy codeThe Code 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]
The Model class is defined as follows:
[Code]
<DataContract ()> _
Public Class Model
<DataMember (emitdefadefavalue: = False, IsRequired: = False, Name: = "A")> Public Property PropertyA As String
<DataMember (emitdefadefavalue: = False, IsRequired: = False, Name: = "B")> Public Property PropertyB As String
<DataMember (emitdefadefavalue: = False, IsRequired: = False, Name: = "C")> Public Property PropertyC As String
End Class

The serialized Model instance is:
{"A": "Hello", "B": "World "}
Here we will provide you with the popular JSON conversion class on the Internet, using JavascriptSerializer. The Code is as follows:
Copy codeThe 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. writeobjects (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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.