13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Using native JSON functionality<br />

You can override, define, or redefine toJSON() on any ActionScript class. However, most built-in ActionScript classes<br />

don't define toJSON(). The Object class does not define toJSON in its default prototype or declare it as a class member.<br />

Only a handful of native classes define the method as a prototype function. Thus, in most classes you can’t override<br />

toJSON() in the traditional s<strong>en</strong>se.<br />

Native classes that don’t define toJSON() are serialized to JSON by the internal JSON implem<strong>en</strong>tation. Avoid<br />

replacing this built-in functionality if possible. If you define a toJSON() member, the JSON class uses your logic<br />

instead of its own functionality.<br />

Using the JSON.stringify() replacer parameter<br />

Overriding toJSON() on the prototype is useful for changing a class’s JSON export behavior throughout an<br />

application. In some cases, though, your export logic might apply only to special cases under transi<strong>en</strong>t conditions. To<br />

accommodate such small-scope changes, you can use the replacer parameter of the JSON.stringify() method.<br />

The stringify() method applies the function passed through the replacer parameter to the object being <strong>en</strong>coded.<br />

The signature for this function is similar to that of toJSON():<br />

function (k,v):*<br />

Unlike toJSON(), the replacer function requires the value, v, as well as the key, k. This differ<strong>en</strong>ce is necessary<br />

because stringify() is defined on the static JSON object instead of the object being <strong>en</strong>coded. Wh<strong>en</strong><br />

JSON.stringify() calls replacer(k,v), it is traversing the original input object. The implicit this parameter<br />

passed to the replacer function refers to the object that holds the key and value. Because JSON.stringify() does<br />

not modify the original input object, that object remains unchanged in the container being traversed. Thus, you can<br />

use the code this[k]to query the key on the original object. The v parameter holds the value that toJSON() converts.<br />

Like toJSON(), the replacer function can return any type of value. If replacer returns a string, the JSON <strong>en</strong>gine<br />

escapes the cont<strong>en</strong>ts in quotes and th<strong>en</strong> wraps those escaped cont<strong>en</strong>ts in quotes as well. This wrapping guarantees that<br />

stringify() receives a valid JSON string object that remains a string in a subsequ<strong>en</strong>t call to JSON.parse().<br />

The following code uses the replacer parameter and the implicit this parameter to return the time and hours values<br />

of a Date object:<br />

JSON.stringify(d, function (k,v):* {<br />

return "any date format you like via replacer: "+<br />

"holder[k].time:"+this[k].time + " holder[k].hours:"+this[k].hours;<br />

});<br />

Using the JSON.parse() reviver parameter<br />

The reviver parameter of the JSON.parse() method does the opposite of the replacer function: It converts a JSON<br />

string into a usable ActionScript object. The reviver argum<strong>en</strong>t is a function that takes two parameters and returns<br />

any type:<br />

function (k,v):*<br />

In this function, k is a key, and v is the value of k. Like stringify(), parse() traverses the JSON key-value pairs and<br />

applies the reviver function—if one exists—to each pair. A pot<strong>en</strong>tial problem is the fact that the JSON class does not<br />

output an object’s ActionScript class name. Thus, it can be chall<strong>en</strong>ging to know which type of object to revive. This<br />

problem can be especially troublesome wh<strong>en</strong> objects are nested. In designing toJSON(), replacer, and reviver<br />

functions, you can devise ways to id<strong>en</strong>tify the ActionScript objects that are exported while keeping the original objects<br />

intact.<br />

Last updated 6/6/2012<br />

120

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!