json-extract-macro

Access nested JSON values in 1 line of code

image

This macro reduces boilerplate when using serde_json::Value variants when trying to get into a nested property.

```rs let jsonparsed = serdejson::json!({ "brand": { "tesla": { "model": { "designers": "Mr Bean" } } } });

rs let designer: Option = jsonextract!("brand.tesla.model.designer", &jsonparsed, String);

println!("Who tf is this designer? {}",designer.unwrapordefault()); ``` or...

```rs if let serdejson::Value::Object(brand) = &jsonparsed { let brand = brand.get("brand").unwrap(); if let serdejson::Value::Object(tesla) = &brand { let tesla = tesla.get("tesla").unwrap(); if let serdejson::Value::Object(model) = &tesla { let model = model.get("model").unwrap(); if let serdejson::Value::Object(designers) = &model { let res = designer.get("designer"); let designer = serdejson::fromvalue::(res.unwrap().toowned()).unwrap(); } } } }

println!("Who tf is this designer? {}",designer.unwrapordefault()); ```

Macro args

The macro accepts 3 arguments:

  1. A &str containg the path, separated by "."
  2. The serde_json::Value variable to read.
  3. The type of the property we want to get.

Types supported

json_serde::Value has the following variants:

The third parameter to pass in the macro is a Rust type, so, things we can pass if we want to get data from some variants:

| Value variant | Rust types | | ------ | ------ | | Array | Vec<String>, Vec<bool>, Vec<f64>, Vec<Value> ... | | Bool | bool | | Number | u32, i32, i64, f32, usize ... | | Object | Value | | String | String | | Null | not supported |