Creating Dynamic Objects for Serialization with wwDynamic

If you need to create JSON structures to serialize from code, you can create FoxPro objects and then serialize them to JSON which ensures that the JSON values are properly serialized.

To make this task easier and more consistent you can use the wwDynamic class which allows creating objects at runtime using a more fluent syntax. wwDynamic allows to just add properties to an object to create it.

Take this JSON for example:

    "name": "Rick"
    "company": "West Wind",
    "address": {
        "street": "10b Northwest Wind Houndage Rd",
        "city": "Paia"
        "state": "HI",
        "postalCode": "96779"

To create this via FoxPro code:

DO wwJsonSerializer
DO wwDynamic

loCust = CREATEOBJECT("wwDynamic")

*** Properties are all created lower case in JSON = "Rick" = "West Wind"
loCust.address.street = "10b Houdage Road" = "Paia"

*** Mixed or other case have to explicitly add property
*** to get the proper mixed case name in JSON

loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(loCust,.T.)
? lcJson

This produces:

  "address": {
    "city": "Paia",
    "postalCode": "96779",
    "street": "10b Houdage Road"
  "company": "West Wind"

© West Wind Technologies, 2023 • Updated: 02/14/20
Comment or report problem with topic