Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

override of Vector.prototype.toJSON results unexpected

Explorer ,
Apr 26, 2012 Apr 26, 2012

I am having trouble overriding the toJSON function

by default toJSON on a Vector3D returns

{ "x": 0, "w": 0, "z": 0, "y": 0, "lengthSquared": 0, "length": 0 }

this is a bit verbose for me, so I am trying to get this result by overriding the toJSON prototype (did I say that right?)

to get a result that looks like

{ "x": 0, "y": 0, "z": 0 }

my function looks like

flash.geom.Vector3D.prototype.toJSON = function (k:*):*

{

   return '{' + ' "x":' + this.x + ', "y":' + this.y  + ', "z":' + this.z + '}';

}   

but I get back

"{\"x\":0,\"y\":0,\"z\":0}"

I have tried a bunch of different ways to encode the result

' - single quote

\" - escape double quote

I am sure I am missing some important peice, anyone have a clue?

TOPICS
ActionScript
923
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 26, 2012 Apr 26, 2012

worked this out from http://help.adobe.com/en_US/as3/dev/WS324d8efcab3b0d1e2408f9e3131fddffcfc-8000.html#WSab2db1cbc17f4f...

the secret is to return an object and not to try and construct teh string yourself

package

{

          import flash.display.Sprite;

          import flash.events.Event;

          public class Main extends Sprite

          {

                    public function Main():void

                    {

                              if (stage)

                              {

                                        init();

                              }

                              else

                              {

                                        addEventListener(Event.ADDED_TO_STAGE, init);

                              }

                    }

                    public function init(e:Event = null):void

                    {

                              removeEventListener(Event.ADDED_TO_STAGE, init);

                              var d:MyVector3D = new MyVector3D();

                              trace(JSON.stringify(d));

                    }

          }

}

package 

{

          import flash.geom.Vector3D;

          public class MyVector3D extends Vector3D

          {

                    public function MyVector3D()

                    {

                    }

                    public function toJSON(s:String):*

                    {

                              return { x:x, y:y, z:z };

                    }

          }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 26, 2012 Apr 26, 2012

or using your method you could use

flash.geom.Vector3D.prototype.toJSON = function (k:*):*

{

   return {x:this.x, y:this.y, z:this.z};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2012 Apr 26, 2012
LATEST

Works like a charm _spoboyle!

I am still stuck in the C++ world, where types are more tightly defined.

I try to stay away from overriding build in classes unless I have a lot to add to them, and so far Vector3D has performed well.

Many Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines