serde_filter/lib.rs
1#![feature(associated_type_defaults)]
2
3//! # Overview
4//! `serde_filter` is a library crate that provides filtering abstractions for JSON objects and arrays
5//! using `serde` as a backend. It allows you to easily filter and transform complex JSON structures by
6//! providing a set of configurable filters.
7//!
8//! The crate provides a number of out-of-the-box filters for common use cases, such as filtering by key
9//! or value, flattening nested objects and arrays, and more. You can also implement your own filters
10//! by implementing the `Filter` trait.
11//!
12//! ## Using Pre-Built Filters
13//! ```
14//! use serde_filter::prelude::*;
15//! use serde_json::json;
16//!
17//! fn main() where {
18//! let json = serde_json::json!({
19//! "Object" : {
20//! "explanation": "test",
21//! "activeRegionNum": 9876897,
22//! },
23//! "2022": {
24//! "Object" : {
25//! "explanation": "test",
26//! "activeRegionNum": 1380402,
27//! }
28//! }
29//! });
30//! let nums: Vec<u64> = filter::<Match<u64>>(json, &Match::new("activeRegionNum")).unwrap();
31//! assert_eq!(nums, vec![9876897u64, 1380402u64]);
32//! }
33//! ```
34
35/// The Filter trait and Adhoc filter function
36pub mod filter;
37/// Flattens a JSON object into a single level
38/// ### Example
39/// ```no_run
40/// use serde_filter::prelude::*;
41///
42/// let json = serde_json::json!({
43/// "a": {
44/// "b": {
45/// "c": {
46/// "d": "value"
47/// }
48/// }
49/// },
50/// "e": "value"
51/// });
52///
53/// let expected = serde_json::json!({
54/// "a.b.c.d": "value",
55/// "e": "value"
56/// });
57///
58/// let flattener = Flatten::default(); // default delimiter is '.'
59/// let result = filter::<Flatten>(json, &flattener).unwrap();
60/// println!("{:?}", result);
61/// assert_eq!(result, expected);
62pub mod flatten;
63/// The Ignore filter
64pub mod ignore;
65/// The Match filter
66pub mod matches;
67
68/// Re-export filters
69pub mod prelude {
70 pub use super::filter::filter;
71 pub use super::filter::Filter;
72 pub use super::flatten::Flatten;
73 pub use super::ignore::Ignore;
74 pub use super::matches::{Match, Matchable};
75}
76
77#[cfg(test)]
78mod filter_test {
79 use super::prelude::{filter, Match};
80
81 #[test]
82 fn test_match() {
83 let json = serde_json::json!({
84 "explanation": "test",
85 "date": "2020-01-01",
86 "title": "test",
87 "url": "test",
88 "media_type": "test",
89 "hdurl": "test",
90 "service_version": "test",
91 "code": 200,
92 "msg": "test"
93 });
94 let values = filter::<Match<String>>(json.clone(), &Match::new("explanation")).unwrap();
95 assert_eq!(values, vec!["test".to_string()]);
96
97 let json = serde_json::json!({
98 "Object" : {
99 "explanation": "test",
100 "activeRegionNum": 1,
101 },
102 "2022": {
103 "Object" : {
104 "explanation": "test",
105 "activeRegionNum": 2,
106 }
107 }
108 });
109 let values = filter::<Match<u64>>(json, &Match::new("activeRegionNum"));
110 if let Ok(nums) = values {
111 assert_eq!(nums, vec![1, 2]);
112 } else {
113 panic!("Failed to match");
114 }
115 }
116}