There have been some comments on other entries here regarding JSON.
JSON is a data interchange format, and is primarily used as a way to package data for use with Ajax development.
ColdFusion 8 (aka Scorpio) adds support for JSON in several ways:
- For starters, two new functions named SerializeJSON() and DeserializeJSON() make it easy to convert data to and from JSON. Simple variables (numbers and strings), dates, arrays, structures, and even queries can be converted back and forth. ColdFusion queries get converted into JSON objects with two entries, COLUMNS contains a list of columns, and DATA is an array of arrays containing the actual data.
- A supporting IsJSON() function does exactly what its name suggests.
- In addition, CFC methods can now be instructed to return data serialized as JSON by specifying returnformat="json". This is perfect for Ajax controls that invoke CFC methods to retrieve data.
May sound strange to you, but this is the first new scorpio feature i hear about that *really* grabs my interest. I could drop this into my code TODAY.
/t
From CF to the browser would work very similar on the cf side. ( btw, I'm just assuming this is how it'll work having done both JSON and WDDX work in the past)
For example:
<cfset mystruct=structnew()>
<cfset mystruct.name="Steve">
<cfwddx action="WDDX2JS" input="#mystruct#" output="mystructJS" toplevelvariable="myStruct">
<script>
<cfoutput>#mystructJS#</cfoutput>
</script>
vs:
<script>
<cfoutput>var mystruct=#SerializeJSON(myStruct)#</cfoutput>
</script>
Going from JS back to CF is a little trickier.
With WDDX, you have to include the WDDX.js file, create a new Serializer, serialize it, then send it via xmlhttp back to CF and call the cfwddx tag.
With JSON, it's a similar process. you serialize the object with one of the various json libraries out there, and send it back to cF and then call the deserialize() function.
So these two processes are almost identical in implementation, the difference is that JSON is dramatically less markup required and doesn't need a parser on the javascript side.
Did that help?
<cfset mystruct=structnew()>
<cfset mystruct.name="Steve">
<cfwddx action="WDDX2JS" input="#mystruct#" output="mystructJS" toplevelvariable="myStruct">
<script>
<cfoutput>#mystructJS#</cfoutput>
</script>
vs:
<script>
<cfoutput>var mystruct=#SerializeJSON(myStruct)#</cfoutput>
</script>
--- Ben
Interesting that you're providing automatic CFC conversions. Are you doing the same for XML and/or WDDX?
--- Ben