Skip to main content

rust_web_server/json/object/
mod.rs

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