Provides an abstract base class for a custom type converter.
Namespace:
System.Web.Script.Serialization
Assembly:
System.Web.Extensions (in System.Web.Extensions.dll)
'Usage
Dim instance As JavaScriptConverter
'Declaration
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public MustInherit Class JavaScriptConverter
The JavaScriptConverter class enables you to implement serialization and deserialization processes for managed types that are not natively supported by the JavaScriptSerializer class. You can also use JavaScriptConverter when you need more control over the serialization and deserialization process.
The SupportedTypes property indicates the types for which a custom converter provides converter services.
To indicate that a custom converter must be used by the JavaScriptSerializer instance, you must register the converter with the instance. If you are using the JavaScriptSerializer class directly, you should use the RegisterConverters method to register the converter. 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. For more information, see How to: Configure ASP.NET Services in ASP.NET AJAX.
When the JavaScriptSerializer instance is serializing a type for which it has a custom converter registered, the serializer calls the Serialize method. 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.
Notes to Inheritors: When you inherit from JavaScriptConverter, you must override the following members:
JavaScriptSerializer provides the ConvertToType<(Of <(T>)>) method that will be used by implementers of JavaScriptConverter. 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 type T. Rather than re-implementing the custom conversion code to accomplish this, you can call the ConvertToType<(Of <(T>)>) method.
The following example shows how to create a custom converter for the ListItemCollection class.
Imports System
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
System..::.Object
System.Web.Script.Serialization..::.JavaScriptConverter
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5
Reference
Other Resources