Web Connection
JSON to CURSOR and back
Gravatar is a globally recognized avatar based on your email address. JSON to CURSOR and back
  Michael Hogan (Ideate Hosting)
  All
  Jun 21, 2021 @ 09:31am

I'd like to be able to quickly convert JSON data to a cursor for easy editing on a VFP form, and then convert it back to JSON for storage in a memo field - basically quick and easy editing for different data structures.

I thought there was easy conversion in webconnect but I'm having trouble finding the most direct route. I suppose I could just convert JSON to Object and then use the object properties as the data source... bypassing the cursor

Suggestions?

Gravatar is a globally recognized avatar based on your email address. re: JSON to CURSOR and back
  Michael Hogan (Ideate Hosting)
  Michael Hogan (Ideate Hosting)
  Jun 21, 2021 @ 09:44am

Perhaps Cursor -> XML would be the better approach?

Gravatar is a globally recognized avatar based on your email address. re: JSON to CURSOR and back
  Tore Bleken
  Michael Hogan (Ideate Hosting)
  Jun 21, 2021 @ 10:05am

Gravatar is a globally recognized avatar based on your email address. re: JSON to CURSOR and back
  Rick Strahl
  Michael Hogan (Ideate Hosting)
  Jun 21, 2021 @ 01:09pm

The problem is that JSON has no concept of a 'cursor' as a serialized Cursor is just a JSON array. A generic parser can't know how to map the array to a cursor without some extra logic, so it's not part of normal deserialization.

But it's possible to do. You can of course serialize a cursor to a JSON array and... you can turn that array back into a FoxPro collection.

So, what you can do is:

  • Serialize cursor to JSON
  • Deserialize JSON array into FoxPro Collection
  • Use CollectionToCursor() to convert collection to Cursor

I thought there was a helper for this somewhere, but I can't find it 😄

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: JSON to CURSOR and back
  Michael Hogan (Ideate Hosting)
  Rick Strahl
  Jun 21, 2021 @ 01:15pm

Nice... very nice. Thanks!

Gravatar is a globally recognized avatar based on your email address. re: JSON to CURSOR and back
  Rick Strahl
  Michael Hogan (Ideate Hosting)
  Jun 21, 2021 @ 01:19pm

Here's an example (using the Web Connection sample customer table)

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


* Collection of cust objects
loCusts = loSer.Deserialize(lcJson)

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

*** Write out the data
CollectionToCursor(loCusts, "TCustomers")

BROWSE NOWAIT

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: JSON to CURSOR and back
  Rick Strahl
  Michael Hogan (Ideate Hosting)
  Jun 21, 2021 @ 02:21pm

Added an explicit method to wwJsonSerializer:

wwJsonSerializer::DeserializeCursor()

************************************************************************
*  DeserializeCursor
****************************************
***  Function: Special Deserialize overload that assumes the result
***            is an object array that can be deserialized into an
***            open cursor
***    Assume:
***      Pass: lcJson           - JSON with Object Array
***            lcWritableCursor - Cursor name that is open and writable
***    Return: Number of records in the cursor
************************************************************************
FUNCTION DeserializeCursor(lcJson, lcWritableCursor)
LOCAL loCol as Collection

loCol = THIS.Deserialize(lcJson)

IF TYPE("loCol.Count") # "N"
   RETURN -1
ENDIF

CollectionToCursor(loCol, lcWritableCursor)

RETURN loCol.Count
ENDFUNC
*   DeserializeCursor

+++ Rick ---

© 1996-2024