17 releases

0.1.16 Mar 29, 2024
0.1.15 Mar 8, 2024
0.1.13 Feb 15, 2024
0.1.10 Jan 29, 2024
0.1.3 Dec 26, 2023

#417 in Encoding

Download history 3/week @ 2023-12-30 6/week @ 2024-01-13 25/week @ 2024-01-20 9/week @ 2024-01-27 1/week @ 2024-02-03 142/week @ 2024-02-10 27/week @ 2024-02-17 18/week @ 2024-02-24 217/week @ 2024-03-02 103/week @ 2024-03-09 4/week @ 2024-03-16 76/week @ 2024-03-23 52/week @ 2024-03-30 8/week @ 2024-04-06

136 downloads per month

MIT/Apache

130KB
3K SLoC

JSON Predicate

release Crates.io version dependency status docs.rs docs PRs Welcome

Partial implementation of Snell Json Predicate Draft 07.

Even if this Draft is Expired, it's still a pretty good draft of predicate over JSON.

Introduction

This specification defines JSON Predicates, a JSON-based RFC4627 syntax for the description and serialization of logical boolean predicate operations intended to be used in conjunction with other JSON-based mechanisms, such as JSON Patch RFC6902, as a means of incorporating conditional processing.

JSON Predicates can be used, for instance, to extend a JSON Patch RFC6902 document to provide for a broader range of conditional processing options not currently supported by JSON Patch.

Example

Given this JSON:

{
    "a": {
        "b": {
            "c": "ABC!XYZ"
        }
    }
}

We could have a predicate like this:

{
    "op": "and",
    "path": "/a/b/c",
    "apply": [
        {
            "op": "type",
            "value": "string"
        },
        {
            "op": "contains",
            "value": "ABC"
        }
    ]
}

which would evaluate as true if evaluated against the previous JSON.

Rust Example

let predicate = Predicate::deserialize(serde_json::json!({
    "op": "and",
    "path": "/objA",
    "apply": [
      {
        "op": "defined",
        "path": "/stringX"
      },
      {
        "op": "defined",
        "path": "/stringXYZ"
      }
    ],
}))?;

let evaluted_predicate: bool = predicate
    .test(&ENTRY, PredicateContext::default());

JSON Patch

The JSON Patch methods described in draft-snell-json-test-07 are not implemented yet.

Features

First order predicate

  • "contains"
  • "contains-"
  • "defined"
  • "undefined"
  • "starts"
  • "starts-"
  • "ends"
  • "ends-"
  • "type"
  • "in"
  • "in-"
  • "matches"
  • "matches-"
  • "test"
  • "test-"
  • "less"
  • "more"

Second order predicate

  • "and"
  • "not"
  • "or"

References

License

Licensed under either of

Dependencies

~6–8.5MB
~148K SLoC