Railo's cool serialize() method, serialize anything!


Railo's cool serialize() method, serialize anything!

Posted by Luis Majano
Dec 16, 2008 00:00:00 UTC
I have been developing more and more on Railo and I am really liking some of its unique features due to its simplicity and total awesomeness. Here is a small tip on how to serialize complex objects such as queries, structures, arrays, and a whole gammut of all those types NESTED!!. All you need to do is call the serialize() method to convert the entire complex object into its string represenation. Then call evaluate() and Railo will evaluate the text back to its original object. EASY!!

At least much easier than trying to do serialization and deserialization with java. Anyways, I thought I would share this little tip:

myStruct = {fname:'Luis',lname:'Majano',great:'You Know it!!',

dates:[now(),now()+1,now()+2],

TodayIs:'#now()#'};

dump(myStruct);

//Serialize the struct

MySerialization = serialize(myStruct);

writeOutput("MySerialized Structure: #MySerialization#");

//Reinflate it back.

NewStruct = evaluate(MySerialization);

dump(newStruct);

 


AJ Mercer

Hi Luis,

Can is serialize components?

Big Mad Kev

@ AJ I believe it can and so can CF8 (But there are some exceptions on the data types) but I believe this is fixed in the Eagerly Anticipated Future Release of ColdFusion

ike

With the exception of CFCs or Java objects, you can also do the same thing with serailizeJSON() and deserializeJSON() - which would probably be my tendency for any data that I expect not to contain CFCs. Though I'm not sure offhand if that would preserve the query as a query once it's deserialized. It may come back as a struct of arrays.

Ben Nadel

And, let's not forget about CFWDDX, which has been around forever :) She ain't pretty, but she gets the job done well.

Site Updates

Entry Comments

Archives

Entries Search