I spent the majority of yesterday evening and this morning debugging what seemed to be JSON’s inability to properly decode even simply formulated nested objects and arrays.

We decided for the first time in our Flash department to rely quite heavily on JSON for data provision on a large project that shall for the time being, remain nameless. The framework for the project is a hybrid site who’s use of Flash will be consistent, but will also do all the “thinking” for Flash. We decided in the early planning stages of this project that this would be the most efficient and dependable modular route to take. The vision is that our Flash peices (built in AS3 and Flash 9) will have various default actions, but otherwise all instruction and data will come via FlashVars and ExternalInterface.

I’ve been working on the shell and base classes for the primary Flash peice for this site, ironing out the abstract classes and functionality we’ll be using with all the flash peices on the site. My initial testing required me to set up my own JavaScript calls, including a test JSON object made up of simple data that would resemble the real data to be passed in at later time.

Immediately upon reading the JSON-encoded string in Flash and decoding it with Adobe’s as3corelib JSON package, my traces revealed that the JSON string being passed in to my ActionScript was not being properly decoded. What seemed, on trace, as a perfectly well formed JSON serialization looked like this:

{"modules":"[\"module0\",\"module1\",\"module2\",\"module3\"]","basePath":"resources/swf/base.swf"}

The data represented here is simple, only an object containing a String property called “basePath” and a Array of Strings called “modules”. The modules Array was at first an Array of Objects, but I narrowed down the complicated data structure when I first began experiencing errors. Attempting to decode this JSON string resulted in an Object in ActionScript that contained two properties of the correct namespace, but the Array would throw runtime errors, claiming “Type Coercion failure”. Nothing unusual in AS3, but it did catch me off guard having been expecting ease with JSON. A trace of the properties of the decoded JSON object revealed the correct “basePath” String, but the “modules” Array was still in encoded format as such:

["module0","module1","module2","module3"]

Debugging, idea after idea proved fruitless, and even simplifying the process showed little result. Typecasting, double decoding, checked code validations, and simplifying structure continued to produce an un-decoded nested Array and nested Objects being passed into AS3.

Finally after discussing with our JavaScript/CSS developer I switched to a different encoder. Previously for my test file I had downloaded the “json2.js” file found on json.org’s list of libraries. This class used a function called “stringify” to encode the JSON object and the file was the only JavaScript encoder I could find. Jeremy’s suggestion was to switch to the MooTools JSON class which would be used in the final project anyway. I can’t say why I didn’t use it in the first place other than having been unaware of its availability.

Sure enough, switching to MooTool’s JSON.encode function suddenly “turned on all the lights”, as you could say and the Flash framework functioned properly without errors. Somehow there is apparently a difference between the encode by MooTools and the stringification by the JSON.org JavaScript file. I have not spent much time in finding what that difference is other than to notice the disappearance of escape slashes in the MooTools encoded string once passed into Flash.

json.org/json2.js :: {"modules":"[\"module0\",\"module1\",\"module2\",\"module3\"]","basePath":"resources/swf/base.swf"}


MooTools json :: {"modules":["module0","module1","module2","module3"],"basePath":"resources/swf/base.swf"}

I know too little about proper JSON serialization to speculate further into the proper encoding of the objects, but it is obvious that MooTools quite handily made my day. Perhaps I was using json2.js’s stringify function for something other than its recommended use but I would have a hard time believing such when its the only obvious encoding JS file available on json.org.

Jeremy, the JS/CSS developer seems to be quite in love with MooTools. His usual statement is that “those MooTools guys are smart. They fixed JavaScript.” At least today, I’ll agree.