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 faster. http://json.codeplex.com/  But Asp.Net 4.0 can handle many peoples needs natively.


Thursday, May 24, 2012

JSON Web Service - Asp.net DataTable

I found a great article on how to pull ADO.Net DataTable in an XML web service and return JSON table.

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

This is a VERY good read - easy to understand.  This is a good place to start when first pulling data from table... for JSON to use.

And if you want to pull JSON data from code behind, it is easy to.. If the data is only used on one page, why expose a web service api to it...

http://dotnet-treasure.blogspot.in/2011/03/bind-drop-down-list-using-json.html

Notice how it is decorated with [ScriptService]
---------------

After doing this, you may consider enhancing.

You can make an array of serializable objects... instead of a Jagged array.   Just apply the JavaScriptSerializer to it. 
http://www.codeproject.com/Articles/1789/Object-Serialization-using-C


------------------------
Putting it together...

If you are binding a Linq Query to Kendo UI, (using JSON) here is an awesome resource:

http://www.kendoui.com/blogs/teamblog/posts/12-05-11/hello_services_webapi_rest_json_and_ajax.aspx

It tells all sorts of goodies - like what references to make... etc.  It is MVC, if you use that.  If not, you have to adjust somewhat.


Tuesday, May 8, 2012

Windows Azure: Microsoft Cloud Computing

I have recently been building an app on Windows Azure.

I thought many of you would like to see some good resources to get a look/feel of what they offer.  I will add to this list as I find good resources.

Friday, April 27, 2012

Kendo UI - mobile tools

I have just completed main features for Legal Paperwork Stinks - a web based legal case management software.

I was about to start work on the mobile site for this, when I got an email from Telerik that they have Kendo UI.  They have some GREAT tools for web apps!  Now I have to redo some of my design docs... but it will be worth it.