15.10.2012 Views

Actionscript 3 Entwicklerhandbuch

Actionscript 3 Entwicklerhandbuch

Actionscript 3 Entwicklerhandbuch

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

ACTIONSCRIPT 3.0 ENTWICKLERHANDBUCH<br />

Verwenden der nativen JSON-Funktionalität<br />

// over that object's properties to construct a fresh dictionary).<br />

//<br />

// The harder exercise is to handle situations where the dictionaries<br />

// themselves are nested in the object passed to JSON.stringify and<br />

// thus does not occur at the topmost level of the resulting string.<br />

//<br />

// (For example: consider roundtripping something like<br />

// var tricky_array = [e1, [[4, e2, 6]], {table:e3}]<br />

// where e1, e2, e3 are all dictionaries. Furthermore, consider<br />

// dictionaries that contain references to dictionaries.)<br />

//<br />

// This parsing (or at least some instances of it) can be done via<br />

// JSON.parse, but it's not necessarily trivial. Careful consideration<br />

// of how toJSON, replacer, and reviver can work together is<br />

// necessary.<br />

var e_roundtrip =<br />

JSON.parse(e_result,<br />

// This is a reviver that is focused on rebuilding JSONDictionaryExtnExample objects.<br />

function (k, v) {<br />

if ("class JSONDictionaryExtnExample" in v) { // special marker tag;<br />

//see JSONDictionaryExtnExample.toJSON().<br />

var e = new JSONDictionaryExtnExample();<br />

var contents = v["class JSONDictionaryExtnExample"];<br />

for (var i in contents) {<br />

// Reviving JSONGenericDictExample objects from string<br />

// identifiers is also special;<br />

// see JSONGenericDictExample constructor and<br />

// JSONGenericDictExample's revive() method.<br />

e[JSONGenericDictExample.revive(i)] = contents[i];<br />

}<br />

return e;<br />

} else {<br />

return v;<br />

}<br />

});<br />

trace("// == Here is an extended Dictionary that has been round-tripped ==");<br />

trace("// == Note that we have revived Jen/Jan during the roundtrip. ==");<br />

trace("e: " + e); //[JSONDictionaryExtnExample ]<br />

trace("e_roundtrip: " + e_roundtrip); //[JSONDictionaryExtnExample ]<br />

trace("Is e_roundtrip a JSONDictionaryExtnExample? " + (e_roundtrip is<br />

JSONDictionaryExtnExample)); //true<br />

trace("Name change: Jen is now Jan");<br />

a_jen.dname = "Jan"<br />

trace("e: " + e); //[JSONDictionaryExtnExample ]<br />

trace("e_roundtrip: " + e_roundtrip); //[JSONDictionaryExtnExample ]<br />

Letzte Aktualisierung 27.6.2012<br />

132

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

Saved successfully!

Ooh no, something went wrong!