Peter Keating

Developer from the New Forest in the South of England.

Native JSON with Flex 3.5 and AIR 3


When Adobe released AIR 3 & Flash Player 11 one of the features introduced was native JSON parsing. If you have got your hands dirty with JSON before while developing for AIR you have probably either used the JSON decoding / encoding classes included in as3corelib or maybe developed your own classes to do the job. Whatever method, I would have a strong hunch that a native JSON approach will be considerably quicker. Switching from the as3corelib classes to native JSON for parsing there were significant performance improvements with the native JSON approach being considerably faster. The API for using native JSON is simple and a good example is given in Chris Cantrell's blog post if you want to know more about implementation. When performing maintenance on an older project at work we switched to native JSON due to improve the performance of parsing a large quantity of JSON. The older project was built using Flex 3.5 with AIR 2.0 SDK and wasn't as straight forward to get native JSON working, this blog post will go through the steps to get it working.

The first step was to upgrade to the AIR 3.0 SDK. We decided to go with AIR 3.0 as this was the lowest version that has support for native JSON. All the available versions of the AIR runtime & SDK can be downloaded from a handy archive page on the Adobe website. Installing the SDK is a simple case of copying the contents of the downloaded zip into the Flex SDK directory that it should be applied to (make sure you take a backup of the Flex SDK first just in case things go wrong). To check this has worked, open your IDE and type "JSON", intellisense should match a class in the default package. If you don't have intellisense then a building the project and getting no errors is also a suitable check.

Debugging the application that now uses the JSON class from the default package throws an exception that is displayed below.

ReferenceError: Error #1065: Variable JSON is not defined.

Googling the error above offers many solutions geared towards Flex 4.x. Adding -swf-version 13 to the command line arguments seemed to be the most suggested and successful answer to the problem. Unfortunately -swf-version isn't supported in Flex 3.5 SDK, resulting in an "unknown configuration variable" error. After a bit of investigating into the compiler arguments that are available to mxmlc application compiler I tried the -target-player argument also using "13" as the value. In order to set compiler arguments in Flash Builder simply right click your project select "Properties" then select the "Flex Compiler" option and at the bottom is an input box for "Additional compiler arguments", as shown in the screenshot below.

screenshot-native-json-compiler-args

This did bring a fresh problem as the application was unable to run. I was getting a popup error dialog with the following error message, "error while loading initial content". This was happening because the application manifest was targeting AIR 2.0, but it needs to target AIR 3.0 because of the -target-player compiler argument is including AIR 3.0 features into the application. The application manifest file is an XML file that can be found in the src directory of your project, likely to be named YourProjectName-app.xml. All that needs to be done is to change the version number at the end of the url in the root <application> node. Below is what the changed application manifest should look like. If your current URL is targeting 2.0 then you will also have to change the <version> node to <versionNumber>, be careful though as AIR 3.0 expects the version number to only contain numbers and full stops (e.g. 2.1.1).

<application xmlns="http://ns.adobe.com/air/application/3.0">  
    ...
</application>

Final note is that if any application users have an AIR runtime that is less than 3.0 their AIR runtime will be automatically updated to the latest available runtime when they install the application.

I hope this post is useful to someone, the aim was to make a post that I would have found useful when trying to solve the issue.

Back to Posts

-->