Skip to main content

[Solution] :How to not serialize the __type property on JSON object

While working on ajax- webservice , most of the time we prefer return value from web service method should be in JSON format . If we expect a web service method to return the value as JSON object then it will display few information about the source code through the __type property. Also this property is additional load for response. We ideally need to avoid this property

Have a look at the below image in which it is returning JSON object which has my custom properties along with __type property.


















Here i have used burp suite penetration testing tool to test the loophole in our software. Through fiddler i have got the information about request /response  and wanted to hide error message which gets generated from exception. Usually stack trace may allow hackers to  retrieve error message which  contains code related information by using which hacker can easily find the loophole to hack the function.So when  exception arises hacker should not be able to know the information about source code.

Remember, in order to access the web application we don't need to open the site through browser . Jquery/javascript related code will run on browser level .So we should not do any severe validation on client side script. It should be done at server level code.

In the above image ,  my code is sending __type property value which is exposing my source code information. So I need to avoid that property to be exposed.

Solution:
There might be n number of solutions. My solution is, in the web service method itself return the value in simple JSON string format . On the client side code (jquery) we can convert this string as JSON object for further manipulation of data to display on client side(browser).

Assume i am using Structure to maintain the different kind of data after retrieving from database, never return it as JSON object. It will add __type property . If you are using NewtonSoft library for JSON serialization then you need to follow the following instruction


  1. Assume you have created a structure(STRUCT keyword)  to hold the returned values from database of different datatypes .
  2. Now you have retrieved the value from database and added to structure object, let's say resItems
  3. now you have to return as JSON serialized string as return new JavaScriptSerializer().Serialize(resItems);  
  4. The webservice method's return type should be string data type
     public string getMydata(parm1,param2)
      {
        /*your code to retreive value form database*/
        /* retrieved values will be saved in resItems object*/

       return new JavaScriptSerializer().Serialize(resItems);  
      }


now returned values will be JSON string .Now in the jquery code you need to convert the JSON string to JSON object for further easiest manipulation of data to display on browser

Assume in success method of ajax in jquery the returned value will be present under object.d. Here object is any variable name. but d is JSON schema
      Now you need to convert the JSON string to JSON object as follows.

/*Here rest of the code for ajax such as data, request type,error etc*/
success: function(res)
{
            var items = res.d;
    items=jQuery.parseJSON(items);
}

in the above code jQuery.parseJSON(JSON_string) will convert the json string to json object so that we can use the object for further usage in  jquery code



Also if we want to retrieve JSON object from server side code itself then the return type of websevice method should not be of public type,In the class which contains web service method , we need to call protected default constructor.


hope this may help someone.

thanks
pradeepa achar

Comments

Popular posts from this blog

error occurred in deployment step 'recycle iis application pool' :object reference not set to an instance of an object

While deploying using Visual studio, we may get an error "error occurred in deployment step 'recycle iis application pool' :object reference not set to an instance of an object" Solution:  Don't get  panic ..Simply restart the visual studio with the solution which you wanted to deploy on to the site This may help some one. -cheers pradeepa achar

The Fastest way to Export Schema of Sharepoint List

Many of my SharePoint Developer friends are struggling to get the Schema.xml file of a newly created list. The schema.xml file has the list's metadata information, by using the schema.xml, we can create the lis definition and paste this contents of Schema.xml to the Schema.xml file of the newly created List definition, so that you can deploy the list definition on another site. In order to do this, you guys might be following the procedure bu saving the site template and get the site definition solution. After this, you will open visual studio and import this solution and wait for few minutes. THIS IS A CRAPPY PROCEDURE..Why do you invest your time to get a schema of one or few list  by taking entire site definition?..If that site has lots of lists, then it will take lots of time. In my Research, i have found a the BEST  and quicker way to get schema.xml file of a particular list which you need Schema.xml. Follow the following procedure : Go to your site  click ...

The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator

You might have developed a functionality where data is being saved in a SharePoint list. By the time you developed this functionality it was working fine.As users adds the data , some day your functionality doesn't work. That means, the data which is present in the list is not being retrieved. Don't be panic. Just open the LOG file which is present in 14 hive folder. You will come to know about this bug with the statement -"The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator". Now what you have to do is, go to central administration site.  1.Click on Manage web application 2.Select the web application on which your sitecollection is created. 3.Click on General settings and select Resource throttling 4.by default list view threshold would be 5000. your list might have data more than 5000. So , increase the number to required threshold limit. you can make it 10000, 20000 like this. Do a i...