JavaScriptSerializer.Serialize Method

Definition

Converts an object to a JSON string.

Overloads

Serialize(Object)

Converts an object to a JSON string.

Serialize(Object, StringBuilder)

Serializes an object and writes the resulting JSON string to the specified StringBuilder object.

Serialize(Object)

Converts an object to a JSON string.

C#
public string Serialize(object obj);

Parameters

obj
Object

The object to serialize.

Returns

The serialized JSON string.

Exceptions

The resulting JSON 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.

Examples

The following example provides a simple illustration of how to serialize and deserialize data objects.

C#
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.Script.Serialization;

namespace ExampleApplication
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var RegisteredUsers = new List<Person>();
            RegisteredUsers.Add(new Person() { PersonID = 1, Name = "Bryon Hetrick", Registered = true });
            RegisteredUsers.Add(new Person() { PersonID = 2, Name = "Nicole Wilcox", Registered = true });
            RegisteredUsers.Add(new Person() { PersonID = 3, Name = "Adrian Martinson", Registered = false });
            RegisteredUsers.Add(new Person() { PersonID = 4, Name = "Nora Osborn", Registered = false });

            var serializer = new JavaScriptSerializer();
            var serializedResult = serializer.Serialize(RegisteredUsers);
            // Produces string value of:
            // [
            //     {"PersonID":1,"Name":"Bryon Hetrick","Registered":true},
            //     {"PersonID":2,"Name":"Nicole Wilcox","Registered":true},
            //     {"PersonID":3,"Name":"Adrian Martinson","Registered":false},
            //     {"PersonID":4,"Name":"Nora Osborn","Registered":false}
            // ]

            var deserializedResult = serializer.Deserialize<List<Person>>(serializedResult);
            // Produces List with 4 Person objects
        }
    }
}

It requires a class named Person, which is shown in the following example.

C#
namespace ExampleApplication
{
    public class Person
    {
        public int PersonID { get; set; }
        public string Name { get; set; }
        public bool Registered { get; set; }
    }
}

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 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 i druge verzije
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

Serialize(Object, StringBuilder)

Serializes an object and writes the resulting JSON string to the specified StringBuilder object.

C#
public void Serialize(object obj, System.Text.StringBuilder output);

Parameters

obj
Object

The object to serialize.

output
StringBuilder

The StringBuilder object that is used to write the JSON string.

Exceptions

The resulting JSON 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.

output is null.

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 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 i druge verzije
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