Sample code for 30+ languages & platforms
PureBasic

Using Pre-defined JSON Templates

See more JSON Examples

Demonstrates how to predefine a JSON template, and then use it to emit JSON with variable substitutions.

Note: This example requires Chilkat v9.5.0.67 or greater.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHashtable.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    ; One way to create JSON is to do it in a straightforward manner:
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)
    CkJsonObject::ckUpdateString(json,"id","0001")
    CkJsonObject::ckUpdateString(json,"type","donut")
    CkJsonObject::ckUpdateString(json,"name","Cake")
    CkJsonObject::ckUpdateString(json,"image.url","images/0001.jpg")
    CkJsonObject::ckUpdateInt(json,"image.width",200)
    CkJsonObject::ckUpdateInt(json,"image.height",200)
    CkJsonObject::ckUpdateString(json,"thumbnail.url","images/thumbnails/0001.jpg")
    CkJsonObject::ckUpdateInt(json,"thumbnail.width",32)
    CkJsonObject::ckUpdateInt(json,"thumbnail.height",32)
    Debug CkJsonObject::ckEmit(json)

    ; The JSON created by the above code:

    ; 	{ 
    ; 	  "id": "0001",
    ; 	  "type": "donut",
    ; 	  "name": "Cake",
    ; 	  "image": { 
    ; 	    "url": "images/0001.jpg",
    ; 	    "width": 200,
    ; 	    "height": 200
    ; 	  },
    ; 	  "thumbnail": { 
    ; 	    "url": "images/thumbnails/0001.jpg",
    ; 	    "width": 32,
    ; 	    "height": 32
    ; 	  }
    ; 	}

    ; An alternative is to predefine a template, and then use it to emit with variable substitutions.
    ; For example:

    jsonTemplate.i = CkJsonObject::ckCreate()
    If jsonTemplate.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonTemplate,"id","{$id}")
    CkJsonObject::ckUpdateString(jsonTemplate,"type","donut")
    CkJsonObject::ckUpdateString(jsonTemplate,"name","{$name}")
    CkJsonObject::ckUpdateString(jsonTemplate,"image.url","{$imageUrl}")
    ; The "i." indicates that it's an integer variable.
    CkJsonObject::ckUpdateString(jsonTemplate,"image.width","{$i.imageWidth}")
    CkJsonObject::ckUpdateString(jsonTemplate,"image.height","{$i.imageHeight}")
    CkJsonObject::ckUpdateString(jsonTemplate,"thumbnail.url","{$thumbUrl}")
    CkJsonObject::ckUpdateString(jsonTemplate,"thumbnail.width","{$i.thumbWidth}")
    CkJsonObject::ckUpdateString(jsonTemplate,"thumbnail.height","{$i.thumbHeight}")
    ; Give this template a name.
    CkJsonObject::ckPredefine(jsonTemplate,"donut")

    ; --------------------------------------------------------------------------
    ; OK, the template is defined.  Defining a template can be done once
    ; at the start of your program, and you can discard the jsonTemplate object (it
    ; doesn't need to stick around..)

    ; Now we can create instances of the JSON object by name:
    jsonDonut.i = CkJsonObject::ckCreate()
    If jsonDonut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(jsonDonut, 0)
    CkJsonObject::ckLoadPredefined(jsonDonut,"donut")
    Debug CkJsonObject::ckEmit(jsonDonut)

    ; The output is this:

    ; 	{ 
    ; 	  "id": "{$id}",
    ; 	  "type": "donut",
    ; 	  "name": "{$name}",
    ; 	  "image": { 
    ; 	    "url": "{$imageUrl}",
    ; 	    "width": "{$i.imageWidth}",
    ; 	    "height": "{$i.imageHeight}"
    ; 	  },
    ; 	  "thumbnail": { 
    ; 	    "url": "{$thumbUrl}",
    ; 	    "width": "{$i.thumbWidth}",
    ; 	    "height": "{$i.thumbHeight}"
    ; 	  }
    ; 	}

    ; Finally, we can substitute variables like this:
    donutValues.i = CkHashtable::ckCreate()
    If donutValues.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHashtable::ckAddStr(donutValues,"id","0001")
    CkHashtable::ckAddStr(donutValues,"name","Cake")
    CkHashtable::ckAddStr(donutValues,"imageUrl","images/0001.jpg")
    CkHashtable::ckAddInt(donutValues,"imageWidth",200)
    CkHashtable::ckAddInt(donutValues,"imageHeight",200)
    CkHashtable::ckAddStr(donutValues,"thumbUrl","images/thumbnails/0001.jpg")
    CkHashtable::ckAddInt(donutValues,"thumbWidth",32)
    CkHashtable::ckAddInt(donutValues,"thumbHeight",32)

    ; Emit with variable substitutions:
    omitEmpty.i = 1
    Debug CkJsonObject::ckEmitWithSubs(jsonDonut,donutValues,omitEmpty)

    ; Output:

    ; 	{ 
    ; 	  "id": "0001",
    ; 	  "type": "donut",
    ; 	  "name": "Cake",
    ; 	  "image": { 
    ; 	    "url": "images/0001.jpg",
    ; 	    "width": 200,
    ; 	    "height": 200
    ; 	  },
    ; 	  "thumbnail": { 
    ; 	    "url": "images/thumbnails/0001.jpg",
    ; 	    "width": 32,
    ; 	    "height": 32
    ; 	  }
    ; 	}

    ; Change some of the values:
    CkHashtable::ckAddStr(donutValues,"id","0002")
    CkHashtable::ckAddStr(donutValues,"imageUrl","images/0002.jpg")
    CkHashtable::ckAddStr(donutValues,"thumbUrl","images/thumbnails/0002.jpg")

    Debug CkJsonObject::ckEmitWithSubs(jsonDonut,donutValues,omitEmpty)

    ; Output:

    ; 	{ 
    ; 	  "id": "0002",
    ; 	  "type": "donut",
    ; 	  "name": "Cake",
    ; 	  "image": { 
    ; 	    "url": "images/0002.jpg",
    ; 	    "width": 200,
    ; 	    "height": 200
    ; 	  },
    ; 	  "thumbnail": { 
    ; 	    "url": "images/thumbnails/0002.jpg",
    ; 	    "width": 32,
    ; 	    "height": 32
    ; 	  }
    ; 	}


    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonTemplate)
    CkJsonObject::ckDispose(jsonDonut)
    CkHashtable::ckDispose(donutValues)


    ProcedureReturn
EndProcedure