If we need to store all the URL variables to the DB, say, for tracking the user, it is easy. We can use a varchar field to store this information and retrieve later.

But when it comes to FORM variables or Objects, the RDBMS does not support storing them.

The solution here is to convert the objects to a string which can be stored and retrieved easily using SQL queries.

In Railo, we have the functions SerializeJSON() and DeSerializeJSON() that can be used to perform this task.

<cfset objUser.Name = “Prasanth Kumar.S” />
<cfset objUser.Job = “Software Engineer” />
<cfset objUser.Place = “Kochi, India” />

<!— Create an array for the users Skills. —>
<cfset arrSkills = ArrayNew( 1 ) />
<cfset arrSkills[ 1 ] = “ColdFusion” />
<cfset arrSkills[ 2 ] = “PHP” />
<cfset arrSkills[ 3 ] = “Android” />
<cfset arrSkills[ 4 ] = “iOS” />

<!— Store the movies in the actress. —>
<cfset objUser.Skills = arrSkills />

<cfdump var=”#objUser#”>

<cfoutput>#serializeJSON(objUser)#</cfoutput>

<cfdump var=”#Deserializejson(serializeJSON(objUser))#”>

 

The output of the above code will be as below:

coldfusion_railo_object_to_string

First we created an Struct with an Array in it.

Then to convert the Struct to string we used the function serializeJSON

To convert the JSON back to object we used the function DeSerializeJSON

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.