JavaScriptConverter 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
사용자 지정 형식 변환기용 추상 기본 클래스를 제공합니다.
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 되지 않는 관리되는 형식에 대한 직렬화 및 역직렬화 프로세스를 구현할 수 있습니다. serialization 및 deserialization 프로세스에 대한 더 많은 제어가 필요한 경우에도 사용할 JavaScriptConverter 수 있습니다.
이 속성은 SupportedTypes 사용자 지정 변환기가 변환기 서비스를 제공하는 형식을 나타냅니다.
인스턴스에서 사용자 지정 변환기를 사용해야 JavaScriptSerializer 함을 나타내려면 변환기를 인스턴스에 등록해야 합니다. 클래스를 JavaScriptSerializer 직접 사용하는 경우 이 메서드를 RegisterConverters 사용하여 변환기를 등록해야 합니다. 그렇지 않은 경우 ECMAScript(JavaScript)에서 웹 메서드를 호출하고 사용자 지정 변환기를 사용하려는 경우 구성 파일에 요소를 추가하여 converters 등록할 수 있습니다. 자세한 내용은 방법: Microsoft Ajax에서 ASP.NET 서비스 구성을 참조하세요.
인스턴스가 JavaScriptSerializer 사용자 지정 변환기가 등록된 형식을 serialize하는 경우 serializer는 메서드를 호출합니다 Serialize . 마찬가지로 인스턴스가 JavaScriptSerializer JSON(JavaScript Object Notation) 문자열을 역직렬화하고 JSON 문자열 내의 형식에 연결된 사용자 지정 변환기가 있음을 인식하는 경우 serializer는 메서드를 Deserialize 호출합니다.
구현자 참고
상속하는 JavaScriptConverter의 경우에는 다음 멤버를 재정의해야 합니다.
Deserialize(IDictionary<String,Object>, Type, JavaScriptSerializer)
SupportedTypes JavaScriptSerializer 의 ConvertToType<T>(Object) 구현자가 사용할 메서드를 JavaScriptConverter제공합니다. 변환기 코드는 serializer가 전달하는 사전에 포함된 값을 가져와서 해당 값을 형식
T의 개체로 변환할 수 있어야 합니다. 이를 위해 사용자 지정 변환 코드를 다시 구현하는 대신 메서드를 호출할 ConvertToType<T>(Object) 수 있습니다.
생성자
| Name | Description |
|---|---|
| JavaScriptConverter() |
JavaScriptConverter 클래스의 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| SupportedTypes |
파생 클래스에서 재정의되는 경우 지원되는 형식의 컬렉션을 가져옵니다. |
메서드
| Name | Description |
|---|---|
| Deserialize(IDictionary<String,Object>, Type, JavaScriptSerializer) |
파생 클래스에서 재정의되는 경우 제공된 사전을 지정된 형식의 개체로 변환합니다. |
| Equals(Object) |
지정된 개체가 현재 개체와 같은지 여부를 확인합니다. (다음에서 상속됨 Object) |
| GetHashCode() |
기본 해시 함수로 사용됩니다. (다음에서 상속됨 Object) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| Serialize(Object, JavaScriptSerializer) |
파생 클래스에서 재정의되는 경우 이름/값 쌍의 사전을 작성합니다. |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |