Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 11:14
Show Gist options
  • Save bennadel/9759595 to your computer and use it in GitHub Desktop.
Save bennadel/9759595 to your computer and use it in GitHub Desktop.
JSON Files As Temporary File Storage In ColdFusion Applications
<!---
Get the full file path to the directory in which we are
storing our serialized JSON data files.
--->
<cfset dataDirectory = getDirectoryFromPath(
getCurrentTemplatePath()
) />
<!--- Create a basic ColdFusion object. --->
<cfset girl = {
name = "Joanna",
hair = "Brunette",
bestAttributes = [
"Smile",
"Legs"
]
} />
<!---
Serialize the ColdFusion data object into a JSON string
and write it to disk.
--->
<cfset fileWrite(
"#dataDirectory#data.json",
serializeJSON( girl )
) />
<!---
Read in serialized JSON string a deserialized it back into
a ColdFusion data object.
--->
<cfset girlFromJSON = deserializeJSON(
fileRead( "#dataDirectory#data.json" )
) />
<!--- Output the ColdFusion data object created JSON. --->
<cfdump
var="#girlFromJSON#"
label="JSON to ColdFusion"
/>
<!---
Get the full file path to the directory in which we are
storing our serialized JSON data files.
--->
<cfset dataDirectory = getDirectoryFromPath(
getCurrentTemplatePath()
) />
<!--- Create a basic ColdFusion object. --->
<cfset girl = {
name = "Joanna",
hair = "Brunette",
bestAttributes = [
"Smile",
"Legs"
]
} />
<!--- Serailize the ColdFusion object into a WDDX string. --->
<cfwddx
action="cfml2wddx"
input="#girl#"
output="girlWDDX"
/>
<!--- Write the WDDX data to disk. --->
<cfset fileWrite(
"#dataDirectory#data.xml",
girlWDDX
) />
<!---
Read in serialized WDDX string a deserialized it back into
a ColdFusion data object.
--->
<cfwddx
action="wddx2cfml"
input="#fileRead( '#dataDirectory#data.xml' )#"
output="girlFromWDDX"
/>
<!--- Output the ColdFusion data object created JSON. --->
<cfdump
var="#girlFromWDDX#"
label="WDDX to ColdFusion"
/>
@OneGrumpyBunny
Copy link

OneGrumpyBunny commented May 27, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment