1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use json::{JsonValue, parse};
use std::collections::VecDeque;
use std::fs::File;
use std::io::Read;

pub struct JsonCommons {}


impl JsonCommons {

    pub fn new() -> JsonCommons {
        return JsonCommons {};
    }

    pub fn read_str(&self, content: &str) -> JsonValue {
        return parse(content).expect("Failed to parse content to JSON");
    }

    pub fn read_file(&self, file_path: &str) -> JsonValue {
        let mut file = File::open(file_path).expect(format!("Failed to open {}", file_path).as_str());
        let mut content = String::new();
        file.read_to_string(&mut content).expect(format!("Failed to read content from {}", file_path).as_str());
        return self.read_str(content.as_str())
    }

    pub fn get_path(&self, path: &str, content: JsonValue) -> Option<JsonValue> {
        let mut keys = path.split(".")
            .map(|key| key.to_string())
            .collect::<VecDeque<String>>();

        if keys.is_empty() {
            return None;
        }

        let key = keys.pop_front().unwrap();

        if content.has_key(&key) && keys.len() > 0 {
            return if content[&key].is_array() {
                let parse_index = keys.pop_front().unwrap().parse::<usize>();

                match parse_index {
                    Ok(index) => {
                        let array = self.parse_to_vec(content[&key].to_owned());

                        match array.get(index).cloned() {
                            Some(object) => {
                                let last_key = format!("{}.{}", &key, &index);
                                return if keys.len() > 0 {
                                    self.get_path(&self.gen_path(path.to_string(), last_key), object)
                                } else {
                                    return Some(object)
                                }
                            }
                            None => None
                        }
                    },
                    Err(_) => None
                }
            } else {
                self.get_path(&self.gen_path(path.to_string(), key.to_owned()), content[&key].to_owned())
            }
        }

        return if content.has_key(&key) {
            Some(content.to_owned()[&key].take())
        } else {
            None
        };
    }

    pub fn path_exists(&self, path: &str, content: JsonValue) -> bool {
        return self.get_path(&path, content.to_owned()).is_some();
    }

    pub fn parse_to_vec(&self, content: JsonValue) -> VecDeque<JsonValue> {
        let mut vec = VecDeque::<JsonValue>::new();
        if content.is_array() && content.members().len() > 0 {
            for member in content.members() {
                if !member.is_empty() {
                    vec.push_back(member.to_owned());
                }
            }
        }
        return vec;
    }

    fn gen_path(&self, path: String, last_key: String) -> String {
        return path.replace(format!("{}.", last_key).as_str(), "");
    }
}