rust_web_server/json/object/
mod.rs

1use std::io;
2use std::io::{BufRead, Read};
3use crate::ext::string_ext::StringExt;
4use crate::json::property::{JSONProperty, JSONValue};
5use crate::symbol::SYMBOL;
6
7#[cfg(test)]
8mod tests;
9
10pub trait ToJSON {
11    fn list_properties() -> Vec<JSONProperty>;
12    fn get_property(&self, property_name: String) -> JSONValue;
13    fn to_json_string(&self) -> String;
14}
15
16pub trait FromJSON {
17    fn parse_json_to_properties(&self, json_string: String) -> Result<Vec<(JSONProperty, JSONValue)>, String>;
18    fn set_properties(&mut self, properties: Vec<(JSONProperty, JSONValue)>) -> Result<(), String>;
19    fn parse(&mut self, json_string: String) -> Result<(), String>;
20}
21
22pub struct JSON;
23
24impl JSON {
25    pub fn parse_as_properties(json_string: String) -> Result<Vec<(JSONProperty, JSONValue)>, String> {
26        let mut properties = vec![];
27
28        let data = json_string.as_bytes();
29        let mut cursor = io::Cursor::new(data);
30        let mut bytes_read : i128 = 0;
31        let total_bytes : i128 = data.len() as i128;
32
33        // read obj start '{'
34        let mut _is_root_opening_curly_brace = true;
35        let mut buf = vec![];
36        let mut boxed_read = cursor.read_until(b'{', &mut buf);
37        if boxed_read.is_err() {
38            let error = boxed_read.err().unwrap().to_string();
39            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
40            return Err(message);
41        }
42        bytes_read = bytes_read + boxed_read.unwrap() as i128;
43
44        let mut b : &[u8] = &buf;
45
46        let mut boxed_line = String::from_utf8(Vec::from(b));
47        if boxed_line.is_err() {
48            let error = boxed_line.err().unwrap().to_string();
49            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
50            return Err(message);
51        }
52        let mut _line = boxed_line.unwrap();
53
54
55        let mut is_there_a_key_value = true;
56        while is_there_a_key_value {
57            // read until key starts '"', save to buffer
58            // it will work for first and consecutive key value pair
59            let mut key_value_pair : String = "".to_string();
60
61
62            buf = vec![];
63            boxed_read = cursor.read_until(b'\"', &mut buf);
64            if boxed_read.is_err() {
65                let error = boxed_read.err().unwrap().to_string();
66                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
67                return Err(message);
68            }
69            bytes_read = bytes_read + boxed_read.unwrap() as i128;
70            b  = &buf;
71
72            boxed_line = String::from_utf8(Vec::from(b));
73            if boxed_line.is_err() {
74                let error = boxed_line.err().unwrap().to_string();
75                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
76                return Err(message);
77            }
78
79            _line = boxed_line.unwrap();
80            let buffer_filtered_control_chars = StringExt::filter_ascii_control_characters(_line.as_str());
81            if buffer_filtered_control_chars != "\"" {
82                let message = format!("provided json is not valid");
83                return Err(message);
84            }
85
86            key_value_pair = [key_value_pair, _line].join(SYMBOL.empty_string);
87
88
89
90            // read until key ends '"', append to buffer
91            buf = vec![];
92            boxed_read = cursor.read_until(b'\"', &mut buf);
93            if boxed_read.is_err() {
94                let error = boxed_read.err().unwrap().to_string();
95                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
96                return Err(message);
97            }
98            bytes_read = bytes_read + boxed_read.unwrap() as i128;
99            b = buf.as_slice();
100
101            boxed_line = String::from_utf8(Vec::from(b));
102            if boxed_line.is_err() {
103                let error = boxed_line.err().unwrap().to_string();
104                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
105                return Err(message);
106            }
107            _line = boxed_line.unwrap();
108            key_value_pair = [key_value_pair, _line].join(SYMBOL.empty_string);
109
110
111            // read until delimiter ':', append to buffer
112            let mut not_delimiter = true;
113            while not_delimiter {
114                let bytes_to_read = 1;
115                let mut char_buffer = vec![bytes_to_read];
116
117                let boxed_read = cursor.read_exact(&mut char_buffer);
118                if boxed_read.is_err() {
119                    let error = boxed_read.err().unwrap().to_string();
120                    let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
121                    return Err(message);
122                }
123                boxed_read.unwrap();
124                bytes_read = bytes_read + bytes_to_read as i128;
125                let boxed_char = String::from_utf8(char_buffer);
126                if boxed_char.is_err() {
127                    let error = boxed_char.err().unwrap().to_string();
128                    let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
129                    return Err(message);
130                }
131
132                let boxed_last_char = boxed_char.unwrap().chars().last();
133                if boxed_last_char.is_none() {
134                    let error = "last char is none (after ':')";
135                    let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
136                    return Err(message);
137                }
138                let char = boxed_last_char.unwrap();
139
140                if char != ' ' && char != '\n' && char != '\r' && !char.is_ascii_control() {
141                    if char == ':' {
142                        not_delimiter = false;
143                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
144                    } else {
145                        let message = format!("while seeking for property delimiter ':', found unexpected character: {}", char);
146                        return Err(message);
147                    }
148                }
149            }
150
151
152
153            // read in a while loop until char is not ascii control char and not whitespace, append to buffer
154            let mut comma_delimiter_read_already = false;
155            let mut is_whitespace_or_new_line_or_carriage_return = true;
156
157            while is_whitespace_or_new_line_or_carriage_return {
158                let bytes_to_read = 1;
159                let mut char_buffer = vec![bytes_to_read];
160                comma_delimiter_read_already = false;
161
162                let boxed_read = cursor.read_exact(&mut char_buffer);
163                if boxed_read.is_err() {
164                    let error = boxed_read.err().unwrap().to_string();
165                    let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
166                    return Err(message);
167                }
168                boxed_read.unwrap();
169                bytes_read = bytes_read + bytes_to_read as i128;
170                let boxed_char = String::from_utf8(char_buffer);
171                if boxed_char.is_err() {
172                    let error = boxed_char.err().unwrap().to_string();
173                    let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
174                    return Err(message);
175                }
176
177                let boxed_last_char = boxed_char.unwrap().chars().last();
178                if boxed_last_char.is_none() {
179                    let error = "last char is none (after ':')";
180                    let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
181                    return Err(message);
182                }
183                let char = boxed_last_char.unwrap();
184
185                if char != ' ' && char != '\n' && char != '\r' && !char.is_ascii_control() {
186                    // we passed opening curly brace at the beginning, at this point only nested objects can have '{'
187                    _is_root_opening_curly_brace = false;
188
189
190                    let is_string = char == '\"';
191                    if is_string {
192                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
193
194                        // read till non escaped '"'
195                        let mut not_end_of_string_property_value = true;
196                        while not_end_of_string_property_value {
197
198                            char_buffer = vec![bytes_to_read];
199                            let boxed_read = cursor.read_exact(&mut char_buffer);
200                            if boxed_read.is_err() {
201                                let error = boxed_read.err().unwrap().to_string();
202                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
203                                return Err(message);
204                            }
205                            boxed_read.unwrap();
206                            bytes_read = bytes_read + bytes_to_read as i128;
207                            let boxed_parse = String::from_utf8(char_buffer);
208                            if boxed_parse.is_err() {
209                                let error = boxed_parse.err().unwrap().to_string();
210                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
211                                return Err(message);
212                            }
213                            let _char = boxed_parse.unwrap();
214                            let last_char_in_buffer = key_value_pair.chars().last().unwrap().to_string();
215                            not_end_of_string_property_value = _char != "\"" && last_char_in_buffer != "\\";
216                            key_value_pair = [key_value_pair, _char].join(SYMBOL.empty_string);
217                        }
218
219
220                        // read till comma
221                        buf = vec![];
222                        let boxed_read = cursor.read_until(b',', &mut buf);
223                        if boxed_read.is_err() {
224                            let error = boxed_read.err().unwrap().to_string();
225                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
226                            return Err(message);
227                        }
228                        bytes_read = bytes_read + boxed_read.unwrap() as i128;
229                        if bytes_read == total_bytes {
230                            is_there_a_key_value = false;
231                        };
232
233                        let boxed_parse = String::from_utf8(buf);
234                        if boxed_parse.is_err() {
235                            let message = boxed_parse.err().unwrap().to_string();
236                            return Err(message);
237                        }
238                        let buffer_before_comma = boxed_parse.unwrap();
239                        let buffer_filtered_control_chars = StringExt::filter_ascii_control_characters(buffer_before_comma.as_str());
240
241                        if buffer_filtered_control_chars.chars().count() != 0 && buffer_filtered_control_chars != "}" && buffer_filtered_control_chars != "," {
242                            let message = format!("there are not expected characters after number (expected comma): {}", buffer_before_comma);
243                            return Err(message);
244                        } else {
245                            comma_delimiter_read_already = true;
246                        }
247                    }
248
249                    let is_null = char == 'n';
250                    if is_null {
251                        // read 'ull'
252                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
253                        let byte = 0;
254                        let mut char_buffer = vec![byte, byte, byte];
255                        let length = char_buffer.len();
256                        let boxed_read = cursor.read_exact(&mut char_buffer);
257                        if boxed_read.is_err() {
258                            let error = boxed_read.err().unwrap().to_string();
259                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
260                            return Err(message);
261                        }
262                        boxed_read.unwrap();
263                        bytes_read = bytes_read + length as i128;
264                        let boxed_parse = String::from_utf8(char_buffer);
265                        if boxed_parse.is_err() {
266                            let error = boxed_parse.err().unwrap().to_string();
267                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
268                            return Err(message);
269                        }
270                        let remaining_bool = boxed_parse.unwrap();
271                        if remaining_bool != "ull" {
272                            let error = format!("Unable to parse null: {}", key_value_pair);
273                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
274                            return Err(message);
275                        }
276                        key_value_pair = [key_value_pair, remaining_bool].join(SYMBOL.empty_string);
277
278                        // read till comma
279                        buf = vec![];
280                        let boxed_read = cursor.read_until(b',', &mut buf);
281                        if boxed_read.is_err() {
282                            let error = boxed_read.err().unwrap().to_string();
283                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
284                            return Err(message);
285                        }
286                        bytes_read = bytes_read + boxed_read.unwrap() as i128;
287                        if bytes_read == total_bytes {
288                            is_there_a_key_value = false;
289                        };
290
291                        let boxed_parse = String::from_utf8(buf);
292                        if boxed_parse.is_err() {
293                            let message = boxed_parse.err().unwrap().to_string();
294                            return Err(message)
295                        }
296                        let buffer_before_comma = boxed_parse.unwrap();
297                        let buffer_filtered_control_chars = StringExt::filter_ascii_control_characters(buffer_before_comma.as_str());
298
299                        if buffer_filtered_control_chars.chars().count() != 0 && buffer_filtered_control_chars != "}" && buffer_filtered_control_chars != "," {
300                            let message = format!("before comma there are some unexpected characters: {}", buffer_before_comma);
301                            return Err(message);
302                        } else {
303                            comma_delimiter_read_already = true;
304                        }
305                    }
306
307                    let is_boolean_true = char == 't';
308                    if is_boolean_true {
309                        // read 'rue'
310                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
311                        let byte = 0;
312                        let mut char_buffer = vec![byte, byte, byte];
313                        let length = char_buffer.len();
314                        let boxed_read = cursor.read_exact(&mut char_buffer);
315                        if boxed_read.is_err() {
316                            let error = boxed_read.err().unwrap().to_string();
317                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
318                            return Err(message);
319                        }
320                        boxed_read.unwrap();
321                        bytes_read = bytes_read + length as i128;
322                        let boxed_parse = String::from_utf8(char_buffer);
323                        if boxed_parse.is_err() {
324                            let error = boxed_parse.err().unwrap().to_string();
325                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
326                            return Err(message);
327                        }
328                        let remaining_bool = boxed_parse.unwrap();
329                        if remaining_bool != "rue" {
330                            let error = format!("Unable to parse boolean: {}", key_value_pair);
331                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
332                            return Err(message);
333                        }
334                        key_value_pair = [key_value_pair, remaining_bool].join(SYMBOL.empty_string);
335
336                        // read till comma
337                        buf = vec![];
338                        let boxed_read = cursor.read_until(b',', &mut buf);
339                        if boxed_read.is_err() {
340                            let error = boxed_read.err().unwrap().to_string();
341                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
342                            return Err(message);
343                        }
344                        bytes_read = bytes_read + boxed_read.unwrap() as i128;
345                        if bytes_read == total_bytes {
346                            is_there_a_key_value = false;
347                        };
348
349                        let boxed_parse = String::from_utf8(buf);
350                        if boxed_parse.is_err() {
351                            let message = boxed_parse.err().unwrap().to_string();
352                            return Err(message);
353                        }
354                        let buffer_before_comma = boxed_parse.unwrap();
355                        let buffer_filtered_control_chars = StringExt::filter_ascii_control_characters(buffer_before_comma.as_str());
356
357                        if buffer_filtered_control_chars.chars().count() != 0 && buffer_filtered_control_chars != "}" && buffer_filtered_control_chars != "," {
358                            let message = format!("before comma there are some unexpected characters: {}", buffer_before_comma);
359                            return Err(message);
360                        } else {
361                            comma_delimiter_read_already = true;
362                        }
363                    }
364
365                    let is_boolean_false = char == 'f';
366                    if is_boolean_false {
367                        // read 'alse'
368                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
369                        let byte = 0;
370                        let mut char_buffer = vec![byte, byte, byte, byte];
371                        let length = char_buffer.len();
372                        let boxed_read = cursor.read_exact(&mut char_buffer);
373                        if boxed_read.is_err() {
374                            let error = boxed_read.err().unwrap().to_string();
375                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
376                            return Err(message);
377                        }
378                        boxed_read.unwrap();
379                        bytes_read = bytes_read + length as i128;
380                        let boxed_parse = String::from_utf8(char_buffer);
381
382                        if boxed_parse.is_err() {
383                            let error = boxed_parse.err().unwrap().to_string();
384                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
385                            return Err(message);
386                        }
387                        let remaining_bool = boxed_parse.unwrap();
388                        if remaining_bool != "alse" {
389                            let message = format!("Unable to parse boolean: {}", key_value_pair);
390                            return Err(message)
391                        }
392                        key_value_pair = [key_value_pair, remaining_bool].join(SYMBOL.empty_string);
393
394                        // read till comma
395                        buf = vec![];
396                        let boxed_read = cursor.read_until(b',', &mut buf);
397                        if boxed_read.is_err() {
398                            let error = boxed_read.err().unwrap().to_string();
399                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
400                            return Err(message);
401                        }
402                        bytes_read = bytes_read + boxed_read.unwrap() as i128;
403                        if bytes_read == total_bytes {
404                            is_there_a_key_value = false;
405                        };
406
407                        let boxed_parse = String::from_utf8(buf);
408                        if boxed_parse.is_err() {
409                            let message = boxed_parse.err().unwrap().to_string();
410                            return Err(message);
411                        }
412                        let buffer_before_comma = boxed_parse.unwrap();
413                        let buffer_filtered_control_chars = StringExt::filter_ascii_control_characters(buffer_before_comma.as_str());
414
415                        if buffer_filtered_control_chars.chars().count() != 0 && buffer_filtered_control_chars != "}" && buffer_filtered_control_chars != "," {
416                            let message = format!("before comma there are some unexpected characters: {}", buffer_before_comma);
417                            return Err(message);
418                        } else {
419                            comma_delimiter_read_already = true;
420                        }
421                    }
422
423                    let is_array = char == '[';
424                    if is_array {
425                        // read the array (including nested objects and arrays)
426                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
427                        let mut number_of_open_square_brackets = 1;
428                        let mut number_of_closed_square_brackets = 0;
429
430                        let mut read_char = true;
431                        while read_char {
432
433                            let byte = 0;
434                            let mut char_buffer = vec![byte];
435                            let length = char_buffer.len();
436                            let boxed_read = cursor.read_exact(&mut char_buffer);
437                            if boxed_read.is_err() {
438                                let error = boxed_read.err().unwrap().to_string();
439                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
440                                return Err(message);
441                            }
442                            boxed_read.unwrap();
443                            bytes_read = bytes_read + length as i128;
444                            let boxed_parse = String::from_utf8(char_buffer);
445                            if boxed_parse.is_err() {
446                                let error = boxed_parse.err().unwrap().to_string();
447                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
448                                return Err(message);
449                            }
450                            let char = boxed_parse.unwrap().chars().last().unwrap();
451
452                            let is_open_square_bracket = char == '[';
453                            if is_open_square_bracket {
454                                number_of_open_square_brackets = number_of_open_square_brackets + 1;
455                            }
456
457
458                            let is_close_square_bracket = char == ']';
459                            if is_close_square_bracket {
460                                number_of_closed_square_brackets = number_of_closed_square_brackets + 1;
461                            }
462
463                            key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
464
465                            if number_of_open_square_brackets == number_of_closed_square_brackets {
466                                read_char = false;
467                            }
468                        }
469
470                        // read till comma
471                        buf = vec![];
472                        let boxed_read = cursor.read_until(b',', &mut buf);
473                        if boxed_read.is_err() {
474                            let error = boxed_read.err().unwrap().to_string();
475                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
476                            return Err(message);
477                        }
478                        bytes_read = bytes_read + boxed_read.unwrap() as i128;
479                        if bytes_read == total_bytes {
480                            is_there_a_key_value = false;
481                        };
482
483                        let boxed_parse = String::from_utf8(buf);
484                        if boxed_parse.is_err() {
485                            let message = boxed_parse.err().unwrap().to_string();
486                            return Err(message);
487                        }
488                        let buffer_before_comma = boxed_parse.unwrap();
489                        let buffer_filtered_control_chars = StringExt::filter_ascii_control_characters(buffer_before_comma.as_str());
490
491                        if buffer_filtered_control_chars.chars().count() != 0 && buffer_filtered_control_chars != "}" && buffer_filtered_control_chars != "," {
492                            let message = format!("before comma there are some unexpected characters: {}", buffer_before_comma);
493                            return Err(message);
494                        } else {
495                            comma_delimiter_read_already = true;
496                        }
497                    }
498
499
500                    let is_nested_object = char == '{' && !_is_root_opening_curly_brace;
501                    if is_nested_object {
502                        // read the object (including nested objects and arrays)
503                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
504                        let mut number_of_open_curly_braces = 1;
505                        let mut number_of_closed_curly_braces = 0;
506
507                        let mut read_char = true;
508                        while read_char {
509
510                            let byte = 0;
511                            let mut char_buffer = vec![byte];
512                            let length = char_buffer.len();
513                            let boxed_read = cursor.read_exact(&mut char_buffer);
514                            if boxed_read.is_err() {
515                                let error = boxed_read.err().unwrap().to_string();
516                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
517                                return Err(message);
518                            }
519                            boxed_read.unwrap();
520                            bytes_read = bytes_read + length as i128;
521                            let boxed_parse = String::from_utf8(char_buffer);
522                            if boxed_parse.is_err() {
523                                let error = boxed_parse.err().unwrap().to_string();
524                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
525                                return Err(message);
526                            }
527                            let boxed_last_char = boxed_parse.unwrap().chars().last();
528                            if boxed_last_char.is_none() {
529                                let error = "last char is empty";
530                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
531                                return Err(message);
532                            }
533                            let char = boxed_last_char.unwrap();
534
535                            let is_open_curly_brace = char == '{';
536                            if is_open_curly_brace {
537                                number_of_open_curly_braces = number_of_open_curly_braces + 1;
538                            }
539
540
541                            let is_close_curly_brace = char == '}';
542                            if is_close_curly_brace {
543                                number_of_closed_curly_braces = number_of_closed_curly_braces + 1;
544                            }
545
546                            key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
547
548                            if number_of_open_curly_braces == number_of_closed_curly_braces {
549                                read_char = false;
550                            }
551                        }
552                        
553                        // read till comma
554                        buf = vec![];
555                        let boxed_read = cursor.read_until(b',', &mut buf);
556                        if boxed_read.is_err() {
557                            let error = boxed_read.err().unwrap().to_string();
558                            let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
559                            return Err(message);
560                        }
561                        bytes_read = bytes_read + boxed_read.unwrap() as i128;
562                        if bytes_read == total_bytes {
563                            is_there_a_key_value = false;
564                        };
565
566                        let boxed_parse = String::from_utf8(buf);
567                        if boxed_parse.is_err() {
568                            let message = boxed_parse.err().unwrap().to_string();
569                            return Err(message);
570                        }
571                        let buffer_before_comma = boxed_parse.unwrap();
572                        let buffer_filtered_control_chars = StringExt::filter_ascii_control_characters(buffer_before_comma.as_str());
573
574                        if buffer_filtered_control_chars.chars().count() != 0 && buffer_filtered_control_chars != "}" && buffer_filtered_control_chars != "," {
575                            let message = format!("before comma there are some unexpected characters: {}", buffer_before_comma);
576                            return Err(message);
577                        } else {
578                            comma_delimiter_read_already = true;
579                        }
580                    }
581
582
583                    let is_number = char.is_numeric();
584                    if is_number {
585                        // read until char is not number and decimal point, minus, exponent
586
587                        key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
588
589                        let mut _is_point_symbol_already_used = false;
590                        let mut _is_exponent_symbol_already_used = false;
591                        let mut _is_minus_symbol_already_used = false;
592
593                        let mut read_char = true;
594                        while read_char {
595
596                            let byte = 0;
597                            let mut char_buffer = vec![byte];
598                            let length = char_buffer.len();
599                            let boxed_read = cursor.read_exact(&mut char_buffer);
600                            if boxed_read.is_err() {
601                                let message = boxed_read.err().unwrap().to_string();
602                                return Err(message);
603                            }
604                            bytes_read = bytes_read + length as i128;
605                            let boxed_parse = String::from_utf8(char_buffer);
606                            if boxed_parse.is_err() {
607                                let error = boxed_parse.err().unwrap().to_string();
608                                let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
609                                return Err(message);
610                            }
611                            let char = boxed_parse.unwrap().chars().last().unwrap();
612
613                            let is_numeric = char.is_numeric();
614                            let is_comma_symbol = char == ',';
615
616                            let is_point_symbol = char == '.';
617                            if is_point_symbol && _is_point_symbol_already_used {
618                                _is_point_symbol_already_used = true;
619                                let message = format!("unable to parse number: {}", key_value_pair);
620                                return Err(message)
621                            }
622
623                            let is_exponent_symbol = char == 'e';
624                            if is_exponent_symbol && _is_exponent_symbol_already_used {
625                                _is_exponent_symbol_already_used = true;
626                                let message = format!("unable to parse number: {}", key_value_pair);
627                                return Err(message)
628                            }
629
630                            let is_minus_symbol = char == '-';
631                            if is_minus_symbol && _is_minus_symbol_already_used {
632                                _is_minus_symbol_already_used = true;
633                                let message = format!("unable to parse number: {}", key_value_pair);
634                                return Err(message)
635                            }
636
637                            let char_is_part_of_number = is_numeric || is_point_symbol || is_exponent_symbol || is_minus_symbol;
638                            let is_delimiter = char == '\r' || char == '\n' || char == ' ';
639
640                            if !is_delimiter {
641                                if char_is_part_of_number {
642                                    key_value_pair = [key_value_pair, char.to_string()].join(SYMBOL.empty_string);
643                                } else {
644                                    read_char = false;
645                                    if char != '}' { // case where property is at the end of json object
646                                        if is_comma_symbol {
647                                            comma_delimiter_read_already = true;
648                                        } else {
649                                            let message = format!("there are not expected characters after number (expected comma): {} after {}", char,  key_value_pair);
650                                            return Err(message);
651                                        }
652                                    }
653                                }
654                            }
655
656
657                        }
658                    }
659
660                    let is_unknown_type =
661                        !is_string &&
662                            !is_null &&
663                            !is_boolean_true &&
664                            !is_boolean_false &&
665                            !is_array &&
666                            !is_number &&
667                            !is_nested_object;
668                    if is_unknown_type {
669                        let message  = "provided json is not valid";
670                        return Err(message.to_string());
671                    }
672
673                    is_whitespace_or_new_line_or_carriage_return = false;
674                }
675
676
677            }
678
679            // attempt to read till comma, indicates presence of another key-value pair
680            if !comma_delimiter_read_already {
681                buf = vec![];
682                boxed_read = cursor.read_until(b',', &mut buf);
683                if boxed_read.is_err() {
684                    let error = boxed_read.err().unwrap().to_string();
685                    let message = format!("error at byte {} of {} bytes, message: {} ", bytes_read, total_bytes, error);
686                    return Err(message);
687                }
688                bytes_read = bytes_read + boxed_read.unwrap() as i128;
689                if bytes_read == total_bytes {
690                    is_there_a_key_value = false;
691                };
692            }
693
694
695            let boxed_parse = JSONProperty::parse(&key_value_pair);
696            if boxed_parse.is_err() {
697                let message = boxed_parse.err().unwrap().to_string();
698                return Err(message);
699            }
700            let (property, value) = boxed_parse.unwrap();
701
702
703            properties.push((property, value));
704
705        }
706        Ok(properties)
707    }
708
709    pub fn to_json_string(key_value_list: Vec<(JSONProperty, JSONValue)>) -> String {
710        let mut json_list = vec![];
711        json_list.push(SYMBOL.opening_curly_bracket.to_string());
712
713
714        let mut properties_list = vec![];
715
716        for (property, value) in key_value_list {
717
718            if &property.property_type == "String" {
719                if value.string.is_some() {
720                    let raw_value = value.string.unwrap();
721                    let formatted_property = format!("  \"{}\": \"{}\"", &property.property_name, raw_value);
722                    properties_list.push(formatted_property.to_string());
723                }
724            }
725
726            if &property.property_type == "bool" {
727                if value.bool.is_some() {
728                    let raw_value = value.bool.unwrap();
729                    let formatted_property = format!("  \"{}\": {}", &property.property_name, raw_value);
730                    properties_list.push(formatted_property.to_string());
731                }
732            }
733
734            if &property.property_type == "i128" {
735                if value.i128.is_some() {
736                    let raw_value = value.i128.unwrap();
737                    let formatted_property = format!("  \"{}\": {}", &property.property_name, raw_value);
738                    properties_list.push(formatted_property.to_string());
739                }
740            }
741
742            if &property.property_type == "f64" {
743                if value.f64.is_some() {
744                    let raw_value = value.f64.unwrap();
745                    let mut _parsed_float = "0.0".to_string();
746                    if raw_value != 0.0 {
747                        _parsed_float = raw_value.to_string();
748                    }
749                    let formatted_property = format!("  \"{}\": {}", &property.property_name, _parsed_float);
750                    properties_list.push(formatted_property.to_string());
751                }
752            }
753
754            if &property.property_type == "object" {
755                if value.object.is_some() {
756                    let raw_value = value.object.unwrap();
757                    let formatted_property = format!("  \"{}\": {}", &property.property_name, raw_value);
758                    properties_list.push(formatted_property.to_string());
759                }
760            }
761
762            if &property.property_type == "array" {
763                if value.array.is_some() {
764                    let raw_value = value.array.unwrap();
765                    let formatted_property = format!("  \"{}\": {}", &property.property_name, raw_value);
766                    properties_list.push(formatted_property.to_string());
767                }
768            }
769        }
770
771
772        let comma_new_line_carriage_return = format!("{}{}", SYMBOL.comma, SYMBOL.new_line_carriage_return);
773        let properties = properties_list.join(&comma_new_line_carriage_return);
774
775        json_list.push(properties);
776        json_list.push(SYMBOL.closing_curly_bracket.to_string());
777        let json= json_list.join(SYMBOL.new_line_carriage_return);
778        json
779    }
780}