valq

valq provides macros for querying semi-structured ("JSON-ish") data with the JavaScript-like syntax.
Look & Feel:
let obj: Value = ...;
// without valq: tedious and_then() chain...
let deep = obj
.get
.and_then
.and_then
.and_then
.and_then;
// with valq: very concise and readable!
use query_value;
let deep = query_value!;
Installation
Add this to the Cargo.toml in your project:
[]
= "*"
What's provided
The principal macro provided by this crate is query_value!.
There is also a Result-returning variant of query_value!, called query_value_reosult!.
query_value! macro
A macro for querying, extracting and converting inner value of semi-structured data.
Basic Queries
// get field `foo` from JSON object `obj`
let foo = query_value!;
// get the first item of the nested JSON array `arr` in `obj`
let head = query_value!;
// more complex query, just works!
let abyss = query_value!;
Extracting Mutable Reference to Inner Value
use
let mut obj = json!;
// with `->` syntax, you can cast `Value` as typed value (see below)
assert_eq!;
assert_eq!;
Casting & Deserializing to Specified Type
// try to cast the queried value into `u64` using `as_u64()` method on that value.
// results in `None` in case of type mismatch
let foo_u64: = query_value!;
// in the context of mutable reference extraction (see below), `as_xxx_mut()` method is used instead.
let arr_vec: = query_value!;
use Deserialize;
use json;
use query_value;
let j = json!;
// try to deserialize the queried value into a value of type `Person`.
let author: = query_value!;
Unwrapping Query Results with Default Values
use json;
use query_value;
let obj = json!;
assert_eq!;
assert_eq!; // explicitly provided default
assert_eq!; // using u64::default()
query_value_result! macro
A variant of query_value! that returns Result<T, valq::Error> instead of Option<T>.
use Deserialize;
use json;
use ;
let obj = json!;
// Error::ValueNotFoundAtPath: querying non-existent path
let result = query_value_result!;
assert!;
// Error::AsCastFailed: type casting failure
let result = query_value_result!;
assert!;
// Error::DeserializationFailed: deserialization failure
let result = query_value_result!;
assert!;
Helper: transpose_tuple! macro
Transposes a tuple of Options/Results into an Option/Result of a tuple.
This is meant to be used with query_value!/query_value_result! macros, for "cherry-picking" deep value from data.
use json;
use ;
let data = json!;
// Combine multiple Option results into Option<tuple>
let picks = transpose_tuple!;
assert_eq!;
// For Result variant, "Result;" prefix is needed
let picks = transpose_tuple!;
assert!;
Compatibility
The query_value! macro can be used with arbitrary data structure(to call, Value) that supports get(&self, idx) -> Option<&Value> method that retrieves a value at idx.
Extracting mutable reference is also supported if your Value supports get_mut(&mut self, idx) -> Option<&Value>.
Instances of compatible data structures:
serde_json::Valueserde_yaml::Valuetoml::Value- and more...