https://crates.io/crates/serde-query
serde-query
allows you to declaratively specify how to fetch the desired parts of a (potentially large) document with jq-like syntax and compiles it to an efficient serde::Deserialize
implementation.
For example, you can extract only the commit messages from a GitHub API:
use serde_query::{DeserializeQuery, Query};
#[derive(DeserializeQuery)]
struct Message {
#[query(".commit.message")]
message: String,
}
fn main() {
let reader = ureq::get("https://api.github.com/repos/pandaman64/serde-query/commits")
.call()
.into_reader();
let messages: Vec<Query<Message>> = serde_json::from_reader(reader).unwrap();
for message in messages.into_iter() {
let message = Message::from(message);
println!("{}", message.message);
}
}
I implemented this just out of curiosity, though I hope this library will help when you need to deal with an external API or process a massive data file.
(crosspost from reddit)