wwJsonSerializer::Serialize

This class can serialize FoxPro objects, values, collections and cursors to JSON and deserialize JSON strings into FoxPro objects, values or collections. Arrays are supported only as members of objects - all lists should be expressed preferably as Collections.

Here's an example that demonstrates how wwJsonSerializer works when round-tripping data:

lcJson = '{ "id": 50445, "message": "Whooo hooo", ' +;
            "status": 10 , "entered": "2018-11-21T23:10:10Z", ' +;
            "address": { "street": "123 Timber Ln", "number": 32 } }'

loSer = CREATEOBJECT("wwJsonSerializer")
loResult = loSer.DeserializeJson(lcJson)

*** 50445
? loResult.Id
? loResult.entered

? loSer.Serialize(loResult,.T.)

This pro The serializer supports complex nested structures and can also serialize (but not deserialize) FoxPro tables and cursors by way of a custom string syntax (cursor:AliasName). Objects returned are created dynamically on the fly with all arrays parsed into FoxPro collections.

To serialize:

do wwJsonSerializer
loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(loObject)  && Objects, Values, Collections

You can also serialize cursors:

select * from customers into TCustomers
lcJson = loSer.Serialize("cursor:TCustomers")  && Tables/Cursors

To de-serialize:

loObject = loSer.DeserializeJson(lcJson)  && JSON Objects, Values, Arrays 
? loObject.Value
o.Serialize(lvValue, llFormat)

Return Value

Serialized JSON string

Parameters

lvValue
The value to serialize

llFormat
Pretty formats the JSON output. Alternately you can also use the FormattedOutput=.T.?on the serializer level to achieve the same thing.

Note: formatting re-parses the JSON, so there is some overhead associated with re-formatting the JSON.

Remarks

Json is created as continuous text. If you would like formatted output set the Formatted property to .t. on the Serializer object. Note there's some overhead in this as the JSON is generated as continuous code first, then passed back to JSON.NET for formatting.


See also:

Class wwJsonSerialize | wwJsonSerializer::DeserializeJson | wwJsonSerializer::PropertyNameOverrides

© West Wind Technologies, 1996-2018 • Updated: 05/29/18
Comment or report problem with topic