pf » Serializing CFC's in ColdFusion 8
Serializing CFC's in ColdFusion 8

One of the handy new features in ColdFusion 8 is that CFC's are now serializable. There isn't a whole lot of information about this new feature in the docs, but I did some playing around and it does appear that they have used Java's serialization API. This means that you can use java's java.io.ObjectOutputStream to serialize your CFC's...
Here's an example of how you might write serialize a CFC to a file:
<cfset myCFCInstance = CreateObject("component", "test")>
<cfset myCFCInstance.setName("something")>
<cfset fileOut = CreateObject("java", "java.io.FileOutputStream")>
<cfset fileOut.init(ExpandPath("./serialized_cfc.txt"))>
<cfset objOut = CreateObject("java", "java.io.ObjectOutputStream")>
<cfset objOut.init(fileOut)>
<cfset objOut.writeObject(myCFCInstance)>
<cfset objOut.close()>
You will now have a file called serialized_cfc.txt that you can open up. The file is actually a binary file, but you can read it with a text editor and see the values of your CFC instance variables inside this file.
Now suppose you want to read that file and reconstruct your CFC instance (deserialize it). We just use a FileInputStream and a ObjectInputStream:
<cfset fileIn = CreateObject("java", "java.io.FileInputStream")>
<cfset fileIn.init(ExpandPath("./serialized_cfc.txt"))>
<cfset objIn = CreateObject("java", "java.io.ObjectInputStream")>
<cfset objIn.init(fileIn)>
<cfset newCFCInstance = objIn.readObject()>
<cfset objIn.close()>
<cfoutput>#newCFCInstance.getName()#</cfoutput>
Now suppose you didn't want to write the CFC instance to a file, but rather store it as a value somewhere else... We can use a ByteArrayOutputStream and then turn turn the Byte Array into a Base64 string.
<cfset byteOut = CreateObject("java", "java.io.ByteArrayOutputStream")>
<cfset byteOut.init()>
<cfset objOut = CreateObject("java", "java.io.ObjectOutputStream")>
<cfset objOut.init(byteOut)>
<cfset objOut.writeObject(myCFCInstance)gt;
<cfset objOut.close()>
<cfdump var="#ToBase64(byteOut.toByteArray())#">
Handy Stuff. Are there any functions or new features that I have missed that make this process any easier?
add to del.icio.us
| Tags: coldfusion, cfml, serialization, cfc, coldfusion 8, java, base64
Related Entries
- 10 Most Useful Image Functions in ColdFusion 8 - April 5, 2008
- CFImage Effects Library for ColdFusion 8 - August 9, 2007
- CFThread - Don't Abuse It - August 7, 2007
- Null Java References in CF 6 vs 7 - January 10, 2006
- DNS Query with ColdFusion - October 27, 2005
With a base64 string, could you stick that representation of the object in a database for instance? If so, what datatype and size would you need (sorry, don't know much about base64)
As for the base64 string - yes you could store this in a database - I would probably use a text feild instead of a varchar, because they can get very big. The size of the base64 string will be bigger than the size of the data it is encoding. Base64 is a way to encode binary data using printable ascii characters.
By the way, I am Rakshith (http://www.rakshith.net/blog/) from Adobe and I was the one who got the CFC serialization working in ColdFusion 8. I have a post on CFC serialization on my blog. Check it out if you are interested http://www.rakshith.net/blog/?p=4
@Micheal : The "has a" component too will be serialized. In fact, the serialization also takes care of circular references where a CFC points to itself.
@William: I start off my post on CFC serialization with a hypothetical use case. That should give you an idea where the functionality can be applied.
Thanks
Is this the suggested method?
@Jason - Yes that is the method I would use to convert the base64 string back into a CFC.
@Michael - There are a lot of different use cases for object serialization, but allowing CFC's in a session variable on a clustered environment is probably the main reason it was added in CF8. See Rakshith's post for more about that.
'java.lang.ClassNotFoundException: coldfusion.runtime.Array' according to the stack trace.
Writing goes fine. Any idea why this is?
Cheers
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
- 10 Most Useful Image Functions in ColdFusion 8
- Speaking at NYC CFUG This Week
- Adobe AIR Tutorial for HTML / JavaScript Developers
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL
RSS
Pete Freitag is a software engineer, and web developer located in









