Sample code for 30+ languages & platforms
Rust

Using JSON StringOf with Non-String Members

See more JSON Examples

If a JSON member is a boolean, integer, or null, using StringOf will return its string representation.

Chilkat Rust Downloads

Rust
// Create JSON with members of different data types.
let json = chilkat::JsonObject::new();

let _ = json.update_int("a", 123);
let _ = json.update_bool("b", true);
let _ = json.update_null("c");

json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());

// Resulting JSON:
// {
//   "a": 123,
//   "b": true,
//   "c": null
// }

let a = json.string_of("a").unwrap_or_default();
println!("{}", a);

let b = json.string_of("b").unwrap_or_default();
println!("{}", b);

let c = json.string_of("c").unwrap_or_default();
println!("{}", c);

// Output

// 123
// true
// null

// If you want to get the integer, boolean, or null value
// you need to use the methods matching the data type
let ival = json.int_of("a");
let bval = json.bool_of("b");
let has_null = json.is_null_of("c");