Friday, March 25, 2011

JSON with extention method, Json Serialization

Extension method allows you to add new method to the existing class without modifying the code, recompiling or modifying the original code.

public static class ExtensionsMethods
{

public static string Reverse(this string content)
{
char[] charArray = new char[content.Length];
int len = content.Length - 1;
for (int i = 0; i <= len; i++)
{
charArray[i] = content[len - i];
}
return new string(charArray);
}


/*Now suppose you want to have some functionality ToJason from all your custom object, you dont have to modify the source code, in the extention method class you just need to add the following line.*/

public static string ToJASON(this object myObject)
{

// you need reference of System.Web.Script.Serialization;
JavaScriptSerializer js = new JavaScriptSerializer();

return js.Serialize(myObject);
}
}


Now lets look at how to use the method

Suppose you have a object called Client

Client client = new Client();
client.Address = "Client's House";
client.ClientCode = "000C123";
client.ClientId = 98;
client.ClientName = "Client the God";
client.Email = "ac@gmail.com";

string jsonClient = client.ToJASON();
Response.Write(jsonClient);

the json out put will be :

{"ClientId":98,"ClientName":"Client the God","ClientCode":"000C123","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":null,"Email":"ac@gmail.com","Address":"Client\u0027s House"}

// now get a list of client. and make a jasonstring.
string jsonClients = MasterDTO.GetClients().ToJASON() ;
Response.Write(jsonClients);

the output will be
"}[{"ClientId":0,"ClientName":" Client - 0","ClientCode":"0010","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 0Client - 0","Email":" 0Client0@gmail.com","Address":null},{"ClientId":1,"ClientName":" Client - 1","ClientCode":"0011","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 1Client - 1","Email":" 1Client1@gmail.com","Address":null},{"ClientId":2,"ClientName":" Client - 2","ClientCode":"0012","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 2Client - 2","Email":" 2Client2@gmail.com","Address":null},{"ClientId":3,"ClientName":" Client - 3","ClientCode":"0013","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 3Client - 3","Email":" 3Client3@gmail.com","Address":null},{"ClientId":4,"ClientName":" Client - 4","ClientCode":"0014","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 4Client - 4","Email":" 4Client4@gmail.com","Address":null},{"ClientId":5,"ClientName":" Client - 5","ClientCode":"0015","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 5Client - 5","Email":" 5Client5@gmail.com","Address":null},{"ClientId":6,"ClientName":" Client - 6","ClientCode":"0016","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 6Client - 6","Email":" 6Client6@gmail.com","Address":null},{"ClientId":7,"ClientName":" Client - 7","ClientCode":"0017","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 7Client - 7","Email":" 7Client7@gmail.com","Address":null},{"ClientId":8,"ClientName":" Client - 8","ClientCode":"0018","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 8Client - 8","Email":" 8Client8@gmail.com","Address":null},{"ClientId":9,"ClientName":" Client - 9","ClientCode":"0019","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 9Client - 9","Email":" 9Client9@gmail.com","Address":null},{"ClientId":10,"ClientName":" Client - 10","ClientCode":"00110","ClientType":null,"Organization":null,"ContactPerson":null,"Phone":" 10Client - 10","Email":" 10Client10@gmail.com","Address":null}]


Similarly you also can convert the JSON string back to an object form by using the following extention method.

public static object JASONToObject(this string myObjectString)
{
JavaScriptSerializer js = new JavaScriptSerializer();

return js.DeserializeObject(myObjectString);
}

No comments:

ETG Consultancy

Web Designing Development Analysis & Promotion
Asp.Net 2.0 SQL WWF WCF SEO Marketing Ajax JQuery NHibernate MVC