'how to combine (including nested array values) two serde_yaml::Value objects?

I am having trouble creating a recursive function to parse two serde_yaml::Value variables and combine them. It's easy enough to combine them at a base-level object, but then the sublevel values are only those of the combined value.

Given:

let original:serde_yaml::Value = serde_yaml::from_str(r#"
keyA:
  subKeyA:
    - A
    - B
    - C
keyB: "one"
keyC: "a"
"#
).unwrap();

let add_or_modify_these_values:serde_yaml::Value = serde_yaml::from_str(r#"
keyA:
  subKeyA:
    - D
  subKeyB:
    - BA
keyB: "two"
keyC:
  - A
  - B
"#
).unwrap();

How would I combine them so all nested properties are accounted for, eg:

keyA:
  subKeyA:
    - A
    - B
    - C
    - D
  subKeyB:
    - BA
keyB: "two"
keyC:
  - A
  - B

When there are complications (for example, different value types like keyC) I'd prefer overwriting the original value type with the new one.

Edit: I've also looked at a similar question for json here: How can I merge two JSON objects with Rust? but that merge method will not combine array values, only overwrite.



Solution 1:[1]

Edit: Proper answer is marked. Will leave this one for the JSON version.


The json version:

fn merge(a: &mut serde_json::Value, b: serde_json::Value) {
    match (a, b) {
        (a @ &mut serde_json::Value::Object(_), serde_json::Value::Object(b)) => {
            let a = a.as_object_mut().unwrap();
            for (k, v) in b {
                if v.is_array() && a.contains_key(&k) && a.get(&k).as_ref().unwrap().is_array() {
                           let mut _a = a.get(&k).unwrap().as_array().unwrap().to_owned();
                           _a.append(&mut v.as_array().unwrap().to_owned());
                           a[&k] = serde_json::Value::from(_a);
                }
                else{   
                    merge(a.entry(k).or_insert(serde_json::Value::Null), v);
                }
            }
        }
        (a, b) => *a = b,
    }
}

Solution 2:[2]

Here's a cleaned-up version (used with #![feature(let_chains)]):

    fn merge_yaml(a: &mut serde_yaml::Value, b: serde_yaml::Value) {
        match (a, b) {
            (serde_yaml::Value::Mapping(ref mut a), serde_yaml::Value::Mapping(b)) => {
                for (k, v) in b {
                    if let Some(b_seq) = v.as_sequence()
                        && let Some(a_val) = a.get(&k)
                        && let Some(a_seq) = a_val.as_sequence()
                    {
                        a[&k] = [a_seq.as_slice(), b_seq.as_slice()].concat().into();
                        continue;
                    }

                    if !a.contains_key(&k) {
                        a.insert(k, v);
                    }
                    else {
                        Self::merge_yaml(&mut a[&k], v);
                    }
                }

            }
            (a, b) => *a = b,
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Parallel Xenoexcite