JavaScriptSerializer.ConvertToType Method

Definition

Overloads

ConvertToType(Object, Type)

Converts the specified object to the specified type.

ConvertToType<T>(Object)

Converts the given object to the specified type.

ConvertToType(Object, Type)

Converts the specified object to the specified type.

C#
public object ConvertToType (object obj, Type targetType);

Parameters

obj
Object

The object to convert.

targetType
Type

The type to convert the object to.

Returns

The serialized JSON string.

Exceptions

The resulting JSON-formatted string exceeds the value of MaxJsonLength.

-or-

obj contains a circular reference. A circular reference occurs when a child object has a reference to a parent object, and the parent object has a reference to the child object.

The recursion limit defined by RecursionLimit was exceeded.

Remarks

When the JavaScriptSerializer instance is serializing a type for which a custom converter is registered, the serializer calls the Serialize method to obtain the dictionary of name/value pairs that will be converted to a JSON-formatted string.

The Serialize method can also throw exceptions if the object graph is too complex, or if registered instances of JavaScriptConverter have caused converter recursion.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 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

ConvertToType<T>(Object)

Converts the given object to the specified type.

C#
public T ConvertToType<T> (object obj);

Type Parameters

T

The type to which obj will be converted.

Parameters

obj
Object

The object to convert.

Returns

T

The object that has been converted to the target type.

Exceptions

obj (or a nested member of obj) contains a "__type" property that indicates a custom type, but the type resolver that is associated with the serializer cannot find a corresponding managed type.

-or-

obj (or a nested member of obj) contains a "__type" property that indicates a custom type, but the result of deserializing the corresponding JSON string cannot be assigned to the expected target type.

-or-

obj (or a nested member of obj) contains a "__type" property that indicates either Object or a non-instantiable type (for example, an abstract type or an interface).

-or-

An attempt was made to convert obj to an array-like managed type, which is not supported for use as a deserialization target.

-or-

It is not possible to convert obj to T.

obj is a dictionary type and a non-string key value was encountered.

-or-

obj includes member definitions that are not available on type T.

Examples

The following example shows how to use the ConvertToType method to obtain a ListItem object from a value in the dictionary that is passed to the converter. This code example is part of a larger example provided for the JavaScriptSerializer class.

C#
ArrayList itemsList = (ArrayList)dictionary["List"];
for (int i=0; i<itemsList.Count; i++)
    list.Add(serializer.ConvertToType<ListItem>(itemsList[i]));

Remarks

The ConvertToType method tries to convert the object instance that is represented by obj to an instance of type T. During this conversion there is no guarantee that object reference equality is maintained. Therefore, you cannot assume that obj and T refer to the same object.

ConvertToType is intended to be used if you implement a class that derives from JavaScriptConverter. Converter code must be able to take a value that is in the dictionary that the serializer passes to it, and then convert that value to an instance of type T. Instead of re-implementing the custom conversion code to perform this task, you can call the ConvertToType method from the converter code.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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