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) 调用 Web 方法,并且想要使用自定义转换器,可以通过在配置文件中添加 converters 元素来注册它。 有关详细信息,请参阅如何:在 Microsoft Ajax 中配置 ASP.NET 服务

JavaScriptSerializer 实例序列化为其注册了自定义转换器的类型时,序列化程序将调用该方法 Serialize 。 同样,当 JavaScriptSerializer 实例反序列化 JavaScript 对象表示法 (JSON) 字符串并识别 JSON 字符串中的类型具有与之关联的自定义转换器时,序列化程序将调用 Deserialize 该方法。

实施者说明

JavaScriptConverter继承时,必须重写以下成员:

构造函数

JavaScriptConverter()

初始化 JavaScriptConverter 类的新实例。

属性

SupportedTypes

当在派生类中重写时,获取受支持类型的集合。

方法

Deserialize(IDictionary<String,Object>, Type, JavaScriptSerializer)

当在派生类中重写时,将所提供的字典转换为指定类型的对象。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Serialize(Object, JavaScriptSerializer)

当在派生类中重写时,生成名称/值对的字典。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅