Tuesday, May 7, 2024
HomeJavaScriptCasting Java Structs And Arrays To ColdFusion Structs And Arrays

Casting Java Structs And Arrays To ColdFusion Structs And Arrays


More often than not, in fashionable ColdFusion, knowledge flows seamlessly in between the ColdFusion layer and the underlying Java layer. Behind the scenes, ColdFusion is working to solid or proxy objects, as wanted, in order that we will devour these objects as in the event that they have been native ColdFusion knowledge varieties. However, this magic is not at all times good. In some edge-cases, we’ve got to explicitly solid Java objects into ColdFusion objects with a purpose to devour the complete object API (equivalent to member strategies).

For example from yesterday, I needed to solid a Java array right into a ColdFusion array when polyfilling the title[] kind subject performance in Adobe ColdFusion. And, a number of years in the past, I ought to have used this strategy when attempting to devour a MongoDB BSON Doc as a ColdFusion struct.

I do not assume there is a one-size-fits-all answer right here. However, the few instances that I’ve come up towards this edge-case, the next approach at all times appears to work: I create a local ColdFusion object after which “append” the Java object to it. This creates a shallow copy of the Java object, swapping out the outer “Java shell” for a “ColdFusion shell”.

<cfscript>

	javaArray = javaCast( "string[]", [ "hello", "world" ] );

	dump( javaArray );
	dump( toCFArray( javaArray ) );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	javaObject = createObject( "java", "java.util.Hashtable" ).init();
	javaObject.put( "foo", "bar" );
	javaObject.put( "reply", 42 );

	dump( javaObject );
	dump( toCFStruct( javaObject ) );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	/**
	* I solid the given Java Array to a ColdFusion array utilizing a shallow copy.
	*/
	public array perform toCFArray( required any javaValue ) {

		return( arrayNew( 1 ).append( javaValue, true ) );

	}


	/**
	* I solid the given Java Object to a ColdFusion struct utilizing a shallow copy.
	*/
	public struct perform toCFStruct( required any javaValue ) {

		return( structNew().append( javaValue ) );

	}

</cfscript>

This method works as a result of the given Java objects are often “Array-like” or “Struct-like”; and, ColdFusion is aware of find out how to iterate over stated object values. And, if ColdFusion can iterate over the values, ColdFusion can append these values to a local ColdFusion knowledge sort.

If we run this Lucee CFML code, we get the next output:

CFDump output showing Java arrays and hash tables being cast to native ColdFusion arrays and structs, respectively.

As you possibly can see, the native Java array and hash-table varieties (represented in orange) have been solid to a local ColdFusion array and struct, respectively.

Apart: In actuality, Java Hashtable lessons could be handled like native ColdFusion structs – it is among the lessons that ColdFusion handles seamlessly. However, I wanted one thing to demo.

Once more, this is not meant to be a universally relevant answer. However, for easy knowledge varieties, this method has served me properly.

Need to use code from this publish?
Take a look at the license.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments