You can use CursorToCollection() to turn any cursor to a collection which you can then assign to a property. Alternately you can use a string value of "cursor_rawarray:Alias" to force serialization into a JSON array from a cursor.DO wwJsonSerializer
loSer = CREATEOBJECT("wwJsonSerializer")USE wwdemo\tt_cust
loObject = CREATEOBJECT("EMPTY")ADDPROPERTY(loObject ,"Name", "Rick")ADDPROPERTY(loObject ,"Company", "West Wind")ADDPROPERTY(loObject , "CustomerList", "cursor_rawarray:TT_Cust")
lcJson = loSer.Serialize(loObject)
ShowText(lcJson)
I think using CursorToCollection is probably easier. You could do a top level scan to get the top level collection. For each record you then collect all the child relationships into CursorToCollections and append them.
String concatenation is not simple because you're going to have to inject it into json and that's not really trivial.
+++ Rick ---
Wouldn't that require doing a scan loop for both master and detail records, and doing an 'add' or 'Scatter Name' for each master AND detail record? That doesn't seem easier or faster then string concatenation... Is there a way of getting a whole cursor (all records) into an object in one step?
I found a sample showing the scan loop methodology here: http://www.universalthread.com/ViewPageNewFAQ.aspx?ID=29297
I suppose the advantage would be just prepackaging the intermediate object this way so your JSON routine can figure out the structure for me...
Yes - you can create a top level object that contains other objects or array/collection/wwCollection instances to represent the JSON arrays. I would presume that whatever you're sending the data to is going to expect a single object.
+++ Rick ---
How would one construct an object representing several master/detail (one to many) records - each with several related records? Nested arrays? Nested Collections?
Seems overly complicated... but perhaps I'm missing something. Perhaps you can point to an example... Eager to learn.
What I ended up doing is generating JSON for the master records, and then looping through to create a child records cursor for each master record, generating JSON from that, and using StringReplace() to insert the children into the master JSON string:
SELECT ..., "Images4"+Listings.ListCode AS Images ;FROM ./Condo/Listings ;
INTO CURSOR TQuery READWRITE
lcReturnValue = loSerializer.Serialize("cursor:TQuery")SCANSELECT ..., TRANSFORM(ImageList.ImgCode) + [.] + ALLTRIM(ImageList.ImgType) AS ImgSrc ;FROM ImageList ;
WHERE ImageList.ListCode = TQuery.ListCode ;
INTO CURSOR TImageList
lcImageList = loSerializer.Serialize("cursor:TImageList")
lcImageList = STRTRAN(lcImageList,["Rows"],["image"])
lcTextToReplace = ["Images4] + TQuery.ListCode + ["]
lcReturnValue = STRTRAN(lcReturnValue,lcTextToReplace,lcImageList)SELECT TQueryENDSCAN
You should be able to keep using it.
When you say nesting what do you mean though? Can't you just construct one Fox object and then serialize the whole thing at once?
+++ Rick ---
I'm going to be nesting some JSON strings. Do I need to Destroy and re-create wwJSONSerializer for each pass with a newly populated cursor, or can I just repeatedly call Serialize() and get clean results from the object?
TIA