5 releases

0.1.4 Nov 20, 2024
0.1.3 Nov 20, 2024
0.1.2 Oct 29, 2024
0.1.1 Apr 11, 2019
0.1.0 Mar 19, 2019

#451 in Encoding

Download history 9/week @ 2024-09-23 139/week @ 2024-10-28 64/week @ 2024-11-04 73/week @ 2024-11-11 331/week @ 2024-11-18 37/week @ 2024-11-25 37/week @ 2024-12-02 70/week @ 2024-12-09

476 downloads per month

MIT license

22KB
553 lines

sorted_json_keys

Rust lib to sort JSON based on string keys and filter json values

Note:

  • filter is working based on the keys
  • sorted is buggy, need improvements

#[cfg(test)]
mod tests {
    use crate::filter::keys::filter;
    
    #[test]
    fn test_map_filter() {
        let data: serde_json::Value = serde_json::json!({
            "foo": "bar",
            "config": {
                "foo-config": "bar",
                "temp": "temppp",
                "hector": "tractor"
            }
        });

        let filtered = filter(data, &|key| {
            vec!["foo", "config.temp", "config.hector"].contains(&key)
        });

        let expected = serde_json::json!({
          "config": {
            "hector": "tractor",
            "temp": "temppp"
          },
          "foo": "bar"
        });

        assert_eq!(expected, filtered);
        // println!("value: {}", serde_json::to_string_pretty(&value).unwrap());
    }

    #[test]
    fn test_list_filter() {
        let data: serde_json::Value = serde_json::json!({
            "foo": "bar",
            "config": [
                {
                    "foo-config": "bar",
                    "temp": "temppp",
                    "hector": "tractor"
                },
                {
                    "foo-config": "bar1",
                    "temp": "temppp1",
                    "hector": "tractor1"
                }
            ]
        });

        let expected = serde_json::json!({
            "foo": "bar",
            "config": [
            {
              "hector": "tractor",
              "temp": "temppp"
            },
            {
              "hector": "tractor1",
              "temp": "temppp1"
            }
          ]
        });

        let filtered = filter(data, &|key| {
            vec!["foo", "config.[].temp", "config.[].hector"].contains(&key)
        });
        assert_eq!(expected, filtered);
    }

    #[test]
    fn filter_inner_list() {
        let data: serde_json::Value = serde_json::json!({
            "foo": "bar",
            "config": [
                {
                    "foo-config": "bar",
                    "temp": [{ "a": 1 }],
                    "hector": "tractor"
                },
                {
                    "foo-config": "bar1",
                    "temp": [{ "a": 2 }],
                    "hector": "tractor1"
                }
            ]
        });

        let filtered = filter(data, &|key| key == "config.[].temp.[].a");

        let expected: serde_json::Value = serde_json::json!({
            "config": [
                {
                    "temp": [{ "a": 1 }],
                },
                {
                    "temp": [{ "a": 2 }],
                }
            ]
        });

        assert_eq!(expected, filtered);

        // println!("value: {}", serde_json::to_string_pretty(&filtered).unwrap());
    }
}

Dependencies

~0.7–1.6MB
~34K SLoC