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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::collections::HashMap;
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub enum JSONValue {
    Null,
    Boolean(bool),
    Number(f64),
    String(String),
    Object(HashMap<String, JSONValue>),
    Array(Vec<JSONValue>),
}

impl JSONValue {
    pub fn as_str(&self) -> String {
        match self {
            JSONValue::String(s) => s.clone(),
            _ => "".to_string(),
        }
    }

    pub fn as_i64(&self) -> i64 {
        match self {
            JSONValue::Number(n) => *n as i64,
            _ => 0,
        }
    }

    pub fn as_f64(&self) -> f64 {
        match self {
            JSONValue::Number(n) => *n,
            _ => 0f64,
        }
    }

    pub fn as_bool(&self) -> bool {
        match self {
            JSONValue::Boolean(b) => *b,
            _ => false,
        }
    }

    pub fn as_array(&self) -> Vec<JSONValue> {
        match self {
            JSONValue::Array(vc) => vc.clone(),
            _ => Vec::new(),
        }
    }

    pub fn as_map(&self) -> HashMap<String, JSONValue> {
        match self {
            JSONValue::Object(hm) => hm.clone(),
            _ => HashMap::new(),
        }
    }

    pub fn get(&self, k: &str) -> Option<JSONValue> {
        match self {
            JSONValue::Object(hm) => match hm.get(k) {
                Some(v) => Some(v.clone()),
                None => None,
            },
            _ => None,
        }
    }

    pub fn exists() -> bool {
        false
    }

    pub fn is_number(&self) -> bool {
        match self {
            JSONValue::Number(_) => true,
            _ => false,
        }
    }

    pub fn is_string(&self) -> bool {
        match self {
            JSONValue::String(_) => true,
            _ => false,
        }
    }

    pub fn is_bool(&self) -> bool {
        match self {
            JSONValue::Boolean(_) => true,
            _ => false,
        }
    }

    pub fn is_object(&self) -> bool {
        match self {
            JSONValue::Object(_) => true,
            _ => false,
        }
    }

    pub fn is_array(&self) -> bool {
        match self {
            JSONValue::Array(_) => true,
            _ => false,
        }
    }

    pub fn is_null(&self) -> bool {
        match self {
            JSONValue::Null => true,
            _ => false,
        }
    }
}

impl fmt::Display for JSONValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            JSONValue::Null => write!(f, "null"),
            JSONValue::Boolean(b) => write!(f, "{}", b),
            JSONValue::Number(n) => write!(f, "{}", n),
            JSONValue::String(s) => write!(f, "\"{}\"", s),
            JSONValue::Object(hm) => {
                let mut ctr = 0;
                write!(f, "{{")?;
                for (k, v) in hm {
                    if ctr < hm.len() - 1 {
                        write!(f, "\"{}\":{},", k, v)?
                    } else {
                        write!(f, "\"{}\":{}", k, v)?
                    };
                    ctr += 1;
                }
                write!(f, "}}")
            }
            JSONValue::Array(vc) => {
                let mut ctr = 0;
                write!(f, "[")?;
                for v in vc {
                    if ctr < vc.len() - 1 {
                        write!(f, "{},", v)?
                    } else {
                        write!(f, "{}", v)?
                    };
                    ctr += 1;
                }
                write!(f, "]")
            }
        }
    }
}

#[derive(Debug)]
pub struct JSONError(String, usize, usize);

impl JSONError {
    pub fn new(err: String, lin: usize, col: usize) -> Self {
        JSONError(err, lin, col)
    }
}

impl fmt::Display for JSONError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "JSONError: {} - @ ({}, {})", self.0, self.1, self.2)
    }
}