|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionIn this article we’ll examine the ASP.NET AJAX serialization process. We will examine the server-side methods and client-side methods which serialize and deserialize objects. What is JavaScript Object Notation (JSON)?JavaScript Object Notation or JSON provides a more efficient means of transferring data than previously found with XML and SOAP. JSON is a means of basically serializing an object into a lightweight string that can be sent across the wire. For example, consider the following C# class: public class User
{
string FirstName;
string LastName;
}
Now consider an instance of the object as follows: User usr = new User();
usr.FirstName = "John"
usr.LastName = "Smith";
When serialized into JSON formatted text we’ll be left with the following: { FirstName : "John"
LastName : "Smith" }
From within JavaScript we can deserialize the JSON text string into an object using the JavaScript supplied method Getting DirtyThe object responsible for serialization/deserialization is the public class JavaScriptSerializer
{
// Fields
internal const int DefaultMaxJsonLength = 0x200000;
internal const int DefaultRecursionLimit = 100;
internal const string ServerTypeFieldName = "__type";
// Methods
static JavaScriptSerializer();
public JavaScriptSerializer();
public JavaScriptSerializer(JavaScriptTypeResolver resolver);
public T ConvertToType
First off, the serialization process will fail if the number nested objects are greater than that defined within the The object is serialized into a StringBuilder object, which after serialization has been completed, the string representation will be returned. The majority of the action happens within the private The public abstract class JavaScriptTypeResolver
{
// Methods
protected JavaScriptTypeResolver();
public abstract Type ResolveType(string id);
public abstract string ResolveTypeId(Type type);
}
The public override Type ResolveType(string id)
{
return Type.GetType(id);
}
public override string ResolveTypeId(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
return type.AssemblyQualifiedName;
}
Finally, the this._converters = new Dictionary
That is, the Type of the object is the key, while the value is an instance of a The
A custom object is serialized similar to an IDictionary, with a few differences. If a Let us now examine the serialization process by way of an example object. Considering the objects defined below. public class Customer
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private string _email;
public string EmailAddress
{
get { return _email; }
set { _email = value; }
}
private Phone _phoneNumber;
public Phone PhoneNumbers
{
get { return _phoneNumber; }
set { _phoneNumber = value; }
}
}
public class Phone
{
private string _homePhone;
public string HomePhone
{
get { return _homePhone; }
set { _homePhone = value; }
}
private string _workPhone;
public string WorkPhone
{
get { return _workPhone; }
set { _workPhone = value; }
}
}
If this object were returned from a web service method, the object would be serialized automatically from within the JavaScriptSerializer jsSerializer = new JavaScriptSerializer(new SimpleTypeResolver());
Customer cust = new Customer();
cust.FirstName = "Joe";
cust.EmailAddress = "jknown@domain.com";
cust.PhoneNumbers = new Phone();
cust.PhoneNumbers.HomePhone = "888-888-8888";
string serializedText = jsSerializer.Serialize(cust);
Notice that the JavaScriptSerializer object has been initialized with the SimpleTypeResolver which as you know, will be invoked to evaluate the type of the serialized object to a string. The serialized JSON text is shown below. {"__type":"Customer, App_Web_plrzlwbj,
Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null","FirstName":"Joe","LastName":null,
"EmailAddress":jknown@domain.com,
"PhoneNumbers":{"__type":"Phone, App_Web_plrzlwbj, Version=0.0.0.0,
Culture=neutral, PublicKeyToken=null",
"HomePhone":"888-888-8888","WorkPhone":null}}
Notice that the PhoneNumbers property is of the custom type Phone, hence when serialized the value of the PhoneNumbers property will be a JSON object, which is the serialized version of the Phone object, again notice the type is identified, which is used when deserialized so that the correct object may be created and populated. That said, let us now examine the deserialization process. The deserialization process is performed using the To deserialize a JSON string we may invoke the Customer cust = jsSerializer.Deserialize
ConclusionThat’s all there really is to it. We should now have a basic understanding of how the AJAX JSON serialization/deserialization process works and how we can leverage JSON serialization for use within our AJAX code. The process is really quite simple.
|
||||||||||||||||||||||||||||||||||||||||||||||||||