Sample code for 30+ languages & platforms
Unicode C++

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 Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkHashtableW.h>

void ChilkatSample(void)
    {
    // One way to create JSON is to do it in a straightforward manner:
    CkJsonObjectW json;
    json.put_EmitCompact(false);
    json.UpdateString(L"id",L"0001");
    json.UpdateString(L"type",L"donut");
    json.UpdateString(L"name",L"Cake");
    json.UpdateString(L"image.url",L"images/0001.jpg");
    json.UpdateInt(L"image.width",200);
    json.UpdateInt(L"image.height",200);
    json.UpdateString(L"thumbnail.url",L"images/thumbnails/0001.jpg");
    json.UpdateInt(L"thumbnail.width",32);
    json.UpdateInt(L"thumbnail.height",32);
    wprintf(L"%s\n",json.emit());

    // 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:

    CkJsonObjectW jsonTemplate;
    jsonTemplate.UpdateString(L"id",L"{$id}");
    jsonTemplate.UpdateString(L"type",L"donut");
    jsonTemplate.UpdateString(L"name",L"{$name}");
    jsonTemplate.UpdateString(L"image.url",L"{$imageUrl}");
    // The "i." indicates that it's an integer variable.
    jsonTemplate.UpdateString(L"image.width",L"{$i.imageWidth}");
    jsonTemplate.UpdateString(L"image.height",L"{$i.imageHeight}");
    jsonTemplate.UpdateString(L"thumbnail.url",L"{$thumbUrl}");
    jsonTemplate.UpdateString(L"thumbnail.width",L"{$i.thumbWidth}");
    jsonTemplate.UpdateString(L"thumbnail.height",L"{$i.thumbHeight}");
    // Give this template a name.
    jsonTemplate.Predefine(L"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:
    CkJsonObjectW jsonDonut;
    jsonDonut.put_EmitCompact(false);
    jsonDonut.LoadPredefined(L"donut");
    wprintf(L"%s\n",jsonDonut.emit());

    // 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:
    CkHashtableW donutValues;
    donutValues.AddStr(L"id",L"0001");
    donutValues.AddStr(L"name",L"Cake");
    donutValues.AddStr(L"imageUrl",L"images/0001.jpg");
    donutValues.AddInt(L"imageWidth",200);
    donutValues.AddInt(L"imageHeight",200);
    donutValues.AddStr(L"thumbUrl",L"images/thumbnails/0001.jpg");
    donutValues.AddInt(L"thumbWidth",32);
    donutValues.AddInt(L"thumbHeight",32);

    // Emit with variable substitutions:
    bool omitEmpty = true;
    wprintf(L"%s\n",jsonDonut.emitWithSubs(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:
    donutValues.AddStr(L"id",L"0002");
    donutValues.AddStr(L"imageUrl",L"images/0002.jpg");
    donutValues.AddStr(L"thumbUrl",L"images/thumbnails/0002.jpg");

    wprintf(L"%s\n",jsonDonut.emitWithSubs(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
    // 	  }
    // 	}
    }