serde-query 0.1.0

Serde-compatible streaming data-query language with a jq-like syntax
Documentation

A query language for Serde data model.

This crate provides DeserializeQuery trait and its derive macro for defining a query.

Example

# use std::error::Error;
# fn main() -> Result<(), Box<dyn Error + 'static>> {
use serde_query::{DeserializeQuery, Query};

#[derive(DeserializeQuery)]
struct Data {
#[query(".commit.author")]
commit_author: String,
#[query(".hash")]
hash_value: u64,
}

let document = serde_json::to_string(&serde_json::json!({
"commit": {
"author": "pandaman64",
"date": "2020/09/03",
},
"hash": 0xabcd,
}))?;

// 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.commit_author, "pandaman64");
assert_eq!(data.hash_value, 0xabcd);
# Ok(())
# }

Deriving DeserializeQuery

To declare a query, put #[derive(DeserializeQuery)] on your type. Each field must have a #[query(...)] attribute for specifying which part of the document should be retrieved, starting from the root.

#[query(...)] syntax

serde-query currently supports the following syntax for accessing a part of the input.

  • .field for accessing a field with a name field of an object.