Sals Hacker Space

Sal's Hacker Space

Monday, February 11, 2013

Thought Clusters - JSON

Recently I've become busy with a lot of work projects. In the course of doing my work, Ive had ideas of different tools i could build to make my life easier (next time i have a similar project). So these tools have taken up a significant amount of my time away from my "blog" projects but hopefully ill be able to share some of the tools here (ill have to find out about that).

Even though work has kept me busy, there still is something new and technically cool that i learn every week. So why not write about that when im not writing about my projects? Ill call these posts thought clusters, because they are not meant to build upon each other, rather these posts are meant to stand on their own talking about some aspect of technology.

This post, well talk about JSON.

What is JSON and why should i care?

JSON stands for Javascript Object Notation.  From the name you must have guessed that it is native to Javascript. It's a lightweight and portable format used for data interchanging.

You can think of it as a format to represent data (objects/arrays etc). JSON is becoming increasingly popular over XML since it requires less markup and its output is more readable for humans and machines as well as not as lengthy.

Being a hardware guy i probably would never have come across JSON if i wasnt interested in video game programming. Ive recently dived into learning libgdx, which is a videogame development framework (i told you that i wasnt kidding when i said im juggling a lot of things at the same time!)

I realized that many of the people using libgdx would serialize data in JSON format whenever they would create a save or a player profile etc. Its a preferred method of reading and writing data as its smaller than XML as well as faster and easier to parse. So thats why i care about JSON.

Please note that while i did mention that JSON is native to JavaScript, it is still language independent. There is a plethora of JSON parsers for many different programming languages.

Okay but how is it structured?

JSON is structured primarily using name value pairs. Let's illustrate this with the help of an example. Lets say we are making a video game and its a video game about ninjas (we all love ninjas). The ninja object has the following fields.

 String name = "Ryu";   
 String giColor = "BLACK";   
 int sobrietyLevel = 7; // Ever seen drunken fist? our ninja likes his drink.  
 List<String> weapons = { "shuriken", "sword", "spear" }; 

So in JSON this object would first be surrounded by {} braces. All objects are represented by curly braces.

The JSON representation would be as follows:

 {  
    "name": "Ryu",  
    "giColor": "Black",  
    "sobrietyLevel": 7,  
    "weapons": [  
      "shuriken",  
      "sword",  
      "spear"  
    ]  
  }  

As you can see the whole object is represented by curly braces, the variables are represented as keyvalue pairs. The array is represented with the use of square brackets.

Keep in mind that im comparing Java notation with Javascript so the conversion is possible with a suitable parser.

This post is meant to be a brief introduction to JSON. If it piqued your interest and you want to read more, please refer to the following links

http://www.json.org/
http://code.drewwilson.com/entry/an-introduction-to-json

No comments:

Post a Comment