4

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:

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
jessieloo
  • 1,759
  • 17
  • 24

1 Answers1

3

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

Henry
  • 32,689
  • 19
  • 120
  • 221
  • I am curious how it behaves with regard to backward compatibility. Sounds like it is based on the java model, which uses serialVersionUID. If you change versions, and the uid value for a the serialized class changes, you won't be able to deserialize the object in the new version. Hopefully CF took that into consideration. – Leigh Aug 22 '14 at 04:16
  • I did a test from cf9 to cf10 and from cf10 to cf9 and they both worked correctly. – jessieloo Aug 26 '14 at 13:41
  • @jessieloo - Just saw your comment. Are you using the same jvm version for CF9 and 10? – Leigh Sep 06 '14 at 03:05
  • @Leigh, they are not the same jvm version. CF10 is 1.7.0_51_b13, CF9 is 1.6.0_17_b04 – jessieloo Sep 08 '14 at 17:20