Thursday, April 7, 2011

Javascript Modules and Objects– Parsing JSON text

A JSON is a lightweight data-interchange format that is easy for machine to parse and generate and easy for humans to read and write. You can parse JSON string in various language like C#, C++ etc.
 
A JSON string is in the form of {<name>:<value>}.
Following is an example how JSON object is created.   
//## To Create a JSON OBJECT
// A JSON object is created using syntax 
// var objName={<name>:<property>};
var myEntity={
    name:'name1',
    age:25,
    sex:'male',
    dob:'18/06/1980',
    printName:function(){
      alert("Printing name "+name);  //this won't work as JSON object behave like module.
      alert("Printing name with module prefix "+myEntity.name); // this work as JSON object behave like module.
    }
};

The above example shows how to build JSON by using simple entity style.

But what about creating JSON by string parsing. there are two methods of the same

Method 1

eval("var stJSON={ 'name':'abc','age':'23'}");

This is pretty nice but have drawback of having slow performance due to eval and also is easily a candidate for malicious activity.

Method 2

var strJson="return {'name':'abc','age':'23'}";
var strToJsonEntity=new Function(strJson)();

This will do the same task but uses a pretty fine technique. It works by creating a Function which returns the newly created object by passing the body as string. We execute this function to return the newly created object. This is pretty fine technique but require understanding of how Function works.

A JSON object can not have further instance members hence the following call will fail.

var myEntityInstance=new myEntity();

This also means that JSON objects doesn't have prototype property and we can easily validate this by checking this.

alert(myEntity.prototype); //results to null.

Hope this introduction will help us in better understanding JSON behavior in javascript.

Sunday, August 1, 2010

Douglas Crockford on javascript

Douglas Crockford is one of my favorite javascript expert. He has conducted a lot of trainings around javascript. A good source of learning javascript for everyone is to watch his series on javascript namded The complete series. It really gives a lot of information about limitation and power of javascript. The same can also be watched in yahoo developer network.

Friday, August 22, 2008

A Really Powerful Vector Javascript Library

Raphaël is a small JavaScript library that should simplify your work with vector graphics on the web. In case you want to create your own specific chart or image crop-n-rotate widget, you can simply achieve it with this library. Raphaël uses SVG and VML as a base for graphics creation. Because of which every created object is a DOM object so you can attach JavaScript event handlers or modify objects later. Raphaël’s goal is to provide an adapter that will make drawing cross-browser and easy. Currently library supports Firefox 3.0+, Safari 3.0+, Opera 9.5+ and Internet Explorer 6.0+.