Search code examples
actionscript-3actionscriptflash-builder

Calling local class


I'm trying to call a static method from a class called "JSON", however the import I'm doing already has this method. How can I call a local class?

I've tried this:

mypackage.subpackage.JSON.encode(param1)

In C # the above would work, but I do not know why in ActionScript below does not work.

import flash.utils.ByteArray;

public class Package extends ByteArray
{
    public function writeJsonObject(param1:Object) : void
    {
        this.writeUTF(JSON.encode(param1));
    }
}

I get this error:

Error: 1061: Call to a possibly undefined method decode through a reference with static type Class


Solution

  • Try using JSON.stringify()

    import flash.utils.ByteArray;
    
    public class MyClass extends ByteArray
    {
        public function writeJsonObject(param1:Object) : void
        {
            this.writeUTF(JSON.stringify(param1));
        }
    }
    

    Reference: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html

    Also worth noting the difference between writeUTF and writeUTFBytes to understand exactly what's getting written to your ByteArray