Function delete_options

Source
pub fn delete_options(
    json: &str,
    path: &str,
    opts: Option<&Options>,
) -> Result<String, SjsonError>
Expand description

DeleteOptions deletes a value from json for the specified path with options.

Examples found in repository?
examples/options.rs (line 42)
3fn main() {
4    println!("=== sjson Options Feature Examples ===\n");
5
6    // 1. Basic Options usage
7    println!("1. Basic Options usage:");
8    let json = r#"{"name":"Tom","age":37}"#;
9    let mut opts = Options::default();
10    opts.optimistic = true;
11    
12    let result = set_options(json, "name", "Jerry", Some(&opts)).unwrap();
13    println!("Original: {}", json);
14    println!("Using optimistic=true: {}", result);
15    println!();
16
17    // 2. Without Options (default behavior)
18    println!("2. Without Options (default behavior):");
19    let result = set_options(json, "name", "Jerry", None).unwrap();
20    println!("Original: {}", json);
21    println!("Without Options: {}", result);
22    println!();
23
24    // 3. Set complex object
25    println!("3. Set complex object:");
26    let json = r#"{"user":{"name":"Tom"}}"#;
27    let mut opts = Options::default();
28    opts.optimistic = true;
29    
30    let complex_value = r#"{"city":"Beijing","country":"China","population":21540000}"#;
31    let result = set_raw_options(json, "user.address", complex_value, Some(&opts)).unwrap();
32    println!("Original: {}", json);
33    println!("Set complex address: {}", result);
34    println!();
35
36    // 4. Delete operation
37    println!("4. Delete operation:");
38    let json = r#"{"name":"Tom","age":37,"city":"Beijing"}"#;
39    let mut opts = Options::default();
40    opts.optimistic = true;
41    
42    let result = delete_options(json, "age", Some(&opts)).unwrap();
43    println!("Original: {}", json);
44    println!("Delete age: {}", result);
45    println!();
46
47    // 5. Performance comparison
48    println!("5. Performance comparison:");
49    let json = r#"{"name":"Tom","age":37,"city":"Beijing","country":"China"}"#;
50    
51    // Using optimistic
52    let mut opts = Options::default();
53    opts.optimistic = true;
54    let start = std::time::Instant::now();
55    for _ in 0..1000 {
56        let _ = set_options(json, "name", "Jerry", Some(&opts)).unwrap();
57    }
58    let optimistic_time = start.elapsed();
59    
60    // Without optimistic
61    let start = std::time::Instant::now();
62    for _ in 0..1000 {
63        let _ = set_options(json, "name", "Jerry", None).unwrap();
64    }
65    let normal_time = start.elapsed();
66    
67    println!("Optimistic mode 1000 operations time: {:?}", optimistic_time);
68    println!("Normal mode 1000 operations time: {:?}", normal_time);
69    println!("Performance improvement: {:.2}x", normal_time.as_nanos() as f64 / optimistic_time.as_nanos() as f64);
70    println!();
71
72    // 6. Error handling
73    println!("6. Error handling:");
74    let json = r#"{"name":"Tom"}"#;
75    let mut opts = Options::default();
76    opts.optimistic = true;
77    
78    match set_options(json, "", "value", Some(&opts)) {
79        Ok(result) => println!("Success: {}", result),
80        Err(e) => println!("Error: {}", e),
81    }
82    
83    match set_options("invalid json", "name", "value", Some(&opts)) {
84        Ok(result) => println!("Success: {}", result),
85        Err(e) => println!("Error: {}", e),
86    }
87}