// you’re reading...

Coldfusion

Serializing in Coldfusion

In one of my more recent projects, it became necessary to store an array for later use. Mysql does not support this feature. However, it is possible to store binary data and a Mysql Blob is the perfect data type for just this. In fact you can store just about anything you want with this method, whether it be an image, pdf, or even a Coldfusion component, these two little function will serve you great.

A LITTLE JAVA TO THE RESCUE

The code below has been posted on several blogs. I’ve found versions of it at Pete Freitag’s site. I modified it just a tad to serve my purpose. Now it’s object in, binary out and vice versa.

<cffunction name="serialize" access="public" output="false" returntype="Binary" >
     <cfargument name="object" type="any" required="true">
 
	   <cfset byteOut = CreateObject("java", "java.io.ByteArrayOutputStream")/>
	   <cfset byteOut.init()/>
	   <cfset objOut = CreateObject("java", "java.io.ObjectOutputStream")/>
	   <cfset objOut.init(byteOut)/>
	   <cfset objOut.writeObject(object) />
	   <cfset objOut.close()/>
 
     <cfreturn byteOut.toByteArray() />
</cffunction>
 
<cffunction name="deserialize" access="public" output="false" returntype="any">
     <cfargument name="data" type="Binary" required="true" />
 
          <cfset inputStream = CreateObject("java", "java.io.ByteArrayInputStream") />
	  <cfset objIn = CreateObject("java", "java.io.ObjectInputStream") />
	  <cfset object = "" />
 
	  <cfset inputStream.init( data ) />
          <cfset objIn.init(inputStream) />
	  <cfset object = objIn.readObject() />
	  <cfset objIn.close() />
 
     <cfreturn object />
</cffunction>

AND NOW FOR A REAL EXAMPLE

The following code invokes a Serializer component, then creates a struct, and puts some code in it. I call a stored procedure that excepts my serialized struct.

<cfinvoke component="com.meekgeek.utils.Serializer" method="init" returnvariable="serializer" />
 
<cfset myStruct = structNew() />
<cfset myStruct["SomeNumber"] = 1 />
<cfset myStruct["MyArray"] = ArrayNew() />
 
<cfset results = "" />
 
<cfstoredproc datasource="#APPLICATION.dataSource#" procedure="save_struct_stored_procedure">
   <cfprocparam cfsqltype="CF_SQL_BLOB" variable="_MY_STRUCT" value="#serializer.serialize(myStruct)#" />
 
   <cfprocresult name="results" />
</cfstoredproc>

Now, I will retrieve my struct and dump it.

<cfinvoke component="com.meekgeek.utils.Serializer" method="init" returnvariable="serializer" />
 
<cfset results = "" />
<cfstoredproc datasource="#APPLICATION.dataSource#" procedure="get_struct_stored_procedure">
   <cfprocresult name="results" />
</cfstoredproc>
 
<cfset unserialized = serializer.deserialize( results.My_Struct ) />
 
<cfdump var="#unserialized#" />

Now, before you go about serializing everything. There are a few things you should know. Morgan Tocker posted an article you should read titled When should you store serialized objects in the database? I hope this saves someone some time.

You can download the class above here.

Discussion

No comments for “Serializing in Coldfusion”

Post a comment