Quantcast
Channel: West Wind Message Board Messages
Viewing all articles
Browse latest Browse all 10393

Re: Serialize Array of Objects

$
0
0
Re: Serialize Array of Objects
Web Connection 5.0
Re: Serialize Array of Objects
Nov. 28, 2012
04:54 pm
3NL108LE5Show this entire thread in new window
Gratar Image based on email address
From:Rick Strahl
To:Harvey Mushman
Hmmm...

Seems to me you could very easily use loSer.Serialize("cursor:Markers") and then when done simply replace the "Rows": with "markers": in the resulting string. Since google expects all lower case as wwJsonSerializer produces that should work.

Or maybe even easier using code like this:

DO wconnectCREATECURSOR Markers ( latitude n (12,6), longitude n(12,6),icon c(50),title c(30),content m)INSERT INTO Markers (latitude,longitude,icon,title,content) ; VALUES (20.90275, -156.37149,"asdasd","Hello World","Paia ")INSERT INTO Markers (latitude,longitude,icon,title,content) ; VALUES (45.708851, -121.510727,"asdasd","Hello World","Hood River") loCol = CursorToCollection(,2) loObj = CREATEOBJECT("EMPTY")ADDPROPERTY(loObj,"markers") loObj.Markers = loCol loSer = CREATEOBJECT("wwJsonSerializer") lcJson = loSer.Serialize(loObj) ShowText(lcJson)

You'll need the CursorToCollection helper that's been added to Web Connection in the latest release:

************************************************************************ * CursorToCollection **************************************** *** Function: Creates a collection from a cursor *** Assume: *** Pass: lcAlias - Optional Name of the table/alias *** if not passed *** lnMode - 0* - FoxPro Collection, 2 - wwCollection *** Return: Collection from cursor or NULL on failure ************************************************************************FUNCTION CursorToCollection(lcAlias, lnMode)LOCAL loResult, loRow, lcClass, lcOldAlias lcOldAlias = ""IF !EMPTY(lcAlias) lcOldAlias = ALIAS()SELECT (lcAlias)ENDIFIFEMPTY(lnMode) lnMode = 0ENDIFIF lnMode = 2 lcClass = "wwCollection"&& avoid pulling into projectELSE lcClass = "Collection"ENDIF loResult = CREATEOBJECT(lcClass)SCAN loRow = nullSCATTERNAME loRow MEMO loResult.Add(loRow)ENDSCANIF !EMPTY(lcOldAlias)SELECT (lcOldAlias)ENDIFRETURN loResultENDFUNC* CursorToCollection

The above produces output like this:

{"markers":[ {"content":"Paia","icon":"asdasd","latitude":20.902750,"longitude":-156.371490,"title":"Hello World"}, {"content":"Hood River","icon":"asdasd","latitude":45.708851,"longitude":-121.510727,"title":"Hello World"} ]}


Hope this helps,

+++ Rick ---



I'm working with Google Maps and having push-pins loaded from an external file JSON file. The content of the Informaion Popup Window along with Latitude and Longitude and pin color and style are all part of the file.

In trying to keep things really simple to program, I tried to use the Serialize Cursor but found the result to be incompatible with Google's specification. The data tag "Rows" in their world is labeled "markers". Trying to tweek the result file once the data is serialized seemed more cumbersome than my second choice. Also, because VFP free tables have an eight character fieldname limit, I can't spell out "longitude" with was another problem.

My second attempt was to create an EMPTY object, AddProperties for the fields I needed to serialize then SCAN through the data building an ARRAY of objects with my data. This went fine until I tried to serialize the array. I tried passing it by referance @laArray but found all the rows to be serialized wit hte same data.

In the end, I SCAN through my cursor, create a correctly labeled object, pass the object to the serializer and concatenate the result to my lcOutPut string. This works just fine!

Here is the sample code:

loMapMarker = CREATEOBJECT('Empty')ADDPROPERTY(loMapMarker,'latitude')ADDPROPERTY(loMapMarker,'longitude')ADDPROPERTY(loMapMarker,'icon')ADDPROPERTY(loMapMarker,'title')ADDPROPERTY(loMapMarker,'content')SELECT * FROM myData into tCursor1 loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = ''SCAN loMapMarker.Latitude = tCursor1.lat loMapMarker.Longitude = tCursor1.lng DOCASECASE tCursor1.Status = 'Approved' loMapMarker.Icon = 'green.png'CASE ... loMapMarker.Title = tCursor1.CaseNum lcContent = '' lcContent = lcContent + [<span style="font-size:9pt"><b>Case:</b> ] + ALLTRIM(tCursor1.CaseNum) +'</span><br>' lcContent = lcContent + [<span style="font-size:9pt"><b>Address:</b> ] + ALLTRIM(tCursor1.Address) +'</span><br>' loMapMarker.Content = lcContent * format data lcJSON = lcJSON + IIF(EMPTY(lcJSON),'',',') + loSerializer.Serialize(loMapMarker)ENDSCAN lcJSON = '{"markers":[ ' + lcJSON + ']}' lnBytes = STRTOFILE(lcJSON,'x:\web site path\myCaseMap.json') return lnBytes

Let me know if you want the stand alone HTML Map file to try out - it is very cool! I can now switch out the data and keep away from a WC generated map page with embedded data. This means the map page can live on any public server so long as the page points to local or remote data.

Onward('I have concrete to pour')!<s>

--hm


--hm



Rick Strahl
West Wind Technologies


from Maui, Hawaii

Making waves on the Web


Viewing all articles
Browse latest Browse all 10393

Trending Articles