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

Nicely done. Exactly what I needed.

@bennadel
Copy link
Author

@OneGrumpyBunny I'm glad this helpful; but, looking at the content of these gists, I wanted to apologize for some of the crude-ish references. I recently tried to clean-up really old (and younger me) references like this; but, it seems that the Gists live on. For a deeper explanation, take a look at: https://www.bennadel.com/blog/3791-as-a-man-i-can-be-a-better-example-than-i-have-been.htm

That said, I am bullish on the use of JSON as a storage mechanism. Have you looked at "NDJSON" or "Newline Delimited JSON". It can be really helpful on massive files, where each line is its own isolated JSON payload: https://www.bennadel.com/blog/3668-parsing-and-serializing-large-datasets-using-newline-delimited-json-in-lucee-5-3-2-77.htm

@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