1

I have a JSON string I'm serializing with serializeJSON() and I'm getting it with a cfhttp but when I use DeserializeJSON() I'm throwing an error "JSON parsing failure: Unexpected end of JSON string".

<cfdirectory directory = "#imagePath#\#getType#\new" action="list" name="getList">
<cfset getDATA = serializeJSON(getList)>

I've validated the JSON string:

{"COLUMNS":["NAME","SIZE","TYPE","DATELASTMODIFIED","ATTRIBUTES","MODE","DIRECTORY","LINK"],"DATA":[["test.jpg",154227,"File","November, 22 2019 12:15:43","","","D:\\wwwroot\\images\\new",false]]} 

And it passes IsJSON() before I try to Deserialize.

<cfhttp method="get" username="#un#" password="#pw#" url="#brkrURL#" port="#brkrPORT#" result="brokerData">
<cfif IsJSON(brokerData.fileContent)>
   <cftry>
      <cfset getData = DeserializeJSON(brokerData.fileContent, false)>
      <cfdump var="#getData#">
      <cfcatch>
         <cfdump var="#brokerData.fileContent#">
         <cfdump var="#cfcatch#">
         <cfabort>                  
      </cfcatch>
   </cftry>
</cfif>

This was working in CF10 but this is a new CF18 server.

From Adobe: useCustomSerializer true/false. Whether to use the customSerializer or not. The default value is true. The custom serializer will always be used for deserialization. If false, the JSON deserialization will be done using the default ColdFusion behavior.

Since the default on SerializeJSON is "true" as a work around I changed the customSerializer to "true" which allowed the JSON to be deserialized but it returns a Struct (where it used to return a query) so I added a function to put the struct into a query.

<!--- convert a Struct to a query --->
<cffunction name="structToQuery" access="public" output="true" returntype="query" hint="Changes a structure to a query.">
   <cfargument name="getStruct" type="struct" required="true" />                            
   <cfset getData = QueryNew("")>
   <cfloop from="1" to="#arrayLen(getStruct.columns)#" index="col">
      <cfset QueryAddColumn(getData, getStruct.columns[col], 'VarChar', ArrayNew(1)) />
   </cfloop>
   <cfloop from="1" to="#arrayLen(getStruct.data)#" index="row">
      <cfset temp = QueryAddRow(getData, 1)>
      <cfloop from="1" to="#arrayLen(getStruct.columns)#" index="col">
         <cfif arrayIsDefined(getStruct.data[row], col)>
            <cfset QuerySetCell(getDataJSON, getStruct.columns[col],getStruct.data[row][col]) />                        
         <cfelse>                       
            <cfset QuerySetCell(getDataJSON, getStruct.columns[col],'') />
         </cfif>
      </cfloop>
   </cfloop>        
   <cfreturn getData>
</cffunction>               
<cfset getData = structToQuery(DeserializeJSON(brokerData.fileContent, true))>

This is now working... is there a better way?

Stephen Sharpe
  • 167
  • 1
  • 8
  • When I change false to true it puts the JSON into a structure. Where in CF10 false would put the JSON into a query. I assume I need another step to put the structure into a query. – Stephen Sharpe Dec 21 '19 at 19:53
  • Also need to add check for other queries that may return a NULL value – Stephen Sharpe Dec 22 '19 at 20:25
  • Just out of couriosity, what kind of a system is consuming this data? – James A Mohler Dec 22 '19 at 20:28
  • 1
    I have a server where photos are uploaded and a second server using the JSON to check if there are new photos. One is internal and the other is in a DMZ so there is no network connectivity. – Stephen Sharpe Dec 27 '19 at 18:29
  • Can we just send over some regular JSON rather than trying to emulate Query? In other words, if you have a struct, just serialize it and send that over. – James A Mohler Dec 27 '19 at 18:36
  • 1
    Its sending "useCustomSerialize" JSON and I'm receiving it in as a struct. Having the receiver convert it to a query was just to bridge to the legacy code. It was just curious to me what the DeserializeJSON useCustomSerialize flag was actually doing. At first, it presented as an error, but what I really need to do is to dig into the documentation on what CF is actually doing to the JSON. – Stephen Sharpe Jan 01 '20 at 17:30

0 Answers0