JSON (Lite)

For a current Flex project we use JSON to transfer data between the application and the backend. Adobe Labs has a JSON serialization library called corelib that includes MD5 hashing, JSON, and other parsing tools.  What if you need just JSON for your Flex or Flash AS3 application development?

There is already a JSON ActionScript file for AS2 available at JSON.org. This file would be great for your older Flash projects and maybe some mobile application development.   Initially, I tried to convert this file to AS3, but then I figured someone probably already did this.   After some extensive searching and help from Lar (as always), we were able to locate a version of JSON that had been converted to AS3 for a project called VEGAS.      

There was no stand alone version of JSON for AS3, we had to extract the JSON pieces from the project files.  I did a little touch up for the error messaging, but it is essentially the same JSON for AS2, just updated to work with AS3.  In fact, it is by the same author, ekameleon.    

I am including all the links to the original source information as well as a stand alone version of the JSON AS3 ActionScript file and Flex Library file with the compiled SWC (called JSONLite).   The total size of the JSON AS3 file is 5KB!   

JSON AS3 (Flash/Flex)

JSON SWC (Flex)

To use the file just add the SWC to your Flex Library path.    In the MXML file, import the file and call to serialize/deserialize the same way you do with the corelib JSON SWC:

import com.serialization.json.JSON;

var dataString:String = JSON.serialize(object);

var obj:Object = JSON.deserialize(string);

I don’t support these files, I am just passing the information along and doing some experimentation with other JSON libraries. Also note that using the JSONLite SWC or class file file instead of the corelib file won’t significantly lighten your Flex applications. Flex only compiles the packages and class files you reference in your application from the library. So the post title is a bit misleading because you only shave off that 1KB. However, I have heard from others that this class file is faster than most other JSON parsers which does give it a slight advantage if speed is a concern.

-Mister

12 Comments

  1. Yeah, the link is in the first paragraph. I know there is already a JSON AS3 Library offered by Adobe. I just like to have the option to use JSON on it’s own without using the entire SWC library. I wanted to have the ActionScript class file on it’s own as well as a SWC file that only included JSON serilization/deserialization.

  2. I have been playing with this library for a while and I really like it.

    But it does not parse booleans or null values, e.g:

    { “a”: false, “b”:null }

    both fail 😦

    Am I doing this correctly? I will keep an eye on these comments, so if anyone can help, please chime in!!

    Thanks,

    monk.e.boy

  3. Monk.E.Boy
    shouldn’t it be:
    eg.

    var blah=[
    {a:”hello”,b:1,c:true,d:null},
    {a:1,b:”there”,c:function(blah){/*etc…*/alert(‘oye’);},blueberry:”pudding”},
    {adobe:”posers”}
    ];

    Is not the first one supposed to be the object? I may be wrong; yet, that is how I have been doing it for a long time with dynamic script tags for AJAJ.

  4. Pure JSON can’t include functions, only some implementations like in ecmascript allow to define any object (function is an object of class Function).

  5. HI All,

    I am also using the inbuilt library. But when the records is more than 5k-6k it is taking around 25-30sec to decoding data. Which will degrade the performance.

    Any suggestion.

Comments are closed.