Friday, July 6, 2012

C# JSON serialize and deserialize?

For those who are new to JSON (JavaScript Object Notation), you may have seen all sorts of articles suggesting different serializers...

Sometimes you are working with a data table... sometimes with a serializable object.

But why?  Do you need a separate serializer? What for?

You could use the Built-In .Net one for ASP.net (3.5 and above), or you can roll your own, or you can use one built by someone else like JSON.net    You need to consider what is best for your personal scenario.

http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net

JSON can be used to take an object, or array of objects with serializable properties and put them into JavaScript dictionary objects or array of JavaScript dictionary objects.

Serialized Data - from a string... This can be stored in a file, a table column etc.  It then can be pulled and assessed as serialized data.  When serializing an object, this is the result you may get.  Take notice of the nesting:

Or when passing data between systems...  or from web service to a JavaScript query of the web service, you get something like:

1{
2    "some_number": 108.541,
3    "date_time""2011-04-13T15:34:09Z",
4    "serial_number""SN1234"
5    "more_data": {
6        "field1": 1.0
7        "field2""hello"  
8    }
9}

[ reference above stackoverflow.com link for more details]



Now if you want to access/use this data... using C# code behind

1using System.Web.Script.Serialization;
2
3var jss = new JavaScriptSerializer();
4var dict = jss.Deserializestring,dynamic>>(jsonText);
5
6Console.WriteLine(dict["some_number"]); //outputs 108.541
7Console.WriteLine(dict["more_data"]["field2"]); //outputs hello
[ reference above stackoverflow.com link for more details]


Now if you want to access/use this using JavaScript:

You can either use JavaScript to pull the data from a XML Web Service - OR
you can Response.Write the serialized data to the page.

You normally don't need to build a JSON "serialized" string of an object yourself.  You can use JavaScriptSerializer or one built by another vendor... to do this.

Also, JavaScript loves to use the serialized data - and can bind them into serialized objects.  This can be a single object, or a list of objects.  You can dynamically write the JSON data into the page... and assign to a root variable....   Or you can pull JSON data from a web service.  You don't even need to Deserialize the data for this!  You just need to uncomment the section at the top that says:  [System.Web.Script.Services.ScriptService]  and the web service will send you a serialized object - in JSON format when requested using JavaScript.

example: http://williamsportwebdeveloper.com/cgi/wp/?p=494

------------------------

If this works, then why ever use a commercial product like JSON.Net?

Reason: some commercial products are better at serializing some types of data, handling nulls, and may be fasterhttp://json.codeplex.com/  But Asp.Net 4.0 can handle many peoples needs natively.