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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use std::collections::vec_deque::VecDeque;
use json::{JsonValue, parse};
use std::fs::File;
use std::io::Read;
use bson::{Array, Bson, Document};
pub struct JsonCommons {}
impl JsonCommons {
/// Creates a new instance of `JsonCommons`
/// # Example
/// ```rust
/// use json_commons::JsonCommons;
///
/// let jsonc = JsonCommons::new();
/// ```
pub fn new() -> JsonCommons {
return JsonCommons {};
}
/// Read a `&str` and parse to a `JsonValue`.
/// # Panics
/// Panics if content cannot be parsed to a `JsonValue`.
/// # Example
/// ```rust
/// let content = "{\"car\": {\"model\": \"Gurgel Itaipu E400\", \"color\": \"red\"}}";
///
/// let json = jsonc.read_str(content);
/// ```
pub fn read_str(&self, content: &str) -> JsonValue {
return parse(content).expect("Failed to parse content to JSON");
}
/// Open and parse all file content to a `JsonValue`
/// # Panics
/// - If occurs an error to open or read file
/// - If content cannot be parsed to a `JsonValue`
/// # Example
/// ```rust
/// let path = "/user/docs/myfile.json";
/// let json = jsonc.read_file(path);
/// ```
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())
}
/// You can get any dotted path as an Optional
///
/// If path is not found then return is `None`
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
};
}
/// Check if a dotted path exists
pub fn path_exists(&self, path: &str, content: JsonValue) -> bool {
return self.get_path(&path, content.to_owned()).is_some();
}
/// Parse a list of `JsonValue` to a `VecDeque<JsonValue>`
///
/// # Examples
///
/// Consider the following content
///
/// ```json
/// {
/// "cars": [
/// {
/// "model": "Gurgel Itaipu E400",
/// "color": "red"
/// },
/// {
/// "model": "Gurgel X15",
/// "color": "brown"
/// }
/// ]
/// }
/// ```
///
/// You can Parse the key **__cars__** to a vec
///
/// ```rust
/// let json = jsonc.read_str(content);
/// let cars = jsonc.get_path("cars", json).unwrap();
///
/// jsonc.parse_to_vec(cars); // The output is a vec of JsonValue, with len 2
/// ```
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() {
vec.push_back(member.to_owned());
}
}
return vec;
}
/// Serialize a `JsonValue` into a `String`.
///
/// Consider the following content
/// ```json
/// {
/// "car": {
/// "model": "Gurgel Itaipu E4000",
/// "color": "red"
/// }
/// }
/// ```
/// Output is equivalent to Javascript **__JSON.stringify__**
/// ```rust
/// let serialized = jsonc.serialize(myjson);
/// assert_eq!("{\"car\":{\"model\":\"Gurgel Itaipu E400\",\"color\":\"red\"}}", serialized);
/// ```
pub fn serialize(&self, json: JsonValue) -> String {
return format!("{:#}", format!("{}", json.to_string()));
}
/// Parse a `Document` into a `JsonValue`
pub fn json_from_document(&self, doc: Document) -> JsonValue {
return self.read_str(format!("{:#}", format!("{}", doc.to_string())).as_str())
}
/// Parse a `Bson` into a `JsonValue`
pub fn json_from_bson(&self, bson: Bson) -> JsonValue {
return self.read_str(format!("{:#}", format!("{}", bson.to_string())).as_str())
}
/// Parse a Hex String into a `JsonValue`
pub fn json_from_bson_hex(&self, bson_hex: String) -> JsonValue {
let bytes = hex::decode(bson_hex).expect("Failed to decode hex");
let doc = Document::from_reader(&mut bytes.as_slice()).expect("Failed to read bytes");
return self.json_from_document(doc);
}
/// Parse a `JsonValue` into a `Document`
pub fn parse_to_document(&self, json: JsonValue) -> Document {
let mut document = Document::new();
for (key, value) in json.entries() {
document.insert(key, self.bson_value(value.to_owned()));
}
return document
}
/// Parse a `JsonValue` into a `Bson`
pub fn parse_to_bson(&self, json: JsonValue) -> Bson {
return Bson::from(self.parse_to_document(json.to_owned()))
}
/// Serialize a `JsonValue` into a Hex `String`.
///
/// Consider the following content
/// ```json
/// {
/// "car": {
/// "model": "Gurgel Itaipu E4000",
/// "color": "red"
/// }
/// }
/// ```
/// Output is a encoded Hex BSON
/// ```rust
/// let bson_hex = jsonc.serialize_to_bson_hex(myjson);
/// assert_eq!("3c000000036361720032000000026d6f64656c001300000047757267656c2049746169707520453430300002636f6c6f720004000000726564000000", bson_hex);
/// ```
pub fn serialize_to_bson_hex(&self, json: JsonValue) -> String {
let document = self.parse_to_document(json.to_owned());
let mut bytes: Vec<u8> = vec![];
document.to_writer(&mut bytes).expect("Failed to write bytes");
return hex::encode(bytes.as_slice());
}
// Private Functions
fn gen_path(&self, path: String, last_key: String) -> String {
return path.replace(format!("{}.", last_key).as_str(), "");
}
fn bson_value(&self, value: JsonValue) -> Bson {
if value.is_null() {
return Bson::Null
}
if value.is_string() {
return Bson::from(value.to_owned().as_str().unwrap());
}
if value.is_boolean() {
return Bson::from(value.to_owned().as_bool().unwrap());
}
if value.is_number() {
let number = value.to_owned().take().as_number().unwrap().to_string();
let float = number.parse::<f64>();
match float {
Ok(float_value) => {
return if float_value.fract() != 0.0 {
Bson::from(float_value)
} else {
Bson::from(float_value as i64)
}
},
Err(e) => panic!("{}", e)
}
}
if value.is_object() {
return Bson::from(self.parse_to_document(value.to_owned()));
}
if value.is_array() {
let mut array = Array::new();
for entry in self.parse_to_vec(value.to_owned()) {
array.push(self.bson_value(entry.to_owned()));
}
return Bson::from(array);
}
return Bson::Null;
}
}