Script based version of CFWDDX

A couple of days ago I wrote a blog post talking about how to deal with things you couldn’t do in ColdFusion scripting. As one of the examples I mentioned WDDX. For some reason I’ve seen multiple people in the last week or so bemoan the lack of support for a script based WDDX implementation so I thought I’d whip up the code for it.

WDDX is one of those things that - like many others - ColdFusion (and Allaire) do not get proper credit for. Did you know WDDX was introduced in 1998, at the same time as XML-RPC and before SOAP and web services? (Reference - Wikipedia entry) At the time the concept was pretty darn cool. You take any form of data, serialize it, and you can then syndicate it to the world. This was before Ajax really took off so the use cases were more HTTP/remote focused. So a news site, for example, could offer their news feed in WDDX and another site could read it remotely and present it on their own site. There were multiple implementations of WDDX - including libraries for PHP, Ruby, Python, Java, Perl, and others - all of which were open source and free for developers to use. This would allow a PHP site to easily aggregate content being served up from a ColdFusion site - or vice versa.

You have to admit - that's pretty cool. ColdFusion was definitely ahead of the curve here and once again did something complex in an incredibly simple fashion. While I'm not sure I'd recommend this now (I think JSON is the best format for complex data), as I said above, some people still actively use it and therefore would like to be able to use it within script based CFCs. I wrote the following component in about 5 minutes. In order to use it within your own model files you will need to inject it (ColdSpring would make this easy), or you could use something like the Helpers feature of Model-Glue.

<cfcomponent output="false"> <cffunction name="toWddx" access="public" returnType="string" output="false"> <cfargument name="input" type="any" required="true"> <cfargument name="useTimezoneInfo" type="boolean" required="false" default="true"> <cfset var result = ""> <cfwddx action="cfml2wddx" input="#arguments.input#" output="result" useTimezoneInfo="#arguments.useTimezoneInfo#"> <cfreturn result> </cffunction> <cffunction name="toCFML" access="public" returnType="any" output="false"> <cfargument name="input" type="any" required="true"> <cfargument name="validate" type="boolean" required="false" default="false"> <cfset var result = ""> <cfwddx action="wddx2cfml" input="#arguments.input#" output="result" validate="#arguments.validate#"> <cfreturn result> </cffunction> </cfcomponent>

And here is a simple example usage. I didn't actually use cfscript, but you can see how it would work:

<cfset s = { name="Ray", age=38, foo=[1,2,3,{x="1",y="2"}]}> <cfdump var="#s#" label="original data structure"> <cfset wddx = new wddx()> <cfset encoded = wddx.toWddx(s)> <cfoutput>#htmlEditFormat(encoded)#</cfoutput> <p> <cfset data = wddx.toCFML(encoded)> <cfdump var="#data#" label="After to wddx and back again...">

And the result...

Please consider the following code licensed under Apache License V2.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments