Custom toJSON() method is not called for each property
I'm getting familiar with JSON in ActionScript 3 (FP 11), and experimenting with custom toJSON methods. The Developer's Guide section on Native JSON Support clearly states:
JSON.stringify() calls toJSON(), if it exists, for each public property that it encounters during its traversal of an object. A property consists of a key-value pair. When stringify() calls toJSON(), it passes in the key, k, of the property that it is currently examining. A typical toJSON() implementation evaluates each property name and returns the desired encoding of its value.
However, both the subsequent examples on the very same page and my own tests indicate that, on the contrary, toJSON() is only called once for the whole object, and no argument is passed for k. Take the following trivial class:
public class JSONTest { public var firstProperty:int = 1; public var secondProperty:String = "Hello world"; public function toJSON(k:String):* { trace("Calling toJSON on key", k); return this.toString(); } }
According to the documentation this should cause the following to print:
Calling toJSON on key firstProperty
Calling toJSON on key secondProperty
And the output should basically be the same as without a custom toJSON method:
{ "firstProperty" : 1, "secondProperty" : "Hello world" }
Instead, this just throws an error because nothing at all is passed as k.
So, is the documentation wrong and the engine is not supposed to call toJSON for every property, or is the engine incorrectly only calling it once?
