Dumping ColdFusion Values Into A String Variable Using WriteDump()
Out of the box, there is no option to output the writeDump() result to a variable. But, when you invoke writeDump(), it will output to the current buffer. This means that we can output it to a variable using CFSaveContent and a user-defined output buffer. Then, we can use the result like any ordinary String variable:
As you can see, we're invoking writeDump() inside the body of a CFSaveContent tag. This allows the output to be written to our CFSaveContent buffer instead of the current request response output buffer. And, when we run the above code, we get the following output:
Just to be clear, I am in no way trying to imply that plain text is better than JSON. I am only saying that in some cases, when my highest priority is readability - not deserialization - I am finding that storing or sending ColdFusion values in writeDump-text format is a fairly nice experience.
Want to use code from this post? Check out the license.
I think a great use case for this is in error reporting. Those are usually something read by a human directly, then dealt with and deleted.
In my applications, in the onError method of Application.cfc I create a dump of the exception variable and several other scopes, such as FORM, URL, CGI and session. Then, save the cfdump of all that to a variable (using cfsavecontent, like in your post) and email it to myself as an HTML attachment.
This typically provides more than enough information to troubleshoot the error and if there's a user logged in, bonus information about who encountered the error. I get a kick out of being able to email someone out of the blue and say "I noticed you may have experienced an error. Here's what happened, and it's now fixed." :)
Error reporting is definitely the context I had when I wrote this function. I have a error logging service that logs exceptions to a database, example:
Then, I just look in the "text" field of the database record and its super easy to read.
What happens with all the extra JS and CSS that comes with a WriteDump?
When you view-source with a WriteDump in a page there's a whole heap of extra stuff added in.
Holy crap bags... ok I didn't know about the format = "text" option... my life has changed forever, for the better.
Ha ha ha, awesome! Glad to hear that :D