Welcome to serde-query, a Rust library that lets you write jq-like queries for your data.
To get started with serde-query, add it to your Rust project using Cargo:
bash
cargo add serde-query
Or, add it to your Cargo.toml
:
toml
[dependencies]
serde-query = "0.2.0"
```rust use serde_query::{DeserializeQuery, Query};
struct Data {
#[query(".commits.[].author")]
authors: Vec
let document = serdejson::json!({ "commits": [ { "author": "Kou", "hash": 0x0202 }, { "author": "Kasumi", "hash": 0x1013 }, { "author": "Masaru", "hash": 0x0809 }, ], "count": 3, }).tostring();
// You can use Query<T>
as a Deserialize
type for any Deserializer
// and convert the result to the desired type using From
/Into
.
let data: Data = serdejson::fromstr::
asserteq!(data.authors, vec!["Kou", "Kasumi", "Masaru"]); asserteq!(data.count, 3); ```
```rust use serde_query::Deserialize;
struct Data { // missing field #[query(".author.name")] authorname: String, // typo #[query(".commit.commiter.name")] committername: String, // type error #[query(".author.id")] id: String, }
let error = serdejson::fromstr::(INPUT).unwraperr();
asserteq!(
error.tostring(),
r#"
Queries failed for fields: 'authorname', 'committername', 'id'
1. Query for field 'authorname' failed at '.author': missing field 'name'
2. Query for field 'committername' failed at '.commit': missing field 'commiter'
3. Query for field 'id' failed at '.author.id': invalid type: integer 5635139
, expected a string at line 34 column 17
"#
.trimstart()
);
```
This library generates Rust types for each query segment (e.g., .commit
, .commit.message
, etc.), which may lead to binary bloat and longer compile time.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.