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

Remarks

Note that there are two deserialization methods:

  • DeserializeJson (recommened!)
    This method uses a .NET wrapper using wwDotnetBridge and a third party JSON library, plus an intermediary parser that turns the .NET object into a FoxPro accessible object. This mechanism is much more efficient and reliable than the FoxPro based parser and therefore the recommended method for deserialization.

    Requires that you distribute wwIpStuff.dll and wwDotnetBridge.dll.

  • Deserialize
    The base (classic) method that deserializes JSON that uses raw FoxPro code. This works fairly well with basic types and JSON serialized by the JavaScript serializers. It doesn't work very well with custom formatted JSON and JSON that contains multiple or nested arrays.

Class Members

MemberDescription

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

Formats a raw JSON string to be indented for easier reading and display.

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

Serializes a FoxPro value to a JSON string. Supports all VFP basic types, most complex objects (as long as there are no circular references), simple arrays and collections and cursors.

o.Serialize(lvValue)

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.

PropertyExclusionList

Comma delimited, lower case list of object properties that should not be serialized when serializing objects.

PropertyNameOverrides

Comma delimited list of property names that you like to override on the object to be serialized. Used to force property names into proper case rather than the default lower case only representation.

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-2016 • Updated: 01/11/16
Comment or report problem with topic