Railo tip: store complex data by using serialize(data)

Railo has this great function, which is my all-time favorite: serialize(). What it does, is converting (almost) any data to a string.
Now if you use the evaluate() function with that string, then the original data is rebuilt again!

So, let's say you have a structure with multiple nested queries and strcutures, and you want to save this data for debugging. Then you only have to serialize() it:

<cfset myDataAsString = serialize(myComplexDataObject) />

Not only can you use this on structures, arrays, and query objects, but you can even serialize CFCs. The variables scope inside the cfc is also serialized, so you will get a completely accurate object back.

Some examples of serialized data

This is the component test.cfc, which has 2 variables inside, test and test2:


This is a structure with 2 queries inside:


As you can see, it doesn't take much space, and is even human-readable.
Did you also notice the functions struct() and query() inside the serialized data? Those are actually Railo built-in functions. But that's for another Railo tip ;-)

Now, if you want to use this data again, you can just simply call evaluate() to get the original data back:

<cfset myData = evaluate(myDataAsString) />

I hope you now share my enthusiasm on this amazing function :-)

del.icio.us Digg StumbleUpon Facebook Technorati Fav reddit Google Bookmarks
| Viewed 3466 times

No comments yet.

(will not be published)
Leave this field empty