Skip to main content

insert_field

Function insert_field 

Source
pub fn insert_field(
    obj: &mut Map<String, Value>,
    key: &str,
    value: Option<&str>,
)
Expand description

Insert a string field into a JSON object only when the value is present.

ยงExamples

use codetether_agent::session::helper::runtime::insert_field;
use serde_json::{Map, Value};

let mut obj = Map::<String, Value>::new();
insert_field(&mut obj, "example", Some("value"));
insert_field(&mut obj, "missing", None);

assert_eq!(obj["example"], "value");
assert!(!obj.contains_key("missing"));