cinqetdemi.JSON: A better JSON parser for AS2, suited for config files

Posted on Saturday 20 May 2006

Although satisfied with my solution to the conendrum of configuration files with the plist parser, it suddenly hit me that I may have been using the wrong solution all along. While plist does preserve ActionScript types, you still have to "translate" actionscript statements into XML. Since this current project I'm working requires a lot of exploratory work, and then upon approval, cleaning up, I found myself doing the same thing over and over, that is, hard-coding configuration into ActionScript, and then translating to XML and using the external configuration. While better than having to parse the XML, an ideal solution would be to be able to cut and paste the ActionScript into an external config file. And so the idea of a JSON config file came to mind.

Now there is a JSON parser on json.org for AS2, however it has the major disadvantage of not being compatible with MTASC. So I rewrote a part of it to compile under MTASC. I then added the provision that keys inside objects don't need to be wrapped in quotes (like traditional object notation in ActionScript). I also added hexadecimal number parsing, single quotes surrounding strings in addition to double-quotes, better error messages, and an optional trailing comma (,) in objects and arrays as in Python (makes adding properties faster).

You can download the updated JSON parser here.

Also available in the zip file is a JSONConfig.as file similar to the previous Config.as file. Use like so:


        import cinqetdemi.JSONConfig;
        import mx.utils.Delegate;
       
        var config:Config = JSONConfig.getInstance();
        config.addEventListener('load', Delegate.create(this, onConfigLoad));
        config.addEventListener('error', Delegate.create(this, onConfigError));
        config.loadConfig();
       
        function onConfigLoad()
        {
            trace(config);
            trace(JSONConfig.getConfig()); //A shortcut with static access
        }
       
        function onConfigError()
        {
            trace('Gosh darn it!');
        }
 

The config file is by default loaded from config.py and not config.js, since my JavaScript editor whines about the trailing commas but not the Python editor I'm using. An example configuration file would look like this:


{
 whiteAsSnow:0xffffff,
 aDeliciousNumber:3.141509,
 pythonIngredients:
 [
   'Spam',
   'Eggs',
 ],
 flashPeople:
 {
   flashguru:"Guy Watson",
   jesterXl:"Jesse Warden",
   mesh:"Mike Chambers",
 },
}
 

Note the trailing commas, as in Python, making adding properties and rearranging easier (note however that this is entirely optional). All in all, I think this provides a very natural way of creating configuration files, preserving types, and using a syntax exactly the same as ActionScript, providing global access through a Singleton. The way I'm using it in my current project is that the config file contains information about strings in the local language, styles, and the location of the Remoting gateway. Thus JSON is used for initialization information, and Remoting for further data communication, a combination not too far from ideal. Enjoy.


WordPress database error: [Can't open file: 'wp_comments.MYD'. (errno: 145)]
SELECT * FROM wp_comments WHERE comment_post_ID = '197' AND comment_approved = '1' ORDER BY comment_date

No comments have been added to this post yet.

Leave a comment




Your e-mail address is never displayed. If you run into issues with SpamKarma blocking you, email me at $patrick->5etdemi(com)


RSS feed for comments on this post | TrackBack URI