use crate::error::{Error, Result};
use serde_json::{Map, Value};
pub fn get<'a>(obj: &'a Value, path: &str) -> Option<&'a Value> {
let parts: Vec<&str> = path.split('.').collect();
let mut current = obj;
for part in parts {
match current {
Value::Object(map) => {
current = map.get(part)?;
}
Value::Array(arr) => {
if let Ok(index) = part.parse::<usize>() {
current = arr.get(index)?;
} else {
return None;
}
}
_ => return None,
}
}
Some(current)
}
pub fn set(obj: &mut Value, path: &str, value: Value) -> Result<()> {
let parts: Vec<&str> = path.split('.').collect();
if parts.is_empty() {
return Err(Error::template("Empty path provided".to_string()));
}
if !obj.is_object() {
*obj = Value::Object(Map::new());
}
let mut current = obj;
for part in &parts[..parts.len() - 1] {
if let Value::Object(map) = current {
let entry = map
.entry(*part)
.or_insert_with(|| Value::Object(Map::new()));
if !entry.is_object() {
*entry = Value::Object(Map::new());
}
current = entry;
} else {
return Err(Error::template(format!(
"Cannot set property '{}' on non-object",
part
)));
}
}
if let Value::Object(map) = current {
let final_key = parts[parts.len() - 1];
map.insert(final_key.to_string(), value);
Ok(())
} else {
Err(Error::template(
"Cannot set property on non-object".to_string(),
))
}
}
pub fn remove_nested_property(obj: &mut Value, path: &str) -> Result<Option<Value>> {
let parts: Vec<&str> = path.split('.').collect();
if parts.is_empty() {
return Err(Error::template("Empty path provided".to_string()));
}
if parts.len() == 1 {
if let Value::Object(map) = obj {
return Ok(map.remove(parts[0]));
} else {
return Err(Error::template(
"Cannot remove property from non-object".to_string(),
));
}
}
let parent_path = parts[..parts.len() - 1].join(".");
let final_key = parts[parts.len() - 1];
if let Some(Value::Object(parent_map)) = get_mut(obj, &parent_path) {
Ok(parent_map.remove(final_key))
} else {
Ok(None)
}
}
fn get_mut<'a>(obj: &'a mut Value, path: &str) -> Option<&'a mut Value> {
let parts: Vec<&str> = path.split('.').collect();
let mut current = obj;
for part in parts {
match current {
Value::Object(map) => {
current = map.get_mut(part)?;
}
Value::Array(arr) => {
if let Ok(index) = part.parse::<usize>() {
current = arr.get_mut(index)?;
} else {
return None;
}
}
_ => return None,
}
}
Some(current)
}
pub fn deep_merge(target: &mut Value, source: &Value) {
match (target.as_object_mut(), source.as_object()) {
(Some(target_map), Some(source_map)) => {
for (key, source_value) in source_map {
match target_map.get_mut(key) {
Some(target_value) => {
deep_merge(target_value, source_value);
}
None => {
target_map.insert(key.clone(), source_value.clone());
}
}
}
}
_ => {
*target = source.clone();
}
}
}
pub fn shallow_merge(target: &mut Value, source: &Value) {
if let (Value::Object(target_map), Value::Object(source_map)) = (target, source) {
for (key, value) in source_map {
target_map.insert(key.clone(), value.clone());
}
}
}
pub fn deep_clone(value: &Value) -> Value {
value.clone()
}
pub fn flatten_object(obj: &Value, prefix: &str) -> Map<String, Value> {
let mut result = Map::new();
match obj {
Value::Object(map) => {
for (key, value) in map {
let new_key = if prefix.is_empty() {
key.clone()
} else {
format!("{}.{}", prefix, key)
};
match value {
Value::Object(_) => {
let nested = flatten_object(value, &new_key);
result.extend(nested);
}
_ => {
result.insert(new_key, value.clone());
}
}
}
}
_ => {
if !prefix.is_empty() {
result.insert(prefix.to_string(), obj.clone());
}
}
}
result
}
pub fn has_nested_property(obj: &Value, path: &str) -> bool {
get(obj, path).is_some()
}
pub fn get_all_keys(obj: &Value) -> Vec<String> {
let flattened = flatten_object(obj, "");
flattened.keys().cloned().collect()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_get() {
let data = json!({
"user": {
"profile": {
"name": "John Doe",
"age": 30
},
"settings": {
"theme": "dark"
}
},
"items": [1, 2, 3]
});
assert_eq!(get(&data, "user.profile.name"), Some(&json!("John Doe")));
assert_eq!(get(&data, "user.settings.theme"), Some(&json!("dark")));
assert_eq!(get(&data, "items.0"), Some(&json!(1)));
assert_eq!(get(&data, "items.2"), Some(&json!(3)));
assert_eq!(get(&data, "user.profile.email"), None);
assert_eq!(get(&data, "missing"), None);
assert_eq!(get(&data, "items.10"), None);
}
#[test]
fn test_set() {
let mut data = json!({});
set(&mut data, "user.profile.name", json!("Jane Doe")).unwrap();
assert_eq!(data["user"]["profile"]["name"], "Jane Doe");
set(&mut data, "user.profile.age", json!(25)).unwrap();
assert_eq!(data["user"]["profile"]["age"], 25);
set(&mut data, "user.settings.theme", json!("light")).unwrap();
assert_eq!(data["user"]["settings"]["theme"], "light");
assert_eq!(data["user"]["profile"]["name"], "Jane Doe");
assert_eq!(data["user"]["profile"]["age"], 25);
}
#[test]
fn test_remove_nested_property() {
let mut data = json!({
"user": {
"name": "John",
"age": 30,
"email": "john@example.com"
}
});
let removed = remove_nested_property(&mut data, "user.age").unwrap();
assert_eq!(removed, Some(json!(30)));
assert!(!has_nested_property(&data, "user.age"));
assert_eq!(data["user"]["name"], "John");
let missing = remove_nested_property(&mut data, "user.phone").unwrap();
assert_eq!(missing, None);
}
#[test]
fn test_deep_merge() {
let mut target = json!({
"a": 1,
"b": {
"c": 2,
"d": 3
}
});
let source = json!({
"b": {
"d": 4,
"e": 5
},
"f": 6
});
deep_merge(&mut target, &source);
assert_eq!(target["a"], 1);
assert_eq!(target["b"]["c"], 2);
assert_eq!(target["b"]["d"], 4); assert_eq!(target["b"]["e"], 5); assert_eq!(target["f"], 6); }
#[test]
fn test_shallow_merge() {
let mut target = json!({
"a": 1,
"b": {"c": 2}
});
let source = json!({
"b": {"d": 3},
"e": 4
});
shallow_merge(&mut target, &source);
assert_eq!(target["a"], 1);
assert_eq!(target["b"]["d"], 3);
assert!(!has_nested_property(&target, "b.c")); assert_eq!(target["e"], 4);
}
#[test]
fn test_flatten_object() {
let nested = json!({
"user": {
"profile": {
"name": "John",
"age": 30
},
"active": true
},
"count": 5
});
let flattened = flatten_object(&nested, "");
assert_eq!(flattened["user.profile.name"], json!("John"));
assert_eq!(flattened["user.profile.age"], json!(30));
assert_eq!(flattened["user.active"], json!(true));
assert_eq!(flattened["count"], json!(5));
}
#[test]
fn test_has_nested_property() {
let data = json!({"user": {"name": "John", "age": 30}});
assert!(has_nested_property(&data, "user"));
assert!(has_nested_property(&data, "user.name"));
assert!(has_nested_property(&data, "user.age"));
assert!(!has_nested_property(&data, "user.email"));
assert!(!has_nested_property(&data, "missing"));
}
#[test]
fn test_get_all_keys() {
let data = json!({
"user": {
"name": "John",
"age": 30
},
"active": true
});
let keys = get_all_keys(&data);
assert!(keys.contains(&"user.name".to_string()));
assert!(keys.contains(&"user.age".to_string()));
assert!(keys.contains(&"active".to_string()));
assert_eq!(keys.len(), 3);
}
}