Skip to content

Commit 0fcdbfd

Browse files
committed
add a serde_json example
1 parent 6464c13 commit 0fcdbfd

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

examples/json.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#[cfg(feature = "with_serde")]
2+
fn main() {
3+
let mut estimator =
4+
cardinality_estimator::CardinalityEstimator::<usize, wyhash::WyHash, 8, 5>::new();
5+
6+
println!(
7+
"serialized empty estimator (small): {}",
8+
serde_json::to_string_pretty(&estimator).unwrap()
9+
);
10+
11+
estimator.insert(&0);
12+
13+
println!(
14+
"serialized with one insert (small): {}",
15+
serde_json::to_string_pretty(&estimator).unwrap()
16+
);
17+
18+
estimator.insert(&1);
19+
estimator.insert(&2);
20+
21+
println!(
22+
"serialized with three inserts (array): {}",
23+
serde_json::to_string_pretty(&estimator).unwrap()
24+
);
25+
26+
for i in 3..1000 {
27+
estimator.insert(&i);
28+
}
29+
30+
println!(
31+
"serialized with many inserts (HLL): {}",
32+
serde_json::to_string_pretty(&estimator).unwrap()
33+
);
34+
}
35+
36+
#[cfg(not(feature = "with_serde"))]
37+
fn main() -> Result<(), u32> {
38+
eprintln!("this example requires --features with_serde");
39+
Err(1)
40+
}

0 commit comments

Comments
 (0)