Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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 };
}
}
}
Copy link to clipboard
Copied
or using your method you could use
flash.geom.Vector3D.prototype.toJSON = function (k:*):*
{
return {x:this.x, y:this.y, z:this.z};
}
Copy link to clipboard
Copied
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!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more