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

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 on the lis

How to avoid refreshing of page on clicking on BUTTON in jQuery?

Suppose you might have created a button on page.When you click on that button without page refresh some other custom things have to happen. But instead of that, on clicking on the button the page is getting refreshed. How to avoid this?.. Simple, The button click event has default event as refreshing the page. The solution is to "prevent the default event". $("#ButtonID").Click(function(e) {   e.preventDefault(); //Write your custom code here } ); Hope this may help Cheers Pradeepa Achar

This solution contains invalid markup or elements that cannot be deployed as part of a sandboxed solution. Solution manifest for solution 'cc218449-78b3-4430-b401-4884977560e2' failed validation, file manifest.xml, line 11, character 4: The element 'Solution' in namespace 'http://schemas.microsoft.com/sharepoint/' has invalid child element 'TemplateFiles' in namespace 'http://schemas.microsoft.com/sharepoint/'. List of possible elements expected: 'FeatureManifests, ActivationDependencies' in namespace 'http://schemas.microsoft.com/sharepoint/'. Troubleshoot issues with Microsoft SharePoint Foundation. Correlation ID: oplh6595a-a6hg-416f-a85f-a173bp03dda6 Date and Time: 30/10/2013 13:33:18

By looking at this bug you may get shocked and may not understand why this is happening. I can say, you have developed a wsp of farm solution, not the sandboxed solution  using visual studio and added some OOTB webparts such as visual webparts and trying to deploy the solution via solution gallery directly on to the sharepoint site. It is so simple.Understand that, when you create a .wsp file using visual studio, you deploy as farm solution. But when you try to deploy the same solution from site settings-> solution gallery, you deploy it as sand boxed solution. In this situation ,the "TemplateFiles" elements refers to the items that will be copied into the web servers. This is allowed only in farm solutions , not in sandboxed solutions. I suggest you guys to deploy the solution either using stsadm command or powershell command, so that it will work fine.If anyone wants to know about how to deploy using powershell command you can refer the   techNet  document for