語言

JavaScriptConverter 類別

定義

提供一個抽象的基底類別,用於自訂型別的轉換器。

public ref class JavaScriptConverter abstract
public abstract class JavaScriptConverter
type JavaScriptConverter = class
Public MustInherit Class JavaScriptConverter
繼承
JavaScriptConverter

範例

以下範例展示了如何為該 ListItemCollection 類別建立自訂轉換器。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;
using System.Collections;

namespace System.Web.Script.Serialization.CS
{
    public class ListItemCollectionConverter : JavaScriptConverter
    {

        public override IEnumerable<Type> SupportedTypes
        {
            //Define the ListItemCollection as a supported type.
            get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(ListItemCollection) })); }
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            ListItemCollection listType = obj as ListItemCollection;

            if (listType != null)
            {
                // Create the representation.
                Dictionary<string, object> result = new Dictionary<string, object>();
                ArrayList itemsList = new ArrayList();
                foreach (ListItem item in listType)
                {
                    //Add each entry to the dictionary.
                    Dictionary<string, object> listDict = new Dictionary<string, object>();
                    listDict.Add("Value", item.Value);
                    listDict.Add("Text", item.Text);
                    itemsList.Add(listDict);
                }
                result["List"] = itemsList;

                return result;
            }
            return new Dictionary<string, object>();
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");

            if (type == typeof(ListItemCollection))
            {
                // Create the instance to deserialize into.
                ListItemCollection list = new ListItemCollection();

                // Deserialize the ListItemCollection's items.
                ArrayList itemsList = (ArrayList)dictionary["List"];
                for (int i=0; i<itemsList.Count; i++)
                    list.Add(serializer.ConvertToType<ListItem>(itemsList[i]));

                return list;
            }
            return null;
        }
    }
}
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Web.UI.WebControls
Imports System.Collections

Namespace System.Web.Script.Serialization.VB

    Public Class ListItemCollectionConverter
        Inherits JavaScriptConverter

        Public Overrides ReadOnly Property SupportedTypes() As _
            System.Collections.Generic.IEnumerable(Of System.Type)
            Get
                ' Define the ListItemCollection as a supported type.
                Return New ReadOnlyCollection(Of Type)(New List(Of Type) _
                (New Type() {GetType(ListItemCollection)}))
            End Get
        End Property

        Public Overrides Function Serialize(ByVal obj As Object, _
            ByVal serializer As JavaScriptSerializer) As _
            System.Collections.Generic.IDictionary(Of String, Object)

            Dim listType As ListItemCollection = CType(obj, ListItemCollection)

            If Not (listType Is Nothing) Then

                ' Create the representation.
                Dim result As New Dictionary(Of String, Object)

                Dim itemsList As New ArrayList()
                Dim item As ListItem
                For Each item In listType
                    ' Add each entry to the dictionary.
                    Dim listDict As New Dictionary(Of String, Object)
                    listDict.Add("Value", item.Value)
                    listDict.Add("Text", item.Text)
                    itemsList.Add(listDict)
                Next item
                result("List") = itemsList

                Return result
            End If
            Return New Dictionary(Of String, Object)
        End Function

        Public Overrides Function Deserialize(ByVal dictionary As _
            System.Collections.Generic.IDictionary(Of String, Object), _
            ByVal type As System.Type, ByVal serializer As JavaScriptSerializer) As Object

            If dictionary Is Nothing Then
                Throw New ArgumentNullException("dictionary")
            End If

            If type Is GetType(ListItemCollection) Then
                ' Create the instance to deserialize into.
                Dim list As New ListItemCollection()

                ' Deserialize the ListItemCollection's items.
                Dim itemsList As ArrayList = CType(dictionary("List"), ArrayList)
                Dim i As Integer
                For i = 0 To itemsList.Count - 1
                    list.Add(serializer.ConvertToType(Of ListItem)(itemsList(i)))
                Next i

                Return list
            End If

            Return Nothing

        End Function
    End Class
End Namespace

備註

這個 JavaScriptConverter 類別讓你能實作該類別原生不支援 JavaScriptSerializer 的受管理型態的序列化和反序列化流程。 當你需要更多序列化和反序列化的控制時,也可以用來 JavaScriptConverter 做。

該 SupportedTypes 屬性表示客製化轉換器所提供轉換服務的類型。

要表示該實例必須使用 JavaScriptSerializer 自訂轉換器,必須將轉換器註冊給實例。 如果你直接使用 JavaScriptSerializer 類別,應該用這個 RegisterConverters 方法註冊轉換器。 否則,如果你是從 ECMAScript(JavaScript)呼叫網頁方法,並且想使用自訂轉換器,可以在設定檔中加入 converters 元素來註冊。 欲了解更多資訊,請參閱 如何:在 Microsoft Ajax 中配置 ASP.NET 服務。

當 JavaScriptSerializer 實例正在序列化一個有自訂轉換器的類型時,序列化器會呼叫該 Serialize 方法。 同樣地,當 JavaScriptSerializer 實例在反序列化 JavaScript 物件符號(JSON)字串,並識別出 JSON 字串中的某個型別有自訂轉換器時,序列化器會呼叫該 Deserialize 方法。

給實施者的注意事項

當你從 繼承 時 JavaScriptConverter,必須覆蓋以下成員:

建構函式

名稱 Description
JavaScriptConverter()

初始化 JavaScriptConverter 類別的新執行個體。

屬性

名稱 Description
SupportedTypes

當在衍生類別中覆寫時,會得到一組支援型別的集合。

方法

名稱 Description
Deserialize(IDictionary<String,Object>, Type, JavaScriptSerializer)

當在衍生類別中覆寫時,會將所提供的字典轉換成指定類型的物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Serialize(Object, JavaScriptSerializer)

當在衍生類別中覆寫時,會建立一個名稱/值對的字典。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱