Analysis on TypeResolver of ASP. NET

Source: Internet
Author: User

I. Client serialization and deserialization capabilities

In ASP. net ajax, The serialize static method of the Sys. Serialization. JavaScriptSerializer class is used to provide Serialization capabilities for the client. This method serializes a client object into a JSON string, which is easy to use. As follows:

 
 
  1. var jsonStr = Sys.Serialization.JavaScriptSerializer.serialize(obj); 

There is not much to say, and it may be a "characteristic" that it serializes the Date object on the client. What will happen if we call the following code?

 
 
  1. var jsonStr = Sys.Serialization.JavaScriptSerializer.serialize(new Date()); 

The result is similar to "@ 1162814090119 @". Note that there are double quotation marks on both sides. This is a special expression of ASP. net ajax for Date objects. If developers need to "splice" strings themselves, pay attention to this.

The deserialize static method of the Sys. Serialization. JavaScriptSerializer class is used to bring deserialization capabilities to the ASP. net ajax client. As follows:

 
 
  1. var obj = Sys.Serialization.JavaScriptSerializer.deserialize(jsonStr);  

In fact, it simply calls the JavaScript built-in eval method. Of course, since the Date object has a special representation method during serialization, this will also be taken into account during deserialization: Sys. serialization. the deserialize static method of the JavaScriptSerializer class sets "@... @ "" to "new Date (...) "format, which is a standard JSON string.

Ii. JavaScriptTypeResolver and JavaScriptConverter

The serialization and deserialization of the client are very simple. I put it here to show that it is more like making the content more complete. Server-side serialization and deserialization are not so easy. They involve a large number of string operations and custom capabilities. This is what I want to emphasize in this article.

The Serialization and deserialization capabilities provided by ASP. net ajax are completed by classes in the Microsoft. Web. Script. Serialization namespace. Fortunately, most of them are internal classes. Only several methods of the JavaScriptSerializer class can be used for developers. ASP. net ajax has brought us ample serialization and deserialization capabilities. We only need to master it and know how they work, which is generally enough.

However, to understand the serialization and deserialization capabilities, you must first understand the other two classes: JavaScriptTypeResolver and JavaScriptConverter.

1. JavaScriptTypeResolver

JavaScriptTypeResolver is an abstract class. Although it is the first time in multiple Release of Atlas, it is not a new thing. The function is equivalent to the IJavaScriptSerializeContext interface in Atlas CTP. It can even be said that the class name and method name are changed from an interface to an abstract class, which is confusing, because the current abstract class does not have any implementation ). This class is used to associate a string with a specific class to make the string an identifier for that specific class ". This abstract class has two methods:
1). String ResolveTypeId (Type): Obtain the ID String of the Type object.
2). Type ResolveType (String): obtains a Type object from the String identifier.

We can see that these two methods are a pair of opposite operations. They will be applied to deserialization respectively. If you are not very familiar with the role of this class, you can take a look at a simple implementation of this abstract class in ASP. net ajax. That is the Microsoft. Web. Script. Serialization. SimpleTypeResolver class. The Code is as follows:

 
 
  1. public sealed class SimpleTypeResolver : JavaScriptTypeResolver  
  2. {  
  3. public override Type ResolveType(string id)  
  4. {  
  5. return Type.GetType(id);  
  6. }  
  7. public override string ResolveTypeId(Type type)  
  8. {  
  9. if (type == null)  
  10. {  
  11. throw new ArgumentNullException("type");  
  12. }  
  13. return type.AssemblyQualifiedName;  
  14. }  

SimpleTypeResolver associates the Assembly Qualified Name of a class with a type. However, I personally think that this class should never be used. If this class is used, will the information of Strong Named Assembly be exposed? Version, Culture, PublicKeyToken, "no less than one ".

2. JavaScriptConverter

The role of the JavaScriptConverter class is to provide developers with the ability to customize serialization and deserialization. This is especially important for operations on complex objects that contain circular references. I have analyzed this class in my previous articles, and I have also used this class as an example. However, the function of this class in RTM Release is simplified. Its methods and attributes are reduced to three:

1). IEnumerable <Type> SupportedTypes: Read-only attribute. All classes supported by Converter are returned.
2). object Deserialize (IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer ):
The first parameter of this method is a Dictionary. Some friends may think that the Dictionary is very close to the JSON String Representation: It is nested by Dictionary and List, the bottom element is some basic type objects. But not in fact. ASP. net ajax deserialization of a json string, if "{" _ type ":"... ",...} "When a segment is converted to a JSON Dictionary, only the Dictionary of the basic type object exists.) If the Dictionary contains the Key" _ type, then we will try to convert it to the type represented by the _ type value at this time. That is to say, the first parameter dictionary accepted by the JavaScriptConverter Deserialize method may already be of a special type.
The second parameter is the conversion target type. The third parameter is the JavaScriptSerializer that calls the current Deserialize method. Some of our deserialization operations can be delegated to it for execution. It has already been associated with the JavaScriptConverter configured in web. config. However, it should be noted that you must avoid returning to the current Deserialize method without any change in the next operation. Obviously, this will lead to an endless loop.
3 ). IDictionary <string, object> Serialize (object obj, JavaScriptSerializer serializer): the function of this method is relatively pure. The obj object is converted into an IDictionary <string, object> object, ASP. net ajax will add the "_ type" value to this Dictionary. In this way, the current JavaScriptConverter can be used for reverse operations during deserialization.


3. Use JavaScriptTypeResolver and JavaScriptConveter

After you define JavaScriptTypeResolver and JavaScriptConverter, you also need to add them to a JavaScriptSerializer before they take effect. The code is roughly as follows:

 
 
  1. // Define a JavaScriptTypeResolver instance
  2. JavaScriptTypeResolverResolver=NewMyTypeResolver ();
  3. // Create a JavaScriptSerializer that uses the preceding Resolver
  4. JavaScriptSerializerSerializer=NewJavaScriptSerializer (resolver );
  5. // Create an JavaScriptConverter Array
  6. JavaScriptConverter []Converters=NewJavaScriptConverter [] {new MyConverter ()};
  7. // Associate Converter with Serializer
  8. Serializer. RegisterConverters (converters );
  9. // Use JavaScriptSerializer for serialization or deserialization
  10. Serializer. Serialize (...);

For the use of JavaScriptConverter, You need to mention that some configuration can be performed in the web. config file. As follows:

 
 
  1. <jsonSerialization> 
  2. <converters> 
  3. <add name="..." type="..." /> 
  4. ...  
  5. </converters> 
  6. </jsonSerialization> 
  7.  

It should be noted that some friends think that after the JavaScriptConverter configuration is implemented in web. config, these converters will be used in the use of JavaScriptSerializer by default. However, in fact, the Converter of these configurations will only be used for Web Service access. If a new JavaScriptSerializer is created, it needs to be re-allocated to make the JavaScriptConverter take effect.

  1. WebRequestExecutor in ASP. NET
  2. IIS6 ASP. net isapi request processing process
  3. Backup in ASP. NET
  4. Introduction to ASP. NET ISAPI
  5. Iis isapi extension of ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.