Last active
June 11, 2018 11:18
-
-
Save PrimaryFeather/6914861 to your computer and use it in GitHub Desktop.
Utility method to clone an AS3 object of any type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gamua.flox.utils | |
{ | |
import flash.utils.describeType; | |
import flash.utils.getQualifiedClassName; | |
/** Creates a deep copy of the object. | |
* Beware: all complex data types will become mere 'Object' instances. Supported are only | |
* primitive data types, arrays and objects. Any properties marked with "NonSerialized" | |
* meta data will be ignored by this method. | |
* | |
* <p>Optionally, you can pass a 'filter' function. It will be called on any child object. | |
* You can use this filter to create custom serializations. The following sample exchanges | |
* every 'Date' instance with a custom string.</p> | |
* | |
* <pre> | |
* clone:Object = cloneObject(original, function(object:Object):Object | |
* { | |
* if (object is Date) return DateUtil.toString(object as Date); | |
* else return null; // 'null' causes default behaviour | |
* });</pre> | |
*/ | |
public function cloneObject(object:Object, filter:Function=null):Object | |
{ | |
if (filter != null) | |
{ | |
var filteredClone:Object = filter(object); | |
if (filteredClone) return filteredClone; | |
} | |
if (object is Number || object is String || object is Boolean || object == null) | |
return object; | |
else if (object is Array) | |
{ | |
var array:Array = object as Array; | |
var arrayClone:Array = []; | |
var length:int = array.length; | |
for (var i:int=0; i<length; ++i) arrayClone[i] = cloneObject(array[i], filter); | |
return arrayClone; | |
} | |
else | |
{ | |
var objectClone:Object = {}; | |
var typeDescription:XML = null; | |
if (getQualifiedClassName(object) == "Object") | |
{ | |
for (var key:String in object) | |
objectClone[key] = cloneObject(object[key], filter); | |
} | |
else | |
{ | |
typeDescription = describeType(object); | |
var properties:XMLList = typeDescription.variable + typeDescription.accessor; | |
for each (var property:XML in properties) | |
{ | |
var propertyName:String = [email protected](); | |
var access:String = [email protected](); | |
var nonSerializedMetaData:XMLList = property.metadata.(@name == "NonSerialized"); | |
if (access != "writeonly" && nonSerializedMetaData.length() == 0) | |
objectClone[propertyName] = cloneObject(object[propertyName], filter); | |
} | |
} | |
return objectClone; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment