Search code examples
serializationcoldfusioncoldfusion-9coldfusion-10

SerializeJSON and DeserializeJSON not working properly for cfcatch block


I have a requirement for storing all the ColdFusion exception details in a database table. So I am planning to serialize the cfcatch structure and store it in db, but I have an issue.The structure before and after the serialization is not the same. So for testing purpose, here is small snippet of code I tried on my local,

<cftry>
 <cfset a = 5/0>
 <cfcatch type="any">
     <cfdump var="#cfcatch#">
     <cfset a = SerializeJSON(cfcatch)>
     <cfdump var = "#DeserializeJSON(a)#">
 </cfcatch>
</cftry>

Here is the screenshot from the first dump (Original cfcatch). enter image description here Here is the screenshot from the second dump(i.e serialize and then deserialize)

enter image description here


Solution

  • Entered as a comment on the OP question, but adding here as well in case it's useful to others.

    Try:

     <cfset a = SerializeJSON(duplicate(cfcatch))>
    

    Although when you dump it, the cfcatch data looks like a ColdFusion struct it isn't really, the duplicate forces it to a ColdFusion struct which it can then serialize. Be aware that duplicate does a deep copy.

    See https://stackoverflow.com/a/30630495/291653 for more info about what cfcatch is.