Include JSON as a custom ActionScript object?

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

mostly:

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

Regards Adlertz

+6
json object casting flash actionscript-3
source share
5 answers

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

However, you can use some simple tricks as a workaround. For example, you can declare a constructor for your custom class that takes an object, and initialize it with members with the properties of the object.

Then you would use:

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

PS. As for comments, this is not true for every language ... I think this makes it ActionScript-specific.

+4
source share

This is essentially impossible. And this 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 hash. Anyway. There are different solutions, this will be one that can handle several 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; } } } 

limitations:

  • will fail if your panic class, if it is spam with zeros when building, or the constructor needs more than 9 arguments
  • fails, and also cannot recursively, so it can simply assign anonymous objects or arrays to the properties of the returned instance

hope this helps anyway;)

Greetz

back2dos

+3
source share

Actually - you can get around this limitation by using the assembly in the parser and overriding the JSON.parse method and using the capabilities of the anonymous function to access the object in the parent function.

For example, check this piece of code

 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 as is code sample, the MutatorData class must be declared dynamic, so that ALMOST hits the goal of creating a class for it. You cannot prevent other encoders from being added to it; spelling errors in your JSON string will become object properties. Evenso, you will get RTTI and code hints in the IDE, which can help prevent an encoder error.

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

In the project I'm working on now, memory and runtime performance are important, and we prefer to use custom parsers for the following reasons:

1) This allows us to modify a strongly typed object at the get-go stage and not copy data to a temporary shared object.

2). We can build the validator directly in the parser, selecting errors at runtime, if necessary, to notify the program about the receipt of invalid JSON data.

+2
source share

there is a way

as3-vanilla lib on github, very good :)

https://github.com/jonnyreeves/as3-vanilla

+2
source share

You cannot create personalized objects from dynamic objects. But you can extend the JSON decoder from as3corelib. I did this for this very reason. When I decode json-String, I pass the class name of the encoded object. With a little use of reflection, you get a strong typed user object back. Of course, before decoding, you need to know the class name of the encoded object ...

0
source share

All Articles