Crates.io | simple-json-filter |
lib.rs | simple-json-filter |
version | 0.1.0 |
source | src |
created_at | 2023-05-31 08:52:05.569207 |
updated_at | 2023-05-31 08:52:05.569207 |
description | simple json filter usindg serde_json |
homepage | |
repository | |
max_upload_size | |
id | 878619 |
size | 12,987 |
This library provides functionality to apply a series of filters to JSON data structures in Rust. The filters are defined by a string and parsed into a series of Filter
structs, which can then be applied to any JSON data structure.
Filters are defined as a string with a simple syntax:
.field = 'value' AND .other_field >= 10
Each part of the string is separated by " AND " to define multiple filters.
In each filter:
.
).=
, !=
, >
, <
, >=
, <=
.'
) or a number.Use the parse
function to parse a filter string into a list of Filter
structs:
let filters = parse(filter_string).unwrap();
Use the apply
function to apply a list of Filter
structs to a JSON data structure:
let v = json!({ "field": "hello", "value": 20 });
let result = apply(&v, &filters);
This returns true
if the data passes all filters, and false
otherwise.
use serde_json::json;
use json_filter::{parse, apply};
let filter_string = ".field = 'hello' AND .value >= 20";
let filters = parse(filter_string).unwrap();
let v = json!({ "field": "hello", "value": 30 });
assert!(apply(&v, &filters));
let v = json!({ "field": "world", "value": 30 });
assert!(!apply(&v, &filters));
The library includes a test suite to validate the functionality. Run the tests with cargo test
.