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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Using native JSON functionality<br />

Parsing example<br />

The following example shows a strategy for reviving objects parsed from JSON strings. This example defines two<br />

classes: JSONG<strong>en</strong>ericDictExample and JSONDictionaryExtnExample. Class JSONG<strong>en</strong>ericDictExample is a custom<br />

dictionary class. Each record contains a person’s name and birthday, as well as a unique ID. Each time the<br />

JSONG<strong>en</strong>ericDictExample constructor is called, it adds the newly created object to an internal static array with a<br />

statically increm<strong>en</strong>ting integer as its ID. Class JSONG<strong>en</strong>ericDictExample also defines a revive() method that extracts<br />

just the integer portion from the longer id member. The revive() method uses this integer to look up and return the<br />

correct revivable object.<br />

Class JSONDictionaryExtnExample ext<strong>en</strong>ds the ActionScript Dictionary class. Its records have no set structure and<br />

can contain any data. Data is assigned after a JSONDictionaryExtnExample object is constructed, rather than as classdefined<br />

properties. JSONDictionaryExtnExample records use JSONG<strong>en</strong>ericDictExample objects as keys. Wh<strong>en</strong> a<br />

JSONDictionaryExtnExample object is revived, the JSONG<strong>en</strong>ericDictExample.revive() function uses the ID<br />

associated with JSONDictionaryExtnExample to retrieve the correct key object.<br />

Most importantly, the JSONDictionaryExtnExample.toJSON() method returns a marker string in addition to the<br />

JSONDictionaryExtnExample object. This string id<strong>en</strong>tifies the JSON output as belonging to the<br />

JSONDictionaryExtnExample class. This marker leaves no doubt as to which object type is being processed during<br />

JSON.parse().<br />

package {<br />

// G<strong>en</strong>eric dictionary example:<br />

public class JSONG<strong>en</strong>ericDictExample {<br />

static var revivableObjects = [];<br />

static var nextId = 10000;<br />

public var id;<br />

public var dname:String;<br />

public var birthday;<br />

}<br />

}<br />

public function JSONG<strong>en</strong>ericDictExample(name, birthday) {<br />

revivableObjects[nextId] = this;<br />

this.id = "id_class_JSONG<strong>en</strong>ericDictExample_" + nextId;<br />

this.dname = name;<br />

this.birthday = birthday;<br />

nextId++;<br />

}<br />

public function toString():String { return this.dname; }<br />

public static function revive(id:String):JSONG<strong>en</strong>ericDictExample {<br />

var r:RegExp = /^id_class_JSONG<strong>en</strong>ericDictExample_([0-9]*)$/;<br />

var res = r.exec(id);<br />

return JSONG<strong>en</strong>ericDictExample.revivableObjects[res[1]];<br />

}<br />

package {<br />

import flash.utils.Dictionary;<br />

import flash.utils.ByteArray;<br />

// For this ext<strong>en</strong>sion of dictionary, we serialize the cont<strong>en</strong>ts of the<br />

// dictionary by using toJSON<br />

public final class JSONDictionaryExtnExample ext<strong>en</strong>ds Dictionary {<br />

public function toJSON(k):* {<br />

var cont<strong>en</strong>ts = {};<br />

for (var a in this) {<br />

Last updated 6/6/2012<br />

121

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

Saved successfully!

Ooh no, something went wrong!