wwJsonSerializer::DeserializeCursor

Deserializes a JSON object array result into a FoxPro cursor or table.

JSON has no concept of 'cursors' and holds columnar data as Arrays. You can serialize cursors and they are serialized into JSON as object arrays. But when that same data is deserialized back into FoxPro, it's returned as an Array of objects not a cursor.

To work around this problem this helper method combines Deserialize() and CollectionToCursor() to deserialize the JSON array into an open cursor that matches the structure of the object data of the JSON Array.

DO wwJsonSerializer

loSer = CREATEOBJECT("wwJsonSerializer")

SELECT TOP 5 * FROM Customers ORDER BY Company INTO CURSOR TQuery

CLEAR
lcJson = loSer.Serialize("cursor:TQuery", .T.)
? lcJson

USE IN TQuery && close

*** Create an empty writable cursor as 'schema' to import to
SELECT * FROM Customers WHERE 1=0 INTO CURSOR TCustomers READWRITE

*** returns 5 and TCustomers now has 5 added customers
? loSer.DeserializeCursor(lcJson, "TCustomers")

BROWSE NOWAIT

This method only works with a top level object JSON Object Array and does not work with nested child properties. The assumption is that the entire result is the table data contained in a single array of objects where the object matches the cursor/table fields.

For de-serializing child cursors in JSON, deserialize the entire object, then explicitly use CollectionToCursor() on the Collection property that you want to de-serialize into a cursor.

o.DeserializeCursor(lcJson, lcReadWriteCursor)

Return Value

Number of records imported or -1 on error

Parameters

lcJson
A JSON string of an object array to be parsed.

lcReadWriteCursor
A writable cursor or table alias, which serves as the structure scheme to import the data to. Any fields matched in this cursor are imported from the json data.


See also:

Class wwJsonSerialize | wwUtils::CollectionToCursor

© West Wind Technologies, 2023 • Updated: 11/26/22
Comment or report problem with topic