in

ASP.NET Weblogs

Cristian Odea

Microsoft Ajax JavaScript Converters

I just joined the ASP.NET blogging community, being one of the 150 newly added bloggers by Joe Stagner,
so here's my first post.

I'd like to share my last research, about implementing custom JavaScript converters.

If you're using web services with Asp.Net Ajax, the custom entities moved back/forward between client side
and server side are converted/populated back to/from a JSON representation. It seems the ASP.NET runtime
checks whether a custom JavaScript convertor is registered for the custom entity type and if there is
none found, it will the objects of this type to JSON using reflections.

Custom JavaScript convertors can be registered in web.config file, under the <system.web.extensions>
section:

web.config

The JavaScriptConverter class has three abstract members which should be implemented:

JavascriptConverter

Implementing custom converters increases the performance of the serialization process, but what if the
model has a lot of entities, with a lot of properties ? it would take a lot of time to develop converters
to support all of the types in the model. There is, of course, the code generation option, but almost each
change in the model would require a regeneration of the converters. A better approach would be to generate
these converters at runtime. I'm not exactly an expert in IL code, but let's just analyze this code using
Reflector:

il

Well, there is more code in this method than in the image above, but it doesn't look too cute, does it ? I don't
know about you, but I really like C# syntax more. Luckily, there is this namespace which can help me a bit
with it: System.Reflection.Emit, so I'll just reproduce the above code using the read only fields of the OpCodes
class. I'll generate a dynamic type, implementing the IJsConverter interface:

IJsConverter

After this type is created, we'll keep an instance of it in a static Dictionary<Type, IJsConverter> variable
and call the Serialize and Deserialize methods whenever they are requested by the ASP.NET runtime:

DynamicConverter

I am not going to cover the details of IL generation, I'll move on to performance testing results.
Serializing 1,000,000 records:

  • Default ASP.NET serializer: 26.813 seconds
  • Custom serializer: 15.922 seconds
  • Dynamic IL serializer: 16.500 seconds.

Of course, in the development process we have to find the best balance between performance, maintenance
costs and development costs (hmm, maybe those last 2 are a management issue).

Published Jan 23 2008, 11:17 PM by crissianx
Filed under: , , ,
Attachment: AjaxSerializer.zip

Comments

 

rrobbins said:

Bleh! Don't use images to show your code. It is virtually unreadable. By the way, do you know of any visualizers for JSON representation?

January 23, 2008 4:38 PM
 

crissianx said:

I use Nikhil's web development helper from projects.nikhilk.net/.../WebDevHelper.aspx

January 23, 2008 4:50 PM
 

rrobbins said:

I just found a JSON Viewer at www.codeplex.com/.../ProjectReleases.aspx

This is awesome! It is like XML Spy for JSON and will help me to figure out the JavaScript object model for the data I receive.

January 23, 2008 4:53 PM

Leave a Comment

(required)  
(optional)
(required)  
Add