Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for serde_json::Value #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ hashbrown = { version = "^0.9", optional = true }
chrono = { version = "^0.4", optional = true }
tokio = { version = "^1.1", optional = true, default-features = false }
actix = { version = "^0.11.0", optional = true, default-features = false }
serde_json = { version = "^1.0.89", optional = true, default-features = false }

[dev-dependencies]
deepsize_derive = { path = "deepsize_derive", version = "0.1.1" }

[features]
default = ["std", "derive"]
derive = ["deepsize_derive"]
std = []
std = ["serde_json?/std"]
serde_json = ["indexmap", "dep:serde_json"]
tokio_net = ["tokio", "tokio/net"]
66 changes: 66 additions & 0 deletions src/external_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,69 @@ mod actix_impl {
}
}
}

#[cfg(feature = "serde_json")]
mod serde_json_impl {
use crate::{known_deep_size, Context, DeepSizeOf};
use alloc::collections::BTreeMap;
use core::mem::size_of;
use serde_json::{Map, Number, Value};

impl DeepSizeOf for Value {
fn deep_size_of_children(&self, context: &mut Context) -> usize {
match self {
Value::Null => 0,
Value::Bool(x) => x.deep_size_of_children(context),
Value::Number(x) => x.deep_size_of_children(context),
Value::String(x) => x.deep_size_of_children(context),
Value::Array(x) => x.deep_size_of_children(context),
Value::Object(x) => x.deep_size_of_children(context),
}
}
}

known_deep_size!(0;
Number,
);

impl DeepSizeOf for Map<String, Value> {
fn deep_size_of_children(&self, context: &mut Context) -> usize {
use crate::{BTREE_B, BTREE_MAX, BTREE_MIN};
use indexmap::IndexMap;

// Either it is a BTreeMap or an IndexMap, which have different sizes
// It would be nice if we could tell this via cfg(...), but it seems
// that isn't possible :(.

// But if they are the same size, then gg.
assert_ne!(
size_of::<BTreeMap<String, Value>>(),
size_of::<IndexMap<String, Value>>()
);

if size_of::<BTreeMap<String, Value>>() == size_of::<Map<String, Value>>() {
// Then its a BTreeMap actually
let element_size = self.iter().fold(0, |sum, (k, v)| {
sum + k.deep_size_of_children(context) + v.deep_size_of_children(context)
});
let overhead = size_of::<(
usize,
u16,
u16,
[(String, Value); BTREE_MAX],
[usize; BTREE_B],
)>();
element_size + self.len() * overhead * 2 / (BTREE_MAX + BTREE_MIN)
} else {
// Then it's an IndexMap actually
let child_sizes = self.iter().fold(0, |sum, (key, val)| {
sum + key.deep_size_of_children(context) + val.deep_size_of_children(context)
});
let estimated_cap = self.len().saturating_mul(2);
let map_size =
estimated_cap * (size_of::<(usize, String, Value)>() + size_of::<usize>());
child_sizes + map_size
}
}
}
}