#json-schema #mask #json #schema #json-object #filter

json_mask

Filter a JSON object as a tree to a subset using a JSON Schema as a mask

1 unstable release

0.0.1 Sep 20, 2023

#15 in #mask

MIT/Apache

18KB
397 lines

json_mask

Filter a JSON object as a tree to a subset using a JSON Schema as a mask.

See docs.rs for full details.

License

This project is licensed under either of

at your option.

The SPDX license identifier for this project is MIT OR Apache-2.0.


lib.rs:

githubcrates-iodocs-rs


This library provides mask::JsonMasker which accepts a JSON Schema document.

An example use case is an API where the backend generates the latest response version, but applies the mask to transform the latest response into other API versions to satisfy backwards compatibility.

This is an early build where the input validation is more flexible. Based on real world usage the first stable release will probably restrict to formal schema draft standards and/or allow you to restrict to specific ones.

Examples

  • Use generate a mask with from_str or from_reader and apply it to a document.

    use json_mask::from_str;
    use json_mask::JsonMasker;
    use json_mask::ValidJsonSchema;
    
    let mut document = serde_json::from_str(r#"{ "foo": 1, "bar": 2}"#).unwrap();
    let schema = r#"
    {
      "$schema": "http://json-schema.org/draft-04/schema",
      "title": "Demo Schema",
      "description": "Demo",
      "type": "object",
      "properties": {
        "foo": {
          "type": "integer"
        }
      }
    }
    "#;
    
    let mask = from_str(schema).unwrap();
    let masker = JsonMasker::new(mask);
    masker.mask(&mut document);
    
    assert_eq!(r#"{"foo":1}"#, serde_json::to_string(&document).unwrap())
    

Dependencies

~10–18MB
~249K SLoC