comparing JSON and xml using Web Service returns data

31. December 2009

Web Services, where XML is difficult to process on the client side. Also, XML is larger in size than its corresponding JSON representation. For each property, XML has two tags: opening and closing. On the other hand, JSON has the property name only once.JSON is a lightweight data-interchange format. Like XML, it is human-readable, platform independent, and enjoys a wide availability of implementations. JSON is a subset of the object literal notation of JavaScript. Data represented in JSON can be parsed by JavaScript easily, so it is ideal for AJAX based web applications. For your information, JSON does not have any support for datetime as JavaScript doesn't have its own datetime data type. Rather, datetime in JavaScript is an object. You can read here details on how JSON datetime is handled in AJAX.

 

If a Web Service returns XML data, then the data to be passed from the server to the client is larger than what it would be in the case of JSON. So, using JSON, we only need to pass fewer amounts of data. Also, XML parsing on client side is cumbersome and costly in the perspective of processing. If there’s another easier way, then why should not we use that? Let’s assume the following XML data is returned by the Web Service.



<Employee>
<EmpID>100</EmpID>
<Name>Aamir Hasan</Name>
<Address>Studentacad.com</Address>
</Employee>



To parse data on the client-side, you’ll need to use JavaScript XML features.

On the other hand, if the Web Service returns JSON format, then the same data is represented as:



{"EmpID":100,"Name":Aamir Hasan,"Address": Studentacad.com}


 

 

we can see that the JSON representation is much smaller in size than its equivalent XML representation. Also, we’ll see later how easy it is to parse JSON formatted data. 


Define JSON serializer  in web.config


he JSON serializer described later is only supported in .NET framework 3.5

<section name="jsonSerialization"
type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions,
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
requirePermission="false" allowDefinition="Everywhere"/>

 

 


By default, the ScriptJsonSerializer limits the JSON string to a length of 102400 (UTF-8) characters.
If you need to change the maximum length of the JSON string, then change the maxJsonLenght property of JsonSerialization.
In the section shown below, the max length is changed to 80000 characters:


<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="80000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>

 

Make the object serializable


[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetProductsJson(string prefix)
{
List<Product> products = new List<Product>();
if (prefix.Trim().Equals(string.Empty, StringComparison.OrdinalIgnoreCase))
{
products = ProductFacade.GetAllProducts();
}
else
{
products = ProductFacade.GetProducts(prefix);
}
//yourobject is your actula object (may be collection) you want to serialize to json
DataContractJsonSerializer serializer = new DataContractJsonSerializer(products.GetType());
//create a memory stream
MemoryStream ms = new MemoryStream();
//serialize the object to memory stream
serializer.WriteObject(ms, products);
//convert the serizlized object to string
string jsonString = Encoding.Default.GetString(ms.ToArray());
//close the memory stream
ms.Close();
return jsonString;

Original Content

 

http://www.codeproject.com/KB/webservices/JsonWebServiceJQuery.aspx 

 

 

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

Ajax, ALL, asp.net, Javascript, JQuery , , , ,

Nested Master Pages

31. December 2009

 

 

 

 

 

 

 

 

Nested Master Pages - Example 1. Tutorial Created  by Aamir Hasan

1. We have 2 master pages, "Parent.master" and "Child.master".

2. "Child.master.Master" is a child of "Parent.Master".

3. All .ASPX pages are inherited from "Child.master".







Parent.master.aspx
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Parent.master.cs" Inherits="Parent" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<p>
parent</p>
<form id="form1" runat="server">
<div>
<p>
Hi, Parent Master Page
Studentacad.com
</p>
<asp:ContentPlaceHolder id="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>

</form>
</body>
</html>

Parent.master.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Parent : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{

}
}






child.master.aspx
<%@ Master Language="C#" MasterPageFile="~/Parent.master" AutoEventWireup="true" CodeFile="child.master.cs" Inherits="child" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">

<p>
Hi, Child Master Page
Studentacad.com
</p>

<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>

child.master.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class child : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{

}
}




default.aspx Page

<%@ Page Title="" Language="C#" MasterPageFile="~/child.master"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
</asp:Content>

 

Original content from Microsoft ASP.NET Site
http://www.asp.net/(S(sf10gzjodvrpce55el2p5cnk))/learn/master-pages/tutorial-10-cs.aspx


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0 , ,

Serialize Any .NET Object to a JSON String

31. December 2009

The following steps explain how to serialize any .NET object into a JSON string. This tip uses the .NET 3.0 Framework. You'll find this capability particularly helpful in applications that use AJAX to pass objects from the server to the client code.

Assume you have a Person object defined as shown below:

public class Person

{

  public int ID { get; set; }

  public string FirstName { get; set; }

  public string LastName { get; set; }

}

Now assume you need to serialize this object to a JSON string to pass it to a client-side script. It turns out you can easily convert an instance of the Person class (or any .NET object) to a JSON string, by using an "extension method" (a capability released with .NET 3.0). To create a JSON serialization extension method, use the following code:

using System.Web.Script.Serialization;

namespace JSON.Namespace

{

  public static class JSONHelper

  {

     public static string ToJSON(this object obj)

     {

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        return serializer.Serialize(obj);

     }

  }

}

The JavaScriptSerializer class (in System.Web.Script.Serialization) handles the actual object-to-JSON conversion. Note that the ToJSON() extension method above is defined for type Object, which means you can use it with all .NET objects. This means that in addition to calling ToJSON() on a Person object, you could call it the same way on a collection of Person objects or any other .NET datatype. The following examples show how to use the ToJSON() method:

using JSON.Namespace; //Reference the namespace that contains ToJSON()

 

Person person = new Person() {

   ID=1, FirstName="Dev",

   LastName="Jackson" }; //Create a Person object

   //Convert to a JSON string

   string JsonString = person.ToJSON();

The output of the preceding code would be:

[{"ID":1, "FirstName":"Dev", "LastName":"Jackson"}]

Conveniently, VS 2008 provides Intellisense and compile-time support for extension methods, just as if they were built-in methods.


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

Ajax, ALL, asp.net , , ,

JSON Hijacking and How ASP.NET AJAX 1.0 Avoids these Attacks

31. December 2009

Recently some reports have been issued by security researchers describing ways hackers can use the JSON wire format used by most popular AJAX frameworks to try and exploit cross domain scripts within browsers.  Specifically, these attacks use HTTP GET requests invoked via an HTML <script src=""> include element to circumvent the "same origin policy" enforced by browsers (which limits JavaScript objects like XmlHttpRequest to only calling URLs on the same domain that the page was loaded from), and then look for ways to exploit the JSON payload content.

ASP.NET AJAX 1.0 includes a number of default settings and built-in features that prevent it from being susceptible to these types of JSON hijacking attacks.  Below are some details of how these attacks are mitigated:

ASP.NET AJAX Web Methods do not enable HTTP GET requests by default

Script files loaded via an HTML <script src=""> element within a browser can only be retrieved via HTTP GET verb requests.

By default ASP.NET AJAX's web services layer does not allow web methods to be invoked via the HTTP GET verb. For example, assume a developer writes a web service method like below:

 

Original Article

http://weblogs.asp.net/scottgu/archive/2007/04/04/json-hijacking-and-how-asp-net-ajax-1-0-mitigates-these-attacks.aspx

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

Ajax, ALL, asp.net , , ,

Software write instructions

31. December 2009

Here are some guidelines to help you to write clear instructions:

  • Prefix the instructions with a clear heading that summarises the task.
  • Show clearly who does what. If a process involves more than one person, write a different set of instructions for each person.
  • Start each instruction with a verb that tells the reader to do something. Examples: "Open the valve…", "Press the emergency button…", "Tell your supervisor…"
  • Use a numbered list when the order is important. Use a bulleted list (like this list) when the order is not important (for example, when the reader can choose between different options).
  • Put notes and warnings at the start of the instructions, or before the list item to which they refer.
  • Specify conditions before the primary part of the instructions. For example, at step 5 of some stocktaking instructions, do not write, "Before you start the stocktake, make sure that…" (This type of problem frequently occurs.)
  • Do not mix instructions with conceptual information. Give the necessary background information before the instructions.
  • Write for your audience and use a level of detail that is suitable to their skill level.
  • Avoid lists of more than approximately ten steps. If possible, divide a long list of instructions into two or more different tasks.
  • Specify what the reader does when the task is complete. If a reader asks, "Now what?", the instructions are not complete.


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL



User Name: Guest

Your Ip: 38.107.191.92
Time: