wwJsonSerializer::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.

This reduces the overhead in code having to explicitly set the propery names in proper case when creating dynamic objects.

Note that this method is not required to create serializable objects - any FoxPro object instance will work. However, it's quite common to create dynamic structures at runtime in order to send data to servers that have to match a specific structure. Use this function if you were already using ADDOBJECT() and were manually setting PropertyNameOverrides. If you are using concrete classes created with DEFINE CLASS, this mechanism is not applicable.

o.Property(loObject,lcProperty,lvValue)

Parameters

loObject
The object on which to add the property

lcProperty
The name of the property to add to the object.

lvValue
The value to set the object to. If not specified the value is .F.

Remarks

If you re-use the wwJsonSerializer instance, make sure to clear out the PropertyNameOverrides property before the next call.

Example

do wwJsonSerializer

*** Create the serializer
LOCAL loSer as wwJsonSerializer
loSer = CREATEOBJECT("wwJsonSerializer")

loRec = CREATEOBJECT("Empty")

*** Create matching properties
loSer.Property(loRec,"AgencyID","021211")
loSer.Property(loRec,"TransactionID","50000156")
loSer.Property(loRec,"PublicLocation",1)
loSer.Property(loRec, "CallReceivedDateTime",DATETIME())

*** Create the child array 
loUnits = CREATEOBJECT("Collection")
loSer.Property(loRec, "Unit",loUnits)

FOR lnX = 1 TO 3v
   loUnit = CREATEOBJECT("EMPTY")   
   loSer.Property(loUnit,"UnitDispatchNumber","10006733")
   loSer.Property(loUnit,"UnitId",TRANSFORM(lnX))
   loSer.Property(loUnit,"PulseDispatchStatus","ER")
   loSer.Property(loUnit,"UnitEnrouteDateTime",DATETIME())   
   loUnits.Add(loUnit)
ENDFOR

*** Serialize object to a string
lcJson = loSer.Serialize(loRec)

This produces an object with proper casing:

{ 
   "AgencyID":"07035",
   "CallReceivedDateTime":"2013-11-12T03:06:36Z",
   "PublicLocation":1,
   "TransactionID":"50000156",
   "Unit":[
    {"PulseDispatchStatus":"ER","UnitDispatchNumber":"10006733",
     "UnitEnrouteDateTime":"2013-11-12T03:06:36Z","UnitId":"1"},
    {"PulseDispatchStatus":"ER","UnitDispatchNumber":"10006733",
     "UnitEnrouteDateTime":"2013-11-12T03:06:36Z","UnitId":"2"},
    {"PulseDispatchStatus":"ER","UnitDispatchNumber":"10006733",
     "UnitEnrouteDateTime":"2013-11-12T03:06:36Z","UnitId":"3"}
   ]
}

See also:

Class wwJsonSerialize

© West Wind Technologies, 2019 • Updated: 02/06/19
Comment or report problem with topic