Skip to content

Instantly share code, notes, and snippets.

@adclz
adclz / json_parse.rs
Created May 8, 2024 13:52
A serde_json parse macro that generates variables automatically.
/// The `json_parse!` macro automatically generates all `serde_json` `as_` operations based on a provided JSON syntax, binding all variables to the keys.
/// When a key cannot be converted, Result is propagated using the `?` operator.
/// To handle errors before propagation, you can provide a closure as the first argument to this macro which will be called by `.map_err()`.
/// The data argument has to be of type `&Map<String, Value>`.
///
/// ## Usage:
///
/// ```rust
/// json_parse!(
/// |err| format!("Could not parse some_data, {}", err),