use serde_json::Value;
use std::mem::size_of;
pub(super) fn calculate_memory_usage(value: &Value) -> usize {
let mut total = size_of::<Value>(); add_heap_usage(value, &mut total);
total
}
fn add_heap_usage(value: &Value, total: &mut usize) {
match value {
Value::String(s) => {
*total += size_of::<String>();
*total += s.capacity();
},
Value::Array(arr) => {
*total += size_of::<Vec<Value>>() + arr.capacity() * size_of::<Value>();
for item in arr {
add_heap_usage(item, total);
}
},
Value::Object(map) => {
for (key, value) in map {
*total += size_of::<String>() + key.capacity();
*total += size_of::<Value>();
add_heap_usage(value, total);
}
},
Value::Number(_) | Value::Null | Value::Bool(_) => {},
}
}