Editar

Compartilhar via


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.

public:
 System::String ^ Serialize(System::Object ^ obj);
public string Serialize (object obj);
member this.Serialize : obj -> string
Public Function Serialize (obj As Object) As String

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.

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
        }
    }
}
Imports System.Web.Script.Serialization

Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim RegisteredUsers As New List(Of Person)()
        RegisteredUsers.Add(New Person With {.PersonID = 1, .Name = "Bryon Hetrick", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 2, .Name = "Nicole Wilcox", .Registered = True})
        RegisteredUsers.Add(New Person With {.PersonID = 3, .Name = "Adrian Martinson", .Registered = False})
        RegisteredUsers.Add(New Person With {.PersonID = 4, .Name = "Nora Osborn", .Registered = False})

        Dim serializer As New JavaScriptSerializer()
        Dim 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}
        ' ]

        Dim deserializedResult = serializer.Deserialize(Of List(Of Person))(serializedResult)
        ' Produces List with 4 Person objects
    End Sub
End Class

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

namespace ExampleApplication
{
    public class Person
    {
        public int PersonID { get; set; }
        public string Name { get; set; }
        public bool Registered { get; set; }
    }
}
Public Class Person
    Public Property PersonID As Integer
    Public Property Name As String
    Public Property Registered As Boolean
End Class

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

Serialize(Object, StringBuilder)

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

public:
 void Serialize(System::Object ^ obj, System::Text::StringBuilder ^ output);
public void Serialize (object obj, System.Text.StringBuilder output);
member this.Serialize : obj * System.Text.StringBuilder -> unit
Public Sub Serialize (obj As Object, output As StringBuilder)

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