views:

957

answers:

4

Hello I was wondering if it is possible to cast my JSON string as a custom object?

basically :

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

Regards Adlertz

A: 

Does it work when you try it?

Alex Balashov
No it doesn't, it gives me a customObject that equals null
+2  A: 

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.

MrKishi
+2  A: 

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 ;)

back2dos
A: 

You cannot cast custom objects from dynamic objects. But you can extend the JSON-Decoder from as3corelib. I did that for exactly this reason. When I'm decoding a json-String I pass the class name of the encoded object. With a little use of reflection you get a strong typed custom object back. Of course you need to know the class name of the encoded object before decoding...

Mike

Mike