JavaScriptConverter Class

Definition

Provides an abstract base class for a custom type converter.

C#
public abstract class JavaScriptConverter
Inheritance
JavaScriptConverter

Examples

The following example shows how to create a custom converter for the ListItemCollection class.

C#
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;
        }
    }
}

Remarks

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 Microsoft 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 Implementers

When you inherit from JavaScriptConverter, you must override the following members:

Constructors

JavaScriptConverter()

Initializes a new instance of the JavaScriptConverter class.

Properties

SupportedTypes

When overridden in a derived class, gets a collection of the supported types.

Methods

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.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from 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.

(Inherited from Object)

Applies to

Proizvod Verzije
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also