Skip to main content
Participant
February 11, 2009
Question

Can a structure be stored in a string using structure notation

  • February 11, 2009
  • 3 replies
  • 550 views
Is there any way to store a structure as a string, for example in a database, that could be converted runtime like javascript can with eval, something like <cfset tmp=Evaluate("{VarA=[1,2,3]}")>
This topic has been closed for replies.

3 replies

Participating Frequently
February 13, 2009
Yes, you absolutely can using WDDX. There are a few caveats, though, like ampersands. Before I deserialize the structure I change them all to &.

I use this method to both store structures in a database and pass them as hidden form fields. Works like a charm!
Inspiring
February 14, 2009
> There are a few caveats, though, like
> ampersands.

What's the caveat with ampersands?

--
Adam
Inspiring
February 11, 2009
The closest you can come is if you want to serialize and deserialize the structure as a JSON object (JavaScript Object Notation).

<cfscript>
struct = StructNew();
struct.key1 = "Key 1";
struct.key2 = "Key 2";
struct.key3 = "Key 3";
myJSONStruct = SerializeJSON(struct);// converts the structure to a JSON string
myStructBack = DeserializeJSON(myJSONStruct);
</cfscript>

<cfdump var="#myJSONStruct#" />
<cfdump var="#myStructBack#" />

Of course, you'll need to know how to use a JSON string/object in your JS for this to be helpful but this will convert your struct to a string representation and allow you the option to convert it back in CF, if needed.
Inspiring
February 11, 2009
no