Zig
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// One way to create JSON is to do it in a straightforward manner:
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.setEmitCompact(false);
json.updateString("id", "0001") catch {};
json.updateString("type", "donut") catch {};
json.updateString("name", "Cake") catch {};
json.updateString("image.url", "images/0001.jpg") catch {};
json.updateInt("image.width", 200) catch {};
json.updateInt("image.height", 200) catch {};
json.updateString("thumbnail.url", "images/thumbnails/0001.jpg") catch {};
json.updateInt("thumbnail.width", 32) catch {};
json.updateInt("thumbnail.height", 32) catch {};
std.debug.print("{s}\n", .{try json.emit(alloc)});
// 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:
const json_template = try chilkat.JsonObject.init();
defer json_template.deinit();
json_template.updateString("id", "{$id}") catch {};
json_template.updateString("type", "donut") catch {};
json_template.updateString("name", "{$name}") catch {};
json_template.updateString("image.url", "{$imageUrl}") catch {};
// The "i." indicates that it's an integer variable.
json_template.updateString("image.width", "{$i.imageWidth}") catch {};
json_template.updateString("image.height", "{$i.imageHeight}") catch {};
json_template.updateString("thumbnail.url", "{$thumbUrl}") catch {};
json_template.updateString("thumbnail.width", "{$i.thumbWidth}") catch {};
json_template.updateString("thumbnail.height", "{$i.thumbHeight}") catch {};
// Give this template a name.
json_template.predefine("donut") catch {};
// --------------------------------------------------------------------------
// 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:
const json_donut = try chilkat.JsonObject.init();
defer json_donut.deinit();
json_donut.setEmitCompact(false);
json_donut.loadPredefined("donut") catch {};
std.debug.print("{s}\n", .{try json_donut.emit(alloc)});
// 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:
const donut_values = try chilkat.Hashtable.init();
defer donut_values.deinit();
donut_values.addStr("id", "0001") catch {};
donut_values.addStr("name", "Cake") catch {};
donut_values.addStr("imageUrl", "images/0001.jpg") catch {};
donut_values.addInt("imageWidth", 200) catch {};
donut_values.addInt("imageHeight", 200) catch {};
donut_values.addStr("thumbUrl", "images/thumbnails/0001.jpg") catch {};
donut_values.addInt("thumbWidth", 32) catch {};
donut_values.addInt("thumbHeight", 32) catch {};
// Emit with variable substitutions:
const omit_empty = true;
std.debug.print("{s}\n", .{try json_donut.emitWithSubs(alloc, donut_values, omit_empty)});
// 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:
donut_values.addStr("id", "0002") catch {};
donut_values.addStr("imageUrl", "images/0002.jpg") catch {};
donut_values.addStr("thumbUrl", "images/thumbnails/0002.jpg") catch {};
std.debug.print("{s}\n", .{try json_donut.emitWithSubs(alloc, donut_values, omit_empty)});
// 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
// }
// }
}