Class wwJsonSerializer

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 preferrably as collections.

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
Custom
  wwJsonSerializer

Class Members

MemberDescription
JsonSerialize

o.JsonSerialize(lvValue, llFormat, loSerializer)

JsonDeserialize

o.JsonSerializer(lvValue, llFormat, loSerializer)

DeserializeJson

Deserializes a JSON string into a FoxPro value, object or wwCollection instance for arrays. This method uses .NET and the JSON.NET library.

o.DeserializeJson(lcJson)

FormatJson

o.FormatJson(lcJson)

Property

This method works like FoxPro's ADDPROPERTY() that automatically adds the property name specified to the PropertyNameOverrides so the case is preserved during serialization.

o.Property(loObject,lcProperty,lvValue)

Serialize

o.Serialize(lvValue, llFormat)

AssumeUtcDates

If set to .T. assumes that all dates in the data structure passed are UTC dates and the dates are not time adjusted for UTC time.

FormattedOutput

When .T. causes the output to be pretty formatted with indentations for each object level.

This flag affects the behavior of the Serialize() method.

PropertyExclusionList

PropertyNameCharacterFilter

Property name filter applied to properties that are dynamically created by the deserializer. Strips out invalid characters for FoxPro variable names.

Defaults to all alpha-numeric characters and _.

This property is internally set and generally you shouldn't mess with this value.

PropertyNameOverrides

TrimStringValues

If .T. trims all string values removing trailing spaces to minimize payload size on the wire.

Example

DO wwJsonSerializer && Load libs

*** Create a complex object
loCust = CREATEOBJECT("Empty")
ADDPROPERTY(loCust,"Name","Rick")
ADDPROPERTY(loCust,"Entered",DATETIME())

*** Create a nested object
ADDPROPERTY(loCust,"Address", CREATEOBJECT("Empty"))
ADDPROPERTY(loCust.Address,"Street","32 Kaiea Place")
ADDPROPERTY(loCust,"Number",32)

loSer = CREATEOBJECT("wwJsonSerializer")

*** Serialize into JSON
lcJson =  loSer.Serialize(loCust)

? lcJson
*{"address":{"street":"32 Kaiea Place"},"entered":"2012-05-12T03:55:35Z","name":"Rick"}

*** read back from JSON into an object
loCust2 = loSer.DeserializeJson(lcJson)

? loCust2.Name
? loCust2.Entered
? loCust2.Address.Street
? loCust2.Address.Number

You can also serialize cursors:

select * from customers ;
    into cursor TCustomers ;
    order by company

lcJson = loSer.Serialize("cursor:TCustomers")

Requirements

Assembly: wwjsonserializer.prg

© West Wind Technologies, 1996-2018 • Updated: 10/09/17
Comment or report problem with topic