Cast JSON as a custom ActionScript object? Cast JSON as a custom ActionScript object? json json

Cast JSON as a custom ActionScript object?


In AS3, you cannot cast a dynamic object to a custom class using as or CustomClass(customObject).

You can, however, use some simple tricks as a workaround.For example, you could declare a constructor for your custom class accepting an Object and initializing it's members with the object properties.

You would then use:

var customObject:CustomClass = new CustomClass(JSON.decode(evt.result as String));

PS. Regarding the comments, this is not true for every language... I guess that makes it actionscript specific.


Per se, this is not possible. And that has nothing to do with ActionScript. In most other languages you have the same problem, since on the left side you have an anonymous object, if the language supports any such thing, or a hash.Anyway. There are different solutions, this would be one, that can handle a few things:

package  {    public class ObjectUtils {        public static function createInstance(constructor:Class):* {            var ret:*;            switch (describeType(to).factory.constructor.parameter.(@optional == "false").length()) {                case 0: ret = new to(); break;                case 1: ret = new to(null); break;                case 2: ret = new to(null, null); break;                case 3: ret = new to(null, null, null); break;                case 4: ret = new to(null, null, null, null); break;                case 5: ret = new to(null, null, null, null, null); break;                case 6: ret = new to(null, null, null, null, null, null); break;                case 7: ret = new to(null, null, null, null, null, null, null); break;                case 8: ret = new to(null, null, null, null, null, null, null, null); break;                case 9: ret = new to(null, null, null, null, null, null, null, null, null); break;                          default: throw new Error("no implementation for instantiating classes that require more than 9 constructor arguments");            }            return ret;        }        public static function castAnonymous(obj:Object, to:Class):* {            var ret = createInstance(obj);            for (var name:String in obj)                try {                    ret[name] = obj[name];                }                catch (e:Error) {                    throw new Error("error trying to assign value " + obj[name] + " to property " + name + " on " + ret + ". reason: " + e);                }            return ret;        }                   }   }

restrictions:

  1. will fail if your class panics, if it is spammed with nulls upon construction, or the constructor needs more than 9 arguments
  2. does not, and also cannot recurse, so it may simply assign anonymous objects or arrays to the returned instance's properties

hope it helps anyway ;)

greetz

back2dos


Actually - you can kinda hack around this restriction using the build in parser and overriding the JSON.parse method and taking advantage of the scope of the anonymous function to access an object in the parent function.

For instance - check out this code snippet

public dynamic class MutatorData extends Object{    public var DisplayName:String;    public var PropertyName:String;}public function parseData( data:String ){    var mutator:MutatorData = new MutatorData();    JSON.parse( data,        function (k, v) {        mutator[k] = v;        });}

With the code sample as is - the MutatorData class has to be declared dynamic, which ALMOST defeats the purpose of creating a class for it. You wont be able to prevent other coders from adding properties to it, spelling mistakes in your JSON string will become properties on the object. Evenso, you'll get some RTTI and code hinting in the IDE - which can help prevent coder error.

But it would be a simple matter to write a custom version of the parse method that would work with a final (non-dynamic) class.

On the project I am currently working on - memory and runtime performance are critical, and we prefer using custom parsers for these reasons:

1) It allows us to alter the strictly typed object at the get-go and forgo copying the data into a temporary generic object.

2) We can build the validator right into the parser, throwing run-time errors as necessary to notify the program we have receive incorrect JSON data.