refactor; add order() fn; add examples

This commit is contained in:
JMARyA 2024-02-09 14:34:12 +01:00
parent d1419a2198
commit 6db5f79743
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
11 changed files with 388 additions and 11 deletions

View file

@ -1,2 +1,32 @@
# JSONFilter
JSONFilter is a rust crate letting you filter JSON objects based on another json object as a filter. Think of MongoDBs `find()` function but as a filter.
`jsonfilter` is a Rust crate designed to facilitate filtering and comparing JSON values based on specified criteria. It provides functions for comparing JSON values, applying filters to JSON objects, and determining if a filter matches a given JSON object. Think of MongoDBs `find()` function but as a filter function.
## Usage
To use `jsonfilter`, add it to your `Cargo.toml` and add the following to your Rust code:
```rust
use jsonfilter::{order, matches};
```
### Comparing JSON Values
You can compare two JSON values using the `order` function:
```rust
use serde_json::json;
use std::cmp::Ordering;
use jsonfilter::order;
let a = json!(10);
let b = json!(5);
assert_eq!(order(&a, &b), Ordering::Greater);
```
### Matching Filters
To check if a JSON object matches a filter, use the `matches` function:
```rust
use serde_json::json;
use jsonfilter::matches;
let filter = json!({"name": "John", "age": 30});
let obj = json!({"name": "John", "age": 30, "city": "New York"});
assert!(matches(&filter, &obj));
```