Crates.io | serde-query |
lib.rs | serde-query |
version | 0.2.0 |
source | src |
created_at | 2020-09-03 14:52:59.227481 |
updated_at | 2023-02-19 12:32:24.707381 |
description | Serde-compatible streaming data-query language with a jq-like syntax |
homepage | |
repository | https://github.com/pandaman64/serde-query/ |
max_upload_size | |
id | 284278 |
size | 169,109 |
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:
cargo add serde-query
Or, add it to your Cargo.toml
:
[dependencies]
serde-query = "0.2.0"
use serde_query::{DeserializeQuery, Query};
#[derive(DeserializeQuery)]
struct Data {
#[query(".commits.[].author")]
authors: Vec<String>,
#[query(".count")]
count: usize,
}
let document = serde_json::json!({
"commits": [
{ "author": "Kou", "hash": 0x0202 },
{ "author": "Kasumi", "hash": 0x1013 },
{ "author": "Masaru", "hash": 0x0809 },
],
"count": 3,
}).to_string();
// 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 = serde_json::from_str::<Query<Data>>(&document)?.into();
assert_eq!(data.authors, vec!["Kou", "Kasumi", "Masaru"]);
assert_eq!(data.count, 3);
use serde_query::Deserialize;
#[derive(Debug, Deserialize)]
struct Data {
// missing field
#[query(".author.name")]
author_name: String,
// typo
#[query(".commit.commiter.name")]
committer_name: String,
// type error
#[query(".author.id")]
id: String,
}
let error = serde_json::from_str::<Data>(INPUT).unwrap_err();
assert_eq!(
error.to_string(),
r#"
Queries failed for fields: 'author_name', 'committer_name', 'id'
1. Query for field 'author_name' failed at '.author': missing field 'name'
2. Query for field 'committer_name' 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
"#
.trim_start()
);
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.