SYSK 282: How JSON is Making Your Code Cleaner


Say, you have to pass in a large number of data elements into a web service (to me, more than 5 is “many”).  You have a few options – pass them as individual parameters, put them all into one custom serializable class (e.g. MyMethodRequest object), pass them as XML… you get the picture.


 


With the introduction of JSON about a year and a half ago, there is a new option.  Here is how your code might look like when using AJAX.NET and JSON:


 


1.     Define the web service:


<asp:ScriptManager ID=”ScriptManager1″ runat=”server” EnablePartialRendering=”true”>


<Services>


         <asp:ServiceReference Path=”~/Services/YourService.asmx” />


</Services>


</asp:ScriptManager>


 


2.    Create web method parameter using JSON notation (name/value pair) and invoke web service from Javascript:


var data = {


            “param1” : param1Data,


            “param2” : param2Data,


            “param3” : param3Data,


            “param4” : param4Data,


            “param5” : param5Data,


            “param6” : param6Data };    


 


YourService.YourMethod(data,


    OnWebMethodCompleted, OnWebMethodError, OnWebMethodTimeout);


 


function OnWebMethodCompleted(result)


{


   // TODO: add your code


}


 


function OnWebMethodError(result)


{


   // TODO: add your code


}


 


function OnWebMethodTimeout(result)


{


   // TODO: add your code


}


 


3.    In your web method implementation, you’d process the data as follows:


[WebService(Namespace = “http://yourcompany.org/”)]


[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


[System.Web.Script.Services.ScriptService]


[System.Web.Script.Services.GenerateScriptType(typeof(YourReturnDataClass))]


public class YourService : System.Web.Services.WebService


{


   public YourReturnDataClass YourMethod(IDictionary data)


   {


         YourReturnDataClass result = null;


 


if (data != null)


            {


                foreach (string key in data.Keys)


                {


                        switch (key.ToLower())


                  {


                              case “param1”:


                                    // TODO: add handling logic


                              break;


 


case “param2”:


                                    // TODO: add handling logic


                              break;


 


                                                .  .  .


}


}


}


   }


}


 


 


Here are some of the advantages of this solution:



  1. The parameter order is irrelevant

  2. You can pass in only the needed parameters instead of all data elements in the canonical schema (thus saving bandwidth and processing time)

  3. If you need to add elements that should be passed in, you don’t need to do anything special – just add the handling logic in your code behind and your code should work with clients of both versions (assuming the new parameter is not required or has a default value)

  4. If you need to remove an element, just delete the logic in web method that deals with that parameter – no need to change client (although it’s always good to keep client & server code in-sync)

  5. JSON is a more compact format than XML

 


 


NOTE: Since JSON supports nesting, you can create as complex data objects as need to.


 


If interested, you might want to visit my post SYSK 217 where I talked about an opposite problem — processing a web service response using JSON. 


 


 

Comments (0)

Skip to main content