simple-json-filter 0.1.0

simple json filter usindg serde_json
Documentation
  • Coverage
  • 75%
    3 out of 4 items documented0 out of 3 items with examples
  • Size
  • Source code size: 14.9 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 402.96 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • asyrafduyshart

Rust JSON Simple Filter Library

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.

Features

  • Define filters using simple string syntax.
  • Apply multiple filters to JSON data.
  • Filters can use string comparison, numeric comparison, or both.
  • Optionally use multipliers for numeric comparisons.

Usage

Defining Filters

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:

  • The field to be filtered is prefixed with a dot (.).
  • The operator can be one of: =, !=, >, <, >=, <=.
  • The value to be compared can be a string (surrounded by ') or a number.

Parsing Filters

Use the parse function to parse a filter string into a list of Filter structs:

let filters = parse(filter_string).unwrap();

Applying Filters

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.

Example

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));

Testing

The library includes a test suite to validate the functionality. Run the tests with cargo test.