Documentation for: ASP.NET Ajax Version 1.0

This documentation is for a previous version. For the current released version, see the ASP.NET Ajax documentation on MSDN.

 

 

 

 

 

 

 

JavaScriptSerializer Class

Provides serialization and deserialization functionality for Microsoft ASP.NET AJAX applications.

Syntax

CSharp

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class JavaScriptSerializer

VisualBasic

<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class JavaScriptSerializer

ManagedCPlusPlus

[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class JavaScriptSerializer

JSharp

/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class JavaScriptSerializer

Members

IconMemberDescription
JavaScriptSerializerNew()Initializes a new instance of the JavaScriptSerializer class with no type resolver.
JavaScriptSerializerNew(JavaScriptTypeResolver)Initializes a new instance of the JavaScriptSerializer class with a custom type resolver.
ConvertToType<(Of T>)(Object)Converts the given object to the specified type.
Deserialize<(Of T>)(String)Converts the specified JavaScript Object Notation (JSON) string to an object of type T.
DeserializeObject(String)Converts the specified JavaScript Object Notation (JSON) string to an object graph.
Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize()Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetHashCode()Serves as a hash function for a particular type. (Inherited from Object.)
GetType()Gets the Type of the current instance. (Inherited from Object.)
MaxJsonLengthGets or sets the maximum length of JavaScript Object Notation (JSON) strings that are accepted by the JavaScriptSerializer class.
MemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
RecursionLimitGets or sets the limit for constraining the number of object levels to process.
RegisterConverters(IEnumerable<(Of JavaScriptConverter>))Registers a custom converter with the JavaScriptSerializer instance.
Serialize(Object)Converts a given Object to a JavaScript Object Notation (JSON) string.
Serialize(Object, StringBuilder)Serializes a given Object and writes the resulting JavaScript Object Notation (JSON) string to the specified StringBuilder.
ToString()Returns a String that represents the current Object. (Inherited from Object.)

Remarks

Although the JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server, you cannot access that instance of the serializer programmatically. However, this class is provided as a public API, so that you can use it directly when you want to work with JavaScript Object Notation (JSON) in managed code.

To serialize an object, invoke the Serialize method. To deserialize a JSON string, invoke the Deserialize<(Of T>)(String) or DeserializeObject(String) methods. To serialize and deserialize types that are not natively supported by JavaScriptSerializer, implement custom converters by using the JavaScriptConverter class, and then register the converters by using the RegisterConverters(IEnumerable<(Of JavaScriptConverter>)) method.

Mapping Between Managed Types and JSON

The following table shows the corresponding mapping between managed types and JSON for the serialization process. These types are natively supported by JavaScriptSerializer. When deserializing from a JSON string to a managed type, the same mapping applies. However, deserialization is asymmetric in its behavior. Not all serializable managed types can be deserialized from JSON.

note

A multidimensional array is serialized as a one-dimensional array; you should use it as a flat array.

Managed type

JSON equivalent

String (UTF-8 encoding only).

String.

Char.

String.

Single null char (such as, \0 ).

Null.

Boolean.

Boolean. Represented in JSON as true or false.

Null values (null object references and Nullable value types).

A null value string.

DBNull.

A null value string.

Primitive numeric (or numeric compatible) types: Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Double, Single. The culture invariant string representation is used.

Number.

DateTime.

Date object. Represented in JSON as "\/Date(number of ticks)\/". The number of ticks is a positive or negative long value that indicates the number of ticks (in milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

The maximum supported date value is MaxValue {12/31/9999 11:59:59 PM} and the minimum supported date value is MinValue {1/1/0001 12:00:00 AM}.

Enumerations.

Integer equivalent of the enumeration value.

Types that implement IEnumerable, or IEnumerable that are not also implementations of IDictionary or IDictionary. This includes types, such as Array, ArrayList and List.

Array that uses JSON array syntax.

Types that implement IDictionary or IDictionary. This includes types, such as Dictionary and Hashtable.

JavaScript object that uses JSON dictionary syntax.

Custom concrete types that have public instance properties with get accessors and/or public instance fields.

Note that public write-only properties, public property or public field attributes with ScriptIgnoreAttribute and public indexed properties in these types are ignored.

JavaScript object that uses JSON dictionary syntax. A special metadata property “__type” is included to ensure correct deserialization.

Guid.

ToString().

Uri.

GetComponents(UriComponents, UriFormat).

Examples

The following example demonstrates how to use the JavaScriptSerializer class to save and restore the state of an object using JSON serialization.

Permissions

  • AspNetHostingPermission

    for operating in a hosted environment. Demand value: LinkDemand. Associated enumeration: Minimal.

  • AspNetHostingPermission

    for operating in a hosted environment. Demand value: InheritanceDemand. Associated enumeration: Minimal.

Inheritance Hierarchy

Object

  • JavaScriptSerializer

Assembly: System.Web.Extensions (Module: System.Web.Extensions)