#json-path #json #query #lua

jsonpath_lib_polars_vendor

这是一个用Rust编写的JsonPath引擎,它还提供了Webassembly和Javascript的类似API接口。- Webassembly演示:https://freestrings.github.io/jsonpath

1个不稳定版本

使用旧的Rust 2015

0.0.1 2024年2月29日

5#jsonpath

Download history 1223/week @ 2024-03-14 1242/week @ 2024-03-21 1466/week @ 2024-03-28 1113/week @ 2024-04-04 2413/week @ 2024-04-11 2501/week @ 2024-04-18 2909/week @ 2024-04-25 3058/week @ 2024-05-02 2649/week @ 2024-05-09 3658/week @ 2024-05-16 3986/week @ 2024-05-23 4084/week @ 2024-05-30 3189/week @ 2024-06-06 2559/week @ 2024-06-13 3270/week @ 2024-06-20 3767/week @ 2024-06-27

13,644 每月下载量
23 个crate中使用(通过 polars-ops

MIT 许可证

300KB
6K SLoC

jsonpath_lib

POLARS供应商;请勿使用。

请参阅https://crates.io/crates/jsonpath_lib以获取原始版本。


lib.rs:

Rust编写的JsonPath实现。

示例

extern crate jsonpath_lib as jsonpath;
#[macro_use] extern crate serde_json;
let json_obj = json!({
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
});

let mut selector = jsonpath::selector(&json_obj);

assert_eq!(selector("$.store.book[*].author").unwrap(),
            vec![
                "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
            ]);

assert_eq!(selector("$..author").unwrap(),
            vec![
                "Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"
            ]);

assert_eq!(selector("$.store.*").unwrap(),
            vec![
                &json!([
                    { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 },
                    { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 },
                    { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 },
                    { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 }
                ]),
                &json!({ "color": "red", "price": 19.95 })
            ]);

assert_eq!(selector("$.store..price").unwrap(),
            vec![
                8.95, 12.99, 8.99, 22.99, 19.95
            ]);

assert_eq!(selector("$..book[2]").unwrap(),
            vec![
                &json!({
                    "category" : "fiction",
                    "author" : "Herman Melville",
                    "title" : "Moby Dick",
                    "isbn" : "0-553-21311-3",
                    "price" : 8.99
                })
            ]);

assert_eq!(selector("$..book[-2]").unwrap(),
            vec![
                &json!({
                    "category" : "fiction",
                    "author" : "Herman Melville",
                    "title" : "Moby Dick",
                    "isbn" : "0-553-21311-3",
                    "price" : 8.99
                })
            ]);

assert_eq!(selector("$..book[0,1]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
            ]);

assert_eq!(selector("$..book[:2]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
            ]);

assert_eq!(selector("$..book[:2]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Evelyn Waugh","title" : "Sword of Honour","price" : 12.99})
            ]);

assert_eq!(selector("$..book[?(@.isbn)]").unwrap(),
            vec![
                &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99}),
                &json!({"category" : "fiction","author" : "J. R. R. Tolkien","title" : "The Lord of the Rings","isbn" : "0-395-19395-8","price" : 22.99})
            ]);

assert_eq!(selector("$.store.book[?(@.price < 10)]").unwrap(),
            vec![
                &json!({"category" : "reference","author" : "Nigel Rees","title" : "Sayings of the Century","price" : 8.95}),
                &json!({"category" : "fiction","author" : "Herman Melville","title" : "Moby Dick","isbn" : "0-553-21311-3","price" : 8.99})
            ]);

依赖项

~1.4–2.4MB
~49K SLoC