Typed AS3 JSON Encoder and Decoder? Typed AS3 JSON Encoder and Decoder? json json

Typed AS3 JSON Encoder and Decoder?


JSON is built in in AS3. The preferred method to transmit data over the wire is AMF, which does provide you typed objects.

If you have to use JSON, then I guess that you might have to do with some sort of custom protocol to be able encode/decode with types.

You would actually need a reflection utility that read beans in JSON format and then produce your object. It really depends on how deep you want to go.

as3Commons has a reflect package that could help. They also have a JSONTypeProvider, which is not exactly what you need but can put you in the right tract.

You could modify any of the IOC frameworks to produce the context by parsing JSON instead of the regular XML most of them use.

You could modify ASON and add a custom type parser. You would have to send a variable in your JSON object containing the type of the object. And use that in with flash.utils.getDefinitionByName.

Another approach would be to just parse the objects with a regular JSON parser and then if it has a defined type create an instance of that objet, and initialize the properties.

Something like this, to get you started:

var beanInfo:Object = JSON.decode( jsonString );beanInfo = _parseBean( beanInfo );private function _parseBean(beanInfo:Object):Object{    if ( beanInfo.hasOwnProperty("_type") ) {        var clazz:Class = getDefinitionByName( beanInfo._type ) as Class;        beanInfo.__clazz = clazz;        var instance:Object = new clazz;        for( var prop:String in beanInfo ) {            if( instance.hasOwnProperty(prop) ) target[prop] = _getPropertyFrom(beanInfo[prop]);        }    }}private function _getPropertyFrom(property:String):* {    var xml:XML = describeType( beanInfo.__clazz );    //find the type of the current property.    var type:String = xml...    //if is a simple object then do something like    switch( type ) {        case "number":            return parseFloat(property ) as Number;        break;        case "int":        case "uint":            return parseInt( property );        break;        case "string":            return property as String;        break;        ...        default            //As it is it does not suppor complex objects.           //You would use reflection. But then you could save the whole switch...        break;    }}


Flash has its own serialization system.

var serializer:ByteArray = new ByteArray();serializer.writeObject(new Sprite());serializer.position = 0;var data:String = serializer.readUTFBytes(serializer.bytesAvailable);trace(data); //Will show you the binary jibberish

You can use registerClassAlias to add support for custom classes.


JSON doens't really define a means to convey type information. It's just strings and ints and arrays and so on. So basically you need some sort of "pickle" for AS3 that's based on JSON. I would suggest you look into Flex/Flash remoting solutions and see how they package objects to be transmitted for RPC; you might be able to modify that solution to use JSON. I'm actually doubtful you'll find a library like this. Does it have to be JSON? I'm pretty sure there are XML based libraries that do this.