Back in October I posed a fledgling cycle-safe CFDUMP replacement. Today, I had need for that same anti-cycle processing in serializeJson, so I abstracted the processing out into a CFC that handles both breaking cycles (for serialization) as well as restoring them (for deserialization). By running a cyclic data structure through the breakCycles method, you can use CFDUMP, serializeJson, or whatever other context-free recursive algorithm you want on it without fear of infinite looping. If you later turn that data structure back into an in-memory structure, you can use the restoreCycles method to recreate the cyclic references that breakCycles removed.
You can download the CFC (as a text file) here: cyclicutils.cfc.txt. If you have the example from the cycle-safe CFDUMP somewhere, save the CFC in the same directory and tack this code on to the end of the test case:
<cfset cu = createObject("component", "cyclicutils") /> <cfset b = cu.breakCycles(b) /> <cfdump var="#b#" label="b" /> <cfset b = cu.restoreCycles(b) /> <u:dump var="#b#" />
You'll see the cyclic structure dumped with the cycle-safe CFDUMP as before, then the cycles are broken and it's dumped with the standard CFDUMP, and then the cycles are restored and it's dumped with the cycle-safe CFDUMP again.
Otherwise, just create yourself a cyclic structure and pass it to breakCycles.
Comments are closed.