JSON parsing failure: Unexpected end of JSON string in CF18

499 Views Asked by At

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?

0

There are 0 best solutions below