Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust
// One way to create JSON is to do it in a straightforward manner:
let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
let _ = json.update_string("id", "0001");
let _ = json.update_string("type", "donut");
let _ = json.update_string("name", "Cake");
let _ = json.update_string("image.url", "images/0001.jpg");
let _ = json.update_int("image.width", 200);
let _ = json.update_int("image.height", 200);
let _ = json.update_string("thumbnail.url", "images/thumbnails/0001.jpg");
let _ = json.update_int("thumbnail.width", 32);
let _ = json.update_int("thumbnail.height", 32);
println!("{}", json.emit().unwrap_or_default());

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

let json_template = chilkat::JsonObject::new();
let _ = json_template.update_string("id", "{$id}");
let _ = json_template.update_string("type", "donut");
let _ = json_template.update_string("name", "{$name}");
let _ = json_template.update_string("image.url", "{$imageUrl}");
// The "i." indicates that it's an integer variable.
let _ = json_template.update_string("image.width", "{$i.imageWidth}");
let _ = json_template.update_string("image.height", "{$i.imageHeight}");
let _ = json_template.update_string("thumbnail.url", "{$thumbUrl}");
let _ = json_template.update_string("thumbnail.width", "{$i.thumbWidth}");
let _ = json_template.update_string("thumbnail.height", "{$i.thumbHeight}");
// Give this template a name.
let _ = json_template.predefine("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:
let json_donut = chilkat::JsonObject::new();
json_donut.set_emit_compact(false);
let _ = json_donut.load_predefined("donut");
println!("{}", json_donut.emit().unwrap_or_default());

// 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:
let donut_values = chilkat::Hashtable::new();
let _ = donut_values.add_str("id", "0001");
let _ = donut_values.add_str("name", "Cake");
let _ = donut_values.add_str("imageUrl", "images/0001.jpg");
let _ = donut_values.add_int("imageWidth", 200);
let _ = donut_values.add_int("imageHeight", 200);
let _ = donut_values.add_str("thumbUrl", "images/thumbnails/0001.jpg");
let _ = donut_values.add_int("thumbWidth", 32);
let _ = donut_values.add_int("thumbHeight", 32);

// Emit with variable substitutions:
let omit_empty = true;
println!("{}", json_donut.emit_with_subs(&donut_values, omit_empty).unwrap_or_default());

// 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:
let _ = donut_values.add_str("id", "0002");
let _ = donut_values.add_str("imageUrl", "images/0002.jpg");
let _ = donut_values.add_str("thumbUrl", "images/thumbnails/0002.jpg");

println!("{}", json_donut.emit_with_subs(&donut_values, omit_empty).unwrap_or_default());

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