[][src]Crate jsonpath_lib

JsonPath implementation for Rust

Example

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

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

   //
   // $..author
   //
   let json = selector("$..author").unwrap();
   let ret = json!(["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]);
   assert_eq!(json, ret);

   //
   // $.store.*
   //
   let json = selector("$.store.*").unwrap();
   let ret = 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}
       ],
       {"color" : "red","price" : 19.95},
   ]);
   assert_eq!(ret, json);

   //
   // $.store..price
   //
   let json = selector("$.store..price").unwrap();
   let ret = json!([8.95, 12.99, 8.99, 22.99, 19.95]);
   assert_eq!(ret, json);

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

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

   //
   // $..book[0,1]
   //
   let json = selector("$..book[0,1]").unwrap();
   let ret = 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}
   ]);
   assert_eq!(ret, json);

   //
   // $..book[:2]
   //
   let json = selector("$..book[:2]").unwrap();
   let ret = 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}
   ]);
   assert_eq!(ret, json);

   //
   // $..book[2:]
   //
   let json = selector("$..book[2:]").unwrap();
   let ret = json!([
       {"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}
   ]);
   assert_eq!(ret, json);

   //
   // $..book[?(@.isbn)]
   //
   let json = selector("$..book[?(@.isbn)]").unwrap();
   let ret = json!([
       {"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}
   ]);
   assert_eq!(ret, json);

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

Structs

Selector

Utility. Functions like jsonpath::selector or jsonpath::compile are also implemented using this structure.

Functions

compile

It is a high-order function. it compile a JsonPath and then returns a function. this return-function can be reused for different JsonObjects.

readDeprecated
readerDeprecated
select

This function compile a jsonpath everytime and it convert serde_json's Value to jsonpath's RefValue everytime and then it return a serde_json::value::Value.

select_as

This function compile a jsonpath everytime and it convert &str to jsonpath's RefValue everytime and then it return a deserialized-instance of type T.

select_as_str

This function compile a jsonpath everytime and it convert &str to jsonpath's RefValue everytime and then it return a json string.

select_strDeprecated
selector

It is a high-order function that return a function. this return-function has a jsonpath as argument and return a serde_json::value::Value. so you can use different JsonPath for one JsonObject.

selector_as

It is a high-order function that returns a function. this return-function has a jsonpath as argument and return a serde::Deserialize. so you can use different JsonPath for one JsonObject.