Sunday, October 01, 2006

JSON is about to become the latest new tool in my IT toolkit.

My last web project had me hand-writing my own C# code to store lists of object properties in a hidden text field. Then JavaScript code to parse this information.

JSON is going to save me doing any of that work and let me pass arrays of objects between serverside code and JavaScript and back again.

I use the JSON parser/stringifier in AJAX.NET professional on the clientside to store an array of my C# objects into a hidden text field.

In the JavaScript I eval the hidden text field and his gives me back the array of objects complete with all the properties.

So now I can add new objects to the array in JavaScript, use JSON stringifier ( I use the one at www.JSON.org ) to put the array back into the hidden text field. Now my server code can use the Ajax.Net pro JSON parser to create an array of C# objects.

Only a couple of issues I found so far :
1. Eval is considered evil because it will execute any JavaScript code. www.json.org has a safer method call to do this, but it doesnt seem to work with ajax.net created JSON.

2. Ajax.net JSON Doesnt seem to work very well if I try and store dotnet DateTime type in objects, so I will avoid doing this.

No comments: