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
- Implicit Structure Notation ColdFusion - January 13, 2009
- 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
Trackbacks
Trackback Address: 649/995E6C23735E7A83C634C8AA4AFD1E6B
Comments
On 08/06/2007 at 11:06:39 AM EDT Ryan Guill wrote:
1
Is this how we are supposed to work with the serializable cfc's? This is really awesome, but I'm just wondering if there is an easier way, such as using the new file handling functions in cf 8?
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)
On 08/06/2007 at 11:17:00 AM EDT Pete Freitag wrote:
2
Ryan - like I said I couldn't find too much about serialization in the Docs, I did test CFWDDX and the new SerializeJSON functions, they don't work with CFC's So as far as I know this is the way to do it, but if anyone else knows a better way I would like to hear it...
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.
On 08/06/2007 at 11:41:42 AM EDT Ryan Guill wrote:
3
Ah, I see. Can you link to the information you did find in the docs... I can't seem to find anything at all.
On 08/06/2007 at 1:22:33 PM EDT Michael Long wrote:
4
Any idea what it does to "has a" component references?
On 08/06/2007 at 1:44:15 PM EDT Pete Freitag wrote:
5
Michael - I haven't had a chance to test that yet, but I would guess that those would be serialized as well. If anyone want's to test please post the results in the comments here.
On 08/06/2007 at 3:52:58 PM EDT robi sen wrote:
6
Thanks for following up on this for me. I am going to mess around with this tonight on a CF cluster to see how the serialization works in a cluster. Ill let you know.
On 08/07/2007 at 12:41:48 AM EDT William from Lagos wrote:
7
very nice pete. But what are the practical use cases for this functionality? In a real world app, why and where can this functionality be applied?
On 08/07/2007 at 1:13:51 AM EDT Rakshith wrote:
8
Nice post. I agree that there is not much information available in the docs on CFC serialization. That is because, CFCs stored in the session just replicate across sessions in a cluster environment during session replication. Nothing specific has to be done to get this working.
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.
On 08/07/2007 at 7:16:21 AM EDT Jason wrote:
9
This is very helpful, but how might one convert the last example back to the instance of the CFC?
Thanks
On 08/07/2007 at 7:32:11 AM EDT Jason wrote:
10
Ok after some playing I found out how to make it happen. Basically I used converted the Base64 string back to binary using toBinary() then used the java.io.ByteArrayInputStream and passed it into the java.io.ObjectInputStream similar to the example reading the file.
Is this the suggested method?
On 08/07/2007 at 10:19:20 AM EDT Pete Freitag wrote:
11
@Rakshith - Thanks for commenting and answering some questions...
@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.
On 10/09/2007 at 9:40:17 AM EDT Lomeos wrote:
12
reconstructing an object seems to fail when there's an array variable in it. ColdFusion gives a 'coldfusion.runtime.Array' error.
'java.lang.ClassNotFoundException: coldfusion.runtime.Array' according to the stack trace.
Writing goes fine. Any idea why this is?
Cheers
On 05/13/2009 at 3:43:43 PM EDT Jim Crews wrote:
13
I found this CFC for creating XML objects from CFCs (and vice versa)in the meantime. It DOES work with Arrays, etc:
http://www.celticinternet.com/blog2/post.cfm/cfc-serialisation-with-coldfusion-8
On 09/01/2009 at 11:29:11 AM EDT Anonymous wrote:
14
In Cumulative Hot Fix 3 for ColdFusion 8.0.1 http://kb2.adobe.com/cps/511/cpsid_51180.html
Bug Id 70580 says "Added support for serialization of Array, Datetime, Query and Java objects in CFCs."
On 02/05/2010 at 8:21:06 AM EST Jon wrote:
15
I always here Java folks talking about serializing objects. I do not understand what it really does, what benefit it gives me, or what a real life use would be. Would you explain. Thanks
On 02/05/2010 at 10:19:18 AM EST Pete Freitag wrote:
16
Jon, one good use case for object serialization is caching. Suppose you had some objects that are expensive to populate and you wanted them to persist for some period of time even if the server was restarted.
There are probably more use cases for serialization when building Java GUI apps, than with building CF web apps, so thats why you hear java folks talking about it more.
Post a Comment
Recent Entries
- Cache Template in Request Setting Explained
- What Version of Java is ColdFusion Using?
- ColdFusion 9 Performance Brief from Adobe
- Request Filtering in IIS 7 Howto
- J2EE Session Cookies on ColdFusion / JRun
- Hands on ColdFusion Security Training
- ColdFusion 9 Solr Vulnerability - Are you at Risk?
- FCKEditor Year 2010 Bug for Firefox 3.6 with ColdFusion
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
I found this CFC for creating XML objects from CFCs (and vice versa)in the meantime. It DOES work with Arrays, etc:
http://www.celticinternet.com/blog2/post.cfm/cfc-serialisation-with-coldfusion-8
Bug Id 70580 says "Added support for serialization of Array, Datetime, Query and Java objects in CFCs."
There are probably more use cases for serialization when building Java GUI apps, than with building CF web apps, so thats why you hear java folks talking about it more.







