Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!---%%% GetSerializableObjectCopy %%% --->
- <cffunction name="GetSerializableObjectCopy" access="public" output="no" returntype="any">
- <cfargument name="o" type="any" required="yes">
- <cfargument name="oKey" type="any" required="no" default="">
- <cfargument name="currentDepth" type="numeric" required="no" default="1">
- <cfscript>
- // //////////////////////////////// //
- // /// Declare local variables /// //
- var MaxDepth = 8;
- var oCopy = StructNew();
- var oKeyArray = ArrayNew(1);
- var oKeyArrayLen = 0;
- var oTreatAsStruct = StructNew();
- var oKeyOrIndex = '';
- var oValue = '';
- var oValueMeta = StructNew();
- var oValueTypeName = 'unknown';
- var oValueTreatAsStruct = StructNew();
- oTreatAsStruct['treatAsStruct'] = false;
- // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //
- // /// Inspect the base object 'o' to see what it is and if it has children we need to inspect (determined by oKeyArrayLen) /// //
- if ( IsStruct( ARGUMENTS['o'] ) ) {
- oKeyArray = StructKeyArray( ARGUMENTS['o'] );
- oKeyArrayLen = ArrayLen( oKeyArray );
- oTreatAsStruct['treatAsStruct'] = true;
- } else if ( IsArray( ARGUMENTS['o'] ) ) {
- oCopy = ArrayNew(1); // Set our copy of the objecy (at this depth) to a type of array sice that is what the object (at this depth) is! (otherwise it remains a struct as defined above)
- oKeyArrayLen = ArrayLen( ARGUMENTS['o'] );
- } else if ( IsSimpleValue( ARGUMENTS['o'] ) ) {
- oCopy = Trim( ARGUMENTS['o'] );
- oKeyArrayLen = 0;
- } else {
- // Call treatObjectAsStruct() to see if this object can be treated as a struct enen though it is of a different type
- oTreatAsStruct = treatObjectAsStruct( ARGUMENTS['o'] );
- if( oTreatAsStruct['treatAsStruct'] ) {
- oKeyArray = oTreatAsStruct['oKeys'];
- oKeyArrayLen = oTreatAsStruct['oKeyLen'];
- } else {
- oKeyArrayLen = 0;
- if( Trim( ARGUMENTS['oKey'] ) NEQ '' ) {
- oCopy[ Trim( ARGUMENTS['oKey'] ) ] = '<UNSERIALIZABLE: Object of type "' & GetMetaData( ARGUMENTS['o'] ).GetName() & '">';
- } else {
- oCopy[ '[EMPTY_STRING]' ] = '<UNSERIALIZABLE: Object of type "' & GetMetaData( ARGUMENTS['o'] ).GetName() & '">';
- }
- }
- }
- // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //
- // /// Ensure that ARGUMENTS['o'] is either an array or is a struct, then... /// //
- // /// if oKeyArrayLen > 0 this object has children to inspect; look at the type/value of each child and add it to our copy or call recursivly if it has children /// //
- if( oTreatAsStruct['treatAsStruct'] OR IsArray( ARGUMENTS['o'] ) ) {
- for( var i = 1; i LTE oKeyArrayLen; i++ ) {
- oKeyOrIndex = ( IsStruct( ARGUMENTS['o'] ) OR oTreatAsStruct['treatAsStruct'] ) ? oKeyArray[i] : i; // Get the key for each child, regardless if it's an array index or struct key
- // Ensure that the key for this position in our loop actually exists (prevents trouble with proper null values)
- if(
- ( oTreatAsStruct['treatAsStruct'] AND StructKeyExists( ARGUMENTS['o'], oKeyOrIndex ) ) OR
- ( IsArray( ARGUMENTS['o'] ) AND NOT IsNull( ARGUMENTS['o'][ oKeyOrIndex ] ) )
- ) {
- oValue = ARGUMENTS['o'][ oKeyOrIndex ]; // Get the value at this key
- if( IsStruct( oValue ) ) {
- oValueTreatAsStruct = { 'treatAsStruct': true };
- } else if( IsArray( oValue ) OR IsSimpleValue( oValue ) OR IsQuery( oValue ) ) {
- oValueTreatAsStruct = { 'treatAsStruct': false };
- } else {
- oValueTreatAsStruct = treatObjectAsStruct( oValue ); // Find out if we can treat the value at this key as a struct
- }
- // This value can have children, prep for recursive call
- if( oValueTreatAsStruct['treatAsStruct'] OR IsArray( oValue ) ) {
- // Before calling recursively, check if this value is a component and kick out of this loop iteration if it is; do not call recursivly
- oValueMeta = GetMetaData( oValue );
- if ( StructKeyExists( oValueMeta, 'type' ) AND oValueMeta['type'] EQ 'component' ) {
- oCopy[ oKeyOrIndex ] = '<UNSERIALIZABLE: Component "' & oValueMeta['fullName'] & '">';
- continue; // Skips any code following in the loop, jumps to the beginning of the next loop iteration (for loop; var i)
- }
- // Call recursively or mark that we are at depth limit
- if( ARGUMENTS['currentDepth'] LT MaxDepth ) {
- // Check if the struct/array actually has a len before the recursive call
- if( ( oValueTreatAsStruct['treatAsStruct'] AND StructCount( oValue ) ) OR ( IsArray( oValue ) AND ArrayLen( oValue ) ) ) {
- oCopy[ oKeyOrIndex ] = GetSerializableObjectCopy( oValue, oKeyOrIndex, Evaluate( ARGUMENTS['currentDepth'] + 1 ) );
- } else if( oValueTreatAsStruct['treatAsStruct'] ) {
- oCopy[ oKeyOrIndex ] = StructNew();
- } else if( IsArray( oValue ) ) {
- oCopy[ oKeyOrIndex ] = ArrayNew(1);
- }
- } else {
- oCopy[ oKeyOrIndex ] = '<UNSERIALIZABLE: Limited at depth ' & MaxDepth & '>';
- }
- // Output either the simple value or an unserializable value message for this key/index
- } else {
- // Simple value, just set it in our copy
- if( IsSimpleValue( oValue ) ) {
- oCopy[ oKeyOrIndex ] = Trim( oValue );
- // Query, mark it as unserializable
- } else if ( IsQuery( oValue ) ) {
- oCopy[ oKeyOrIndex ] = '<UNSERIALIZABLE: Object value of type "query">';
- // Unknown type, lets check it
- } else {
- // This value behaves like a struct, pass it recursively!
- if( oValueTreatAsStruct['treatAsStruct'] ) {
- // Call recursively or mark that we are at depth limit
- if( ARGUMENTS['currentDepth'] LT MaxDepth ) {
- // Check if the struct actually has a len before the recursive call
- oCopy[ oKeyOrIndex ] = ( StructCount( oValue ) ) ? GetSerializableObjectCopy( oValue, oKeyOrIndex, Evaluate( ARGUMENTS['currentDepth'] + 1 ) ) : StructNew();
- } else {
- oCopy[ oKeyOrIndex ] = '<UNSERIALIZABLE: Limited at depth ' & MaxDepth & '>';
- }
- // Can't treat it like a struct, log it as unserializable
- } else {
- try {
- oValueTypeName = GetMetaData( oValue ).GetName();
- } catch ( any e ) {
- oValueTypeName = ( IsCustomFunction( oValue ) ) ? 'function' : 'unknown';
- }
- oCopy[ oKeyOrIndex ] = '<UNSERIALIZABLE: Object value of type "' & oValueTypeName & '">';
- }
- }
- }
- // Could not find the value at the specified key or position, mark it
- } else {
- oCopy[ oKeyOrIndex ] = '<UNSERIALIZABLE: Value for key "' & oKeyOrIndex & '" could not be found or evaluated; may be null>';
- }
- }
- }
- return oCopy;
- </cfscript>
- </cffunction>
- <!---%%% TreatObjectAsStruct %%% --->
- <cffunction name="TreatObjectAsStruct" access="private" output="no" returntype="struct">
- <cfargument name="o" type="any" required="yes">
- <cfscript>
- var oKeys = ArrayNew(1);
- var ReturnStruct = StructNew();
- ReturnStruct['treatAsStruct'] = false;
- ReturnStruct['oKeys'] = oKeys;
- ReturnStruct['oKeyLen'] = 0;
- // Check first if this object is not a (custom) function
- if( NOT IsCustomFunction( o ) ) {
- try {
- oKeys = StructKeyArray( ARGUMENTS['o'] );
- } catch( any e ) {
- // oKeys defined above
- }
- if( ArrayLen( oKeys ) ) {
- ReturnStruct['treatAsStruct'] = true;
- ReturnStruct['oKeys'] = oKeys;
- ReturnStruct['oKeyLen'] = ArrayLen( oKeys );
- }
- }
- return ReturnStruct;
- </cfscript>
- </cffunction>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement