Search code examples
pointersstructcoldfusioncoldfusion-9wddx

How to WDDX ColdFusion struct and maintain pointers or recursion


I am using WDDX to to store a ColdFusion struct in a database, and I would like to maintain the pointers. Here's an example (sorry, the shorthand notation may be full of errors b/c I hardly ever use it):

tshirt={color={selected="red",options=["red","blue","yellow","white"]}};
tshirt.front= {colors=tshirt.color,design="triangle",ink="green"};
tshirt.back= {color=tshirt.color,design="square",ink="black"};

Right now, tshirt.front.color, tshirt.back.color and tshirt.color are all pointers to the same struct. If I change tshirt.color.selected to "blue", tshirt.back.color.selected and tshirt.front.color.selected will also be "blue".

However, suppose I WDDX tshirt and then unWDDX it. When I change tshirt.color.selected to "white", it is not changed in tshirt.front.color.selected or tshirt.back.color.selected.

Can anyone suggest another way to serialize and unserialize data that would preserve the pointers?

Just a few links that I've been using to research so far:


Solution

  • Use ObjectSave(), new in CF9:

    Description

    Converts a ColdFusion array, CFC, DateTime object, Java object, query, or structure into a serializable binary object and optionally saves the object in a file.

    Returns

    A serializable binary representation of the object.

    <cfscript>
        shirtdata = objectSave(tshirt);
        tshirt2 = objectLoad(shirtdata);
    
        tshirt2.color.selected = "blue";
        writeOutput(tshirt2.front.colors.selected);  // "blue" => reference kept
    </cfscript>
    

    Live Demo: http://www.trycf.com/scratch-pad/pastebin?id=L0g211aD