Returning a struct through a web service
Has anyone ever tried returning a ColdFusion structure through a web service? The WSDL type CF assigns to this is apachesoap:Map, but when I consume this in VB.NET, the data is null.
I have converted the entire thing into a JSON string, and that seems to work, but it requires a ton of code on the .NET side to handle. We are trying to do this with a minimal amount of work for our customers.
Thanks
cmhampton
Junior Poster in Training
79 posts since Feb 2008
Reputation Points: 23
Solved Threads: 10
Skill Endorsements: 0
I've found a "solution". It's not perfect, but it works pretty well. If you use SerializeJSON, it will convert it into a properly formatted JSON string. In this case we are actually returning an array of structures, so instead of serializing the entire object, if you serialize each struct as you append it to the array, it gets returned as a impl:ArrayOf_xsd_anyType SOAP type. .NET has no trouble with this at all. Then there's a couple lines to write in .NET to convert that into objects:
<cfset retArray = ArrayNew(1) />
<cfloop query="queryToProcess">
<cfscript>
tempStruct = StructNew();
StructInsert(tempStruct, "Field1", queryToProcess.field1);
StructInsert(tempStruct, "Field2", queryToProcess.field2);
ArrayAppend(retArray, SerializeJSON(tempStruct));
</cfscript>
</cfloop>
Dim objTemp As Object 'This has to be created as an object instead of an array.
objTemp = WebServiceClass.GetData() 'This would be the web service class created by the Web Reference.
Dim serializer As New JavaScriptSerializer()
Dim theClass As New MyClass()
Dim theCollection As New MyClassCollection() 'These are your class objects
Dim index As Integer = 0
'Loop through each entity in the object
For index = 0 To objTemp.length - 1
theClass = serializer.Deserialize(Of MyClass)(objTemp(index)) 'Deserialize the JSON data
theCollection.Add(theClass) ' Add it to the collection
Next
The important thing here is that the Class created in .NET must have properties named exactly the same as the keys of the structure. Otherwise, they will not map properly.
I hope this helps someone else out there. I searched for hours to find a solution and found many others having the same problem, with no answer.
cmhampton
Junior Poster in Training
79 posts since Feb 2008
Reputation Points: 23
Solved Threads: 10
Skill Endorsements: 0
Question Answered as of 2 Years Ago by
arrgh