#serde-json #create #traversal #style #boilerplate #macro #js-ts

serde_json_traversal

A macro that creates the boilerplate to traverse JSON (js/ts style)

2 unstable releases

0.2.0 Aug 12, 2021
0.1.0 Aug 16, 2020

#1035 in Encoding

Download history 207/week @ 2024-03-24 77/week @ 2024-03-31 370/week @ 2024-04-07 251/week @ 2024-04-14 197/week @ 2024-04-21 261/week @ 2024-04-28 357/week @ 2024-05-05 936/week @ 2024-05-12 716/week @ 2024-05-19 325/week @ 2024-05-26 84/week @ 2024-06-02 25/week @ 2024-06-09 866/week @ 2024-06-16 402/week @ 2024-06-23 268/week @ 2024-06-30 1224/week @ 2024-07-07

2,767 downloads per month

MIT/Apache

7KB

Synopsis

use serde_json_traversal::serde_json_traversal;

#[test]
fn success() {
    let json_obj = serde_json::json!({
        "foo": "bar",
        "bar": [
            "foo",
            "baz",
        ],
        "baz": {
            "foo": [
                "bar",
                {
                    "bar": "baz",
                },
            ],
        },
    });

   assert_eq!(serde_json_traversal!(json_obj => foo).unwrap(), "bar");
   assert_eq!(serde_json_traversal!(json_obj => bar => [0]).unwrap(), "foo");
   assert_eq!(serde_json_traversal!(json_obj => baz => foo => [0]).unwrap(), "bar");
   assert_eq!(serde_json_traversal!(json_obj => baz => foo => [1] => bar).unwrap(), "baz");

   let json_arr = serde_json::json!([
        "foo",
        {
            "bar": "baz",
        }
   ]);

   assert_eq!(serde_json_traversal!(json_arr => [0]).unwrap(), "foo");
   assert_eq!(serde_json_traversal!(json_arr => [1] => bar).unwrap(), "baz");
}

No runtime deps