JavaScriptConverter クラス
定義
カスタム型コンバーターの抽象基本クラスを提供します。Provides an abstract base class for a custom type converter.
public ref class JavaScriptConverter abstract
public abstract class JavaScriptConverter
type JavaScriptConverter = class
Public MustInherit Class JavaScriptConverter
- 継承
-
JavaScriptConverter
例
次の例は、クラスのカスタムコンバーターを作成する方法を示して ListItemCollection います。The following example shows how to create a custom converter for the ListItemCollection class.
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 ます。The JavaScriptConverter class enables you to implement serialization and deserialization processes for managed types that are not natively supported by the JavaScriptSerializer class. また JavaScriptConverter 、シリアル化および逆シリアル化プロセスをより詳細に制御する必要がある場合にも使用できます。You can also use JavaScriptConverter when you need more control over the serialization and deserialization process.
プロパティは、 SupportedTypes カスタムコンバーターがコンバーターサービスを提供する対象の型を示します。The SupportedTypes property indicates the types for which a custom converter provides converter services.
インスタンスでカスタムコンバーターを使用する必要があることを示すには、 JavaScriptSerializer コンバーターをインスタンスに登録する必要があります。To indicate that a custom converter must be used by the JavaScriptSerializer instance, you must register the converter with the instance. クラスを直接使用する場合は、メソッドを使用して JavaScriptSerializer コンバーターを登録する必要があり RegisterConverters ます。If you are using the JavaScriptSerializer class directly, you should use the RegisterConverters method to register the converter. それ以外の場合、ECMAScript (JavaScript) から Web メソッドを呼び出し、カスタムコンバーターを使用する場合は、構成ファイルに要素を追加することで登録でき converters
ます。Otherwise, if you are invoking Web methods from ECMAScript (JavaScript) and you want to use the custom converter, you can register it by adding a converters
element in the configuration file. 詳細については、「 方法: Microsoft Ajax で ASP.NET Services を構成する」を参照してください。For more information, see How to: Configure ASP.NET Services in Microsoft Ajax.
JavaScriptSerializerインスタンスがカスタムコンバーターが登録されている型をシリアル化する場合、シリアライザーはメソッドを呼び出し Serialize ます。When the JavaScriptSerializer instance is serializing a type for which it has a custom converter registered, the serializer calls the Serialize method. 同様に、 JavaScriptSerializer インスタンスが JavaScript Object Notation (json) 文字列を逆シリアル化し、json 文字列内の型にカスタムコンバーターが関連付けられていることを認識すると、シリアライザーはメソッドを呼び出し Deserialize ます。Similarly, when the JavaScriptSerializer instance is deserializing a JavaScript Object Notation (JSON) string and recognizes that a type inside the JSON string has a custom converter associated with it, the serializer calls the Deserialize method.
注意 (実装者)
から継承する場合は、 JavaScriptConverter 次のメンバーをオーバーライドする必要があります。When you inherit from JavaScriptConverter, you must override the following members:
Deserialize(IDictionary<String,Object>, Type, JavaScriptSerializer)
SupportedTypesJavaScriptSerializerConvertToType<T>(Object)の実装によって使用されるメソッドを提供 JavaScriptConverter します。
SupportedTypesJavaScriptSerializer provides the ConvertToType<T>(Object) method that will be used by implementers of JavaScriptConverter. コンバーターコードは、シリアライザーによって渡されるディクショナリに含まれている値を取得し、その値を型のオブジェクトに変換できる必要があり
T
ます。Converter code must be able to take a value that is contained in the dictionary that the serializer passes to it, and then convert that value into an object of typeT
. カスタム変換コードを再実装してこれを実現するのではなく、メソッドを呼び出すことができ ConvertToType<T>(Object) ます。Rather than re-implementing the custom conversion code to accomplish this, you can call the ConvertToType<T>(Object) method.
コンストラクター
JavaScriptConverter() |
JavaScriptConverter クラスの新しいインスタンスを初期化します。Initializes a new instance of the JavaScriptConverter class. |
プロパティ
SupportedTypes |
派生クラスでオーバーライドされた場合、サポートされている型のコレクションを取得します。When overridden in a derived class, gets a collection of the supported types. |
メソッド
Deserialize(IDictionary<String,Object>, Type, JavaScriptSerializer) |
派生クラスでオーバーライドされた場合、提供されたディクショナリを、指定された型のオブジェクトに変換します。When overridden in a derived class, converts the provided dictionary into an object of the specified type. |
Equals(Object) |
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object. (継承元 Object) |
GetHashCode() |
既定のハッシュ関数として機能します。Serves as the default hash function. (継承元 Object) |
GetType() |
現在のインスタンスの Type を取得します。Gets the Type of the current instance. (継承元 Object) |
MemberwiseClone() |
現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object. (継承元 Object) |
Serialize(Object, JavaScriptSerializer) |
派生クラスでオーバーライドされた場合、名前/値ペアのディクショナリを構築します。When overridden in a derived class, builds a dictionary of name/value pairs. |
ToString() |
現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object. (継承元 Object) |