Native JSON support in AIR 3

One the most exciting things in the latest releases of the Flash runtimes, is the ability to natively work with JSON. Now, I know that the Stage3D feature might be a bit more visually compelling, but any Flash application developer usually has to work with JSON data. If you are not familiar with what JSON is, it stands for Javascript Object Notation, and is quickly becoming the standard method of data transfer with modern web services (like Twitter).

Here is an example of an JSON data file:

{
   "firstName": "Chris",
   "lastName": "Griffith",
   "education":
   {
      "elementary": "Franklin",
      "jrhighSchool": "Curran",
      "highSchool": "Bakersfield High",
      "college": "University of California, Santa Barbara"
   }
 }

One of the attractions of using JSON as your data type is it’s readability of the data. So having native support, and hence native performance in working with the data, is a great benefit to developers.

JSON.parse()

This is the method that will ingest the JSON data and create the native data structure. Here is a brief code snippet that demonstrates loading a JSOn file and tracing out the contents.

var json:URLLoader;
 var parsedJSONData:Object;
function loadMyJSONData():void {
   json = new URLLoader();
   json.addEventListener(Event.COMPLETE, parseJSON);
   json.load(new URLRequest("data.json"));
   trace("Loading JSON file...");
}
function parseJSON(evt:Event):void {
   trace("JSON file loaded successfully!");
   trace("Parsing JSON...");
   trace("RESULTS:"); 
   parsedJSONData = JSON.parse(json.data);
   trace("firstName: " + parsedJSONData.firstName);
   trace("lastName: " + parsedJSONData.lastName);
   trace("education.elementary: " + parsedJSONData.education.elementary);
   trace("education.jrhighSchool: " + parsedJSONData.education.jrhighSchool);
   trace("education.highSchool: " + parsedJSONData.education.highSchool);
   trace("education.college: " + parsedJSONData.education.college);
 }

So, you can see that with one line of ActionScript, you have native JSON parsing.

To convert your data into native JSON, it is as simple as calling JSON.stringify(). This method takes in three parameters. The first parameter is the data that you want to convert. There are restrictions to the types of data that can be converted into JSON data. These are Arrays,Strings, Numbers, Booleans, and the null value. The second parameter is an optional filter function or array. You can use this remove elements that are not supported in the JSON format. The final parameter sets the level of spaces that is added before each data pair (thus making it readable).

Here is a simple example:

var myDataObject:Object = new Object();
myDataObject.firstName = "Chris";
myDataObject.lastName = "Griffith";
myDataObject.hasCollegeDegree = true;
var newJSON:Object = JSON.stringify(myDataObject, null, 4);

As someone who often consumes lots of JSON data into my applications, these features are one of these little ‘wins’ that make the next version of the Flash Platform an attractive solution for my development.

Advertisements

4 comments

  1. I’ve encountered a problem with jSON which was that data i got back to AIR could diff alot depending on requests made from AIR.

    Say if i get this in one request:
    {
    “user”: “danel”,
    “languages”:
    {
    “flash”: “AS3, AS2”,
    “web”:”html,php,js”
    }
    }

    and another request gives me this:

    {
    “user”: “melin”,
    “languages”:
    {
    “web”: “php, html, js”
    }
    }

    as you see second person misses flash param in json.

    So if same function is triggered when data is loaded i can not access json.languages.flash in the second call since there is no such param.

    what would be the safest and most effective way to see if param is there in json before accessing it ?

    I remember i used if-case before accessing that param to see that it is really there. But that gave me lots of code and if-cases which i thought to be a ugly-fix and not the ultimate way.

    if(json.languages.flash){
    user.flash = json.languages.flash
    }

    Another point worth to mention is that say if the second request contains no such param as languages at all… in that case would the if-case throw an error cause there is no such param as json.languages.

    to use if-cases to see if params exists would make me to code hundreds of if cases for each params… equals to nasty code 😛

  2. Since the native JSON.parse just creates generic Objects, you can use the hasOwnProperty() method on the object to test if it exists. One can debate if the data structure should be constant from request to request, or only send properties that have values.The later case will produce smaller files, but place a burden on the developer to handle the “if the property is not there” case.

    Here some sample ActionScript that would handle the data that you outlined:

    var myObject:Object = new Object();
    myObject.user = “danel”;
    var myLanguages:Object = new Object();
    myLanguages.flash = “AS2,AS3”;
    myLanguages.web = “html,php,js”;
    myObject.languages = myLanguages;

    if (myObject.languages.hasOwnProperty(“flash”)) {
    trace(“true”);
    } else {
    trace(“false”);
    }

    myObject = new Object();
    myObject.user = “melin”;
    myLanguages = new Object();
    myLanguages.web = “php, html, js”;
    myObject.languages = myLanguages;

    if (myObject.languages.hasOwnProperty(“flash”)) {
    trace(“true”);
    } else {
    trace(“false”);
    }

    Now I do assume there is a languages element in my example. To be completely safe, I should first check that there is a language property first, then if there is a flash property.

  3. Can you use Native JSON in a web application?? of course the security access is enabled and taken care of?
    I do a lot of Socket comms in my apps.. So I am wondering if I can use JSON going forward.

    Thanks,
    James

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s