Search This Blog

Friday, May 31, 2013

ColdFusion: Working with JSON

JSON has been gaining popularity to represent data in JavaScript applications. Below is an example of JSON data with some array and a ColdFusion parse function to convert JSON data to ColdFusion structure.

<cfsavecontent variable="personnel_record">
 "firstname": "John",
 "lastname": "Smith",
 "age": 36,
 "address": {
  "number": "12345",
  "street": "Any Street",
  "city": "Any City",
  "state": "AA",
  "zip": "90876"
 "children": [
   "firstname": "Ian",
   "age": 7,
   "married": false
   "firstname": "Heather",
   "age": 6,
   "married": false

<cfdump var="#deserializeJSON(personnel_record)#" />

Wednesday, May 29, 2013

ColdFusion: Reduce/Suppress Whitespaces

Without implementing proper technique, by default ColdFusion generates unnecessary whitespaces on result page. This will cause the page size bigger and consequently it will take more time to load the page. To suppress the whitespaces, we can enable whitespace management in CF Admin and/or do one of the following techniques:

 Absolutely no output. Not even <cfoutput> nor <cfdump>.
 Require closing tag </cfsilent>.
 One of the attributes in <cfcomponent> and <cffunction>.
 It works like <cfsilent>.
<cfsetting enablecfoutputonly="true">
 Only output things that are wrapped with <cfoutput>. 
 It works with <cfdump>.
 The effect goes to <cfinclude> as well.
 Matching tag, <cfsetting enablecfoutputonly="false"> is highly recommended.
 Closing tag </cfsetting> is optional and does not have any effect.
 Multiple declarations behave like a counter. 

<cfprocessingdirective suppresswhitespace="true">...</cfprocessingdirective >
 It reduces whitespaces, but it is not as effective as <cfsilent> or <cfsetting>.
 <cfoutput> is not required.
 Require closing tag </cfprocessingdirective>.

<!--- BONUS --->
<cfsetting showdebugoutput="false">
 Disable debugging mode when Debugging Settings in CF Admin is enabled.

ColdFusion Whitespace Options