Search code examples
jsoncoldfusioncoldfusion-8cfc

Coldfusion 8 - SerializeJSON not serializing


I been developing a site offline on a WAMP setup and have been serializing beans using the SerializeJSON function like so:

propertyImageBean = CreateObject("component","cfcs.beans.property_image").init();
propertyImageBean.setname("test");
propertyImageBean.setalt("test alt");

<cfoutput>#SerializeJSON(propertyImageBean)#</cfoutput>

This has all been working correctly and the code above produces:

{"name":"test","alt":"asdasd","id":""}

However, when I upload my files to the live site the response is just an empty object:

{}

The local version is running on CF9 and the prod version is on CF8, so this is probably what the issue is.

Any ideas?

Thanks

EDIT:

<cfscript>

    propertyImageBean = CreateObject("component","cfcs.beans.property_image").init();
    propertyImageBean.setid(1);
    propertyImageBean.setname("test");
    propertyImageBean.setalt("asdasd");

</cfscript>
<cfdump var=#propertyImageBean#>
<cfoutput>id: #propertyImageBean.getid()#</cfoutput>
<cfoutput>alt: #propertyImageBean.getalt()#</cfoutput>
<cfoutput>name: #propertyImageBean.getname()#</cfoutput>

Outputs (prod server(:

  • A dump of the bean but does not display the bean properties in the dump whereas on the local server it does

    id: 1 alt: asdasd name: test

So it looks like it is a problem with the bean, unless there is a config issue on the server.

CFC:

<!--- PROPERTIES FOR DOCUMENTATION PURPOSES ONLY --->
<cfproperty name="id" displayname="id" hint="id of the property_image" type="any" required="True" />
<cfproperty name="name" displayname="name" hint="name of the property_image" type="any" required="True" />
<cfproperty name="alt" displayname="alt" hint="alt of the property_image" type="any" required="True" />

<!--- PSEUDO-CONSTRUCTOR: SETS DEFAULT VALUES IF INIT METHOD IS NOT CALLED --->
<cfscript>
    variables.id = "";
    variables.name = "";
    variables.alt = "";
</cfscript>

<!--- CONSTRUCTOR: TAKES IN ARGUMENTS AND CALLS SETTER (MUTATOR) FOR EACH ATTRIBUTE OF THE BEAN --->
<cffunction name="init" displayname="Init" hint="Constructor for the CFC" access="public" output="false" returntype="any">
    <!--- ARGUMENTS FOR THE CONSTRUCTOR, ALL OF WHICH ARE OPTIONAL (NO-ARG CONSTRUCTOR) --->
    <cfargument name="aid" displayname="id" hint="id of the property_image" type="any" required="false" default="" />
    <cfargument name="aname" displayname="name" hint="name of the property_image" type="any" required="false" default="" />
    <cfargument name="aalt" displayname="alt" hint="alt of the property_image" type="any" required="false" default="" />
    <!--- CALL THE SETTERS (MUTATORS) FOR EACH OF THE property_image ATTRIBUTES AND PASS IN THE ARGUMENTS --->
    <cfscript>
        setid(arguments.aid);
        setname(arguments.aname);
        setalt(arguments.aalt);
    </cfscript>

    <cfreturn this />
</cffunction>

<!--- GETTERS AND SETTERS (MUTATORS AND ACCESSORS) --->
<cffunction name="getid" access="public" output="false" returntype="string">
    <cfreturn variables.id />
</cffunction>
<cffunction name="setid" access="public" output="false" returntype="void">
    <cfargument name="aid" type="string" required="true" />
    <cfset variables.id = arguments.aid />
</cffunction>
<cffunction name="getname" access="public" output="false" returntype="string">
    <cfreturn variables.name />
</cffunction>
<cffunction name="setname" access="public" output="false" returntype="void">
    <cfargument name="aname" type="string" required="true" />
    <cfset variables.name = arguments.aname />
</cffunction>
<cffunction name="getalt" access="public" output="false" returntype="string">
    <cfreturn variables.alt />
</cffunction>
<cffunction name="setalt" access="public" output="false" returntype="void">
    <cfargument name="aalt" type="string" required="true" />
    <cfset variables.alt = arguments.aalt />
</cffunction>


Solution

  • First, setup CF8 locally. http://www.adobe.com/support/coldfusion/downloads.html#cf8proddl

    I guess SerializeJSON(cfc) is something new in CF9 that's not available in CF8. This is not an official, documented feature of SerializeJSON I don't believe.

    Implement a getMemento() function that returns the variables in as a struct, then you can SerializeJSON on that struct.

    /** a public function in Obj.CFC */
    function getMemento()
    {
        return {
            x=variables.x,
            y=variables.y
        };
    }
    
    // outside of the cfc...
    obj = createComponent("component","Obj").init();
    objJson = serializeJSON(obj.getMemento());