JSON in Classical Web Services ASMX

10.23.2009

Most of the websites we see these days have great user experience by providing asynchronous postbacks and means of data transport has been JSON. Even though most of us have already migrated to new versions of .NET, there are still some people using the older versions. This post is about supporting JSON in Classical Web Services (.asmx).

Add a new ASMX web service file as usual. Then decorate your class with [System.Web.Script.Services.ScriptService] attribute as shown below.

You can also convert your own objects (classes) to string by calling the Helper method easily.

The other alternative i.e. without using the Newtonsoft.Json could also be done. Instead of returning string one could return the Object itself. For instance let’s assume we have the class Person as below.

And let us also assume that we have a method called JsonHelper.CreateSamplePerson() which return a new instance of the Person class. We could then write it as below. Notice the return type is Person instead of string.

We would get the following result for the ReturnComplexObject() where the return type is string. Notice that it is not fully JSON as it contains mixture of XML. this is because the content-type is not set to application/json.

When the content-type is set to application/json the following result is obtained.

It now gives a JSON string.

Now for the ReturnComplexObjectRaw() which returns the Person the following result would be obtained. (The object is converted automatically to JSON.)

What is the difference of returning converted JSON string using the Newtonsoft Json and or the autoserializer returning the Person Object?

First of all as you can see the autoserializer from done by .NET adds one more field called __type. This isn’t much of a problem though. __type is added only if the Object is complex. For simple types such as string, it is not added.

Secondly the result returned by .NET autoserializer returns JSON object while from Newtonsoft it return a JSON string which needs to be converted to JSON object manually (more of this in the coming posts). That is why you can see a lot of \” in the result by Newtonsoft. Its actually an escape sequence character for the string.

OK. But which method to choose then?

Well, that is quite a tough question. But from my experience I have choosen to use Newtonsoft by serialize the object and returning it as a string rather than using the concrete object. This was due to the fact that, some of my classes doesn’t contain a constructor without any parameters (default constructor) such as below.

It doesn’t have a constructor. Then if I return Student in my Web Service it would throw me the yellow screen of death. Where as using the Newtonsoft serialization it would work great. The another reason is that in WCF to serialize an object to JSON using DataContract it needs both get and set properties, but the LastName in Student class doesn’t have set property. This would not include LastName in the serialize JSON string which was not what I wanted. So the safer way for me was to use Newtonsoft.JSON.

These preferences are from my own perspectives. If you find that it is not good to use Newtonsoft over .NET default serialization or have a better one, please feel free to leave a comment.