Tagged: Asp.net MVC

Which JSON Serializer to use?

This is an easy one, but just want to make a note for easy reference. JSON is more ubiquitous than ever these days in ASP.Net web development. From AJAX enables web sites to MVC AJAX, JSON is “the” data-interchange format we want to use.

There are 2 possible class libraries that you can use in ASP.net to serialize objects to JSON and back. If you are using WCF and your custom objects are marked as [Data Contratcs], then DataContractJsonSerializer (System.Runtime.Serialization.Json) is the class you want to use. But if you are using an object that you cannot mark as a DataContract (may be part of a third-party component), then you want to use JavaScriptSerializer (System.Web.Script.Serialization).

Here is some code snippets that shows both approaches:

/* Using JavaScriptSerializer */
//Serialize		
String s;
JavaScriptSerializer ser = new JavaScriptSerializer();
ser.MaxJsonLength = Int32.MaxValue;
s = ser.Serialize(obj);

//De-Serialize
JavaScriptSerializer ser = new JavaScriptSerializer();
ser.MaxJsonLength = Int32.MaxValue;
obj = ser.Deserialize(json);
return (obj);



/* Using DataContractJsonSerializer */

//Serialize
DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());

//De-Serialize
T obj = Activator.CreateInstance();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
obj = (T)ser.ReadObject(ms);
ms.Close();

Happy programming!

Build a web site using ASP.Net MVC – Part 1

At last, I have decided to get my hands dirty with some real project using ASP.Net MVC. This will give me an opportunity to explore various features that are offered with ASP.Net MVC extension. There are lots
of articles available to read about this online. I’ve been reading all these time and now its time to do something more than reading …

How to prepare for development?

    Install .Net Framework 3.5 SP1 – If you do not have it yet – get it from here.
    Install Visual Studio 2008 Service Packs – Good to have..
    Install ASP.Net MVC 1.0 – Get it from here.

Like I mentioned earlier in this note, there are a lot of articles out there, so I have decided to pick the best for my reading.

1. Scott Guthrie’s Blog
2. Asp.net MVC Official Site

I am planning to use these 2 great resources, to build the site. Guthrie has build a tutorial and a sample site named Nerd Dinner which I think should give me a good starting point.

This is it for now, I will posting notes as we progress…
Lot of reading and research to do…

Cheers!