Unicode C
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
#include <C_CkJsonObjectW.h>
#include <C_CkHashtableW.h>
void ChilkatSample(void)
{
HCkJsonObjectW json;
HCkJsonObjectW jsonTemplate;
HCkJsonObjectW jsonDonut;
HCkHashtableW donutValues;
BOOL omitEmpty;
// One way to create JSON is to do it in a straightforward manner:
json = CkJsonObjectW_Create();
CkJsonObjectW_putEmitCompact(json,FALSE);
CkJsonObjectW_UpdateString(json,L"id",L"0001");
CkJsonObjectW_UpdateString(json,L"type",L"donut");
CkJsonObjectW_UpdateString(json,L"name",L"Cake");
CkJsonObjectW_UpdateString(json,L"image.url",L"images/0001.jpg");
CkJsonObjectW_UpdateInt(json,L"image.width",200);
CkJsonObjectW_UpdateInt(json,L"image.height",200);
CkJsonObjectW_UpdateString(json,L"thumbnail.url",L"images/thumbnails/0001.jpg");
CkJsonObjectW_UpdateInt(json,L"thumbnail.width",32);
CkJsonObjectW_UpdateInt(json,L"thumbnail.height",32);
wprintf(L"%s\n",CkJsonObjectW_emit(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 = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(jsonTemplate,L"id",L"{$id}");
CkJsonObjectW_UpdateString(jsonTemplate,L"type",L"donut");
CkJsonObjectW_UpdateString(jsonTemplate,L"name",L"{$name}");
CkJsonObjectW_UpdateString(jsonTemplate,L"image.url",L"{$imageUrl}");
// The "i." indicates that it's an integer variable.
CkJsonObjectW_UpdateString(jsonTemplate,L"image.width",L"{$i.imageWidth}");
CkJsonObjectW_UpdateString(jsonTemplate,L"image.height",L"{$i.imageHeight}");
CkJsonObjectW_UpdateString(jsonTemplate,L"thumbnail.url",L"{$thumbUrl}");
CkJsonObjectW_UpdateString(jsonTemplate,L"thumbnail.width",L"{$i.thumbWidth}");
CkJsonObjectW_UpdateString(jsonTemplate,L"thumbnail.height",L"{$i.thumbHeight}");
// Give this template a name.
CkJsonObjectW_Predefine(jsonTemplate,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:
jsonDonut = CkJsonObjectW_Create();
CkJsonObjectW_putEmitCompact(jsonDonut,FALSE);
CkJsonObjectW_LoadPredefined(jsonDonut,L"donut");
wprintf(L"%s\n",CkJsonObjectW_emit(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 = CkHashtableW_Create();
CkHashtableW_AddStr(donutValues,L"id",L"0001");
CkHashtableW_AddStr(donutValues,L"name",L"Cake");
CkHashtableW_AddStr(donutValues,L"imageUrl",L"images/0001.jpg");
CkHashtableW_AddInt(donutValues,L"imageWidth",200);
CkHashtableW_AddInt(donutValues,L"imageHeight",200);
CkHashtableW_AddStr(donutValues,L"thumbUrl",L"images/thumbnails/0001.jpg");
CkHashtableW_AddInt(donutValues,L"thumbWidth",32);
CkHashtableW_AddInt(donutValues,L"thumbHeight",32);
// Emit with variable substitutions:
omitEmpty = TRUE;
wprintf(L"%s\n",CkJsonObjectW_emitWithSubs(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:
CkHashtableW_AddStr(donutValues,L"id",L"0002");
CkHashtableW_AddStr(donutValues,L"imageUrl",L"images/0002.jpg");
CkHashtableW_AddStr(donutValues,L"thumbUrl",L"images/thumbnails/0002.jpg");
wprintf(L"%s\n",CkJsonObjectW_emitWithSubs(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
// }
// }
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonTemplate);
CkJsonObjectW_Dispose(jsonDonut);
CkHashtableW_Dispose(donutValues);
}